Lets go GIT 2 — Learning Simplified

Ghanshyam Varindani
4 min readJul 19, 2020
Lets go GIT

In our previous tutorial we learnt the basic of GIT and features of working on master branch. In this tutorial I will be focusing on branching and merging concepts of GIT

Previous Tutorial Reference: https://medium.com/@varindani.ghanshyam/lets-go-git-d5d52ce150c1

Branching:

We learnt how to add and modify files on GIT. In cases explained, we were making changes on master branch which is not the standard approach followed in any development. Hence it become utmost important to know how to work on individual copy and request for merge. This will help developer follow standards of Dev/QA/Prod repositories. We will learn here how to create branches, also known as feature or topic branches from Master branch, perform the work and get it to stable form and merge with master branch.

Step : How to create branch. First clone the master branch from git project. Once cloned, run below command “git branch -a”. The command will show you are on master branch on local and have remote pointer-HEAD and remote branch as master.

git branch -a

Create local branch with command “git branch newbranch”

git branch newbranch

To switch branch run command “git checkout newbranch”

bit checkout newbranch
git branch -a

In order to delete branch, checkout to branch other than one to be deleted and write command “git branch -d newbranch”

Now, we know how to create branches, lets learn how to merge your changes to master branch

Fast Forward Merge

One can use below command to create branch and checkout to branch “git branch -b <<branchname>>, in our ex. git branch -b featurebranch

Once in featurebranch, modify one of the file and save. In our case, we will modify sample.txt and add text to it.

Add to the staging and local repository using git add and git commit command. Once done change your branch to master branch with command git checkout master

git checkout master

Fast forward merge work in scenario when there is no change expected to be on master or you are the only one who will make a change on master. In order to merge featurebranch to master branch we have to find the difference between two branches using command “git diff <<source>> <<target>>

git diff master featurebranch

Once difference are reviewed, we can go ahead and merge the two branches using command “git merge <<source>><< target>>

git merge master featurebranch

The above will work if you are the owner of master branch and have rights to push on to master branch in remote repository. Say, if you are developer and would like to push changes to master branch, you can follow below steps.

Developer can create feature branch and push it to gitlab and submit request for merge(when master merge is restricted with an approval). Lets review an example:

Create branch name “feature1” on local machine. This will copy your master branch files in feature1 branch.

git branch feature1

Checkout branch feature1 and make modification to sample.txt file. Once done commit the change with git commit -am “<<text>>”

post making change to sample.txt

Once changes are committed on git local repository, its time to push branch feature1 on remote repo/project.

git push origin feature1

You will be able to see feature1 branch on gitlab.

Next step is to submit the merge request to “maintainer” who will review your code and approve merge into master branch. Go to merge request and submit New Merge request.

SELECT source and target branches
Fill in the merge request, with option of deleting branch if no longer needed
Request will be submitted to approver, who will review conflict if any and approve merge

Once approved master branch will contain the code from feature1 and feature1 branch will be deleted.

feature branch now doesn’t exist

In this tutorial, we learnt how to create feature branches to work in silos and post completion how to submit the merge request to merge code into master branch.

****************************************************************

--

--