Git Branches – Create Branch, Commit Branch, Merge, and Delete Branch

In Git, the branch is the working directory of the project. For example, if there are multiple developers and wants to work on multiple tasks parallel, then we need to create multiple branches so that all developers can work independently on their corresponding tasks.

 

By default, the master branch will be created with the Git repository. We should not work on the master branch directly because if an issue occurs, it is very difficult to get the original code base. Instead of working on the main master branch, we need to create a separate branch by giving a meaningful name. For example, if we work on task1. Give the branch name as task1-master which derives from the master branch. Based on your convenient, create one more branch task1-dev which derives from task1-master branch. You work on task1-dev branch and once your development completes, merge your changes from task1-dev to task1-master. After everything completes and if you feel the code is in the stable state then merge changes from task1-master to the main master branch.

 

Please remember one important thing here is we can create n number of branches in Git. Because in Git only main master branch maintains complete code base and all other derived branches will maintain only differences.

 

To view all branches, execute git branch command in the window. Execute this command on our git repository which we created in our previous articles.

 

C:\Examples\techExamples>git branch

* master

 

Here you can see only one branch master because we haven’t created any other branch.

 

Let’s create a new branch task1 by using git branch command and display all branches.

 

C:\Examples\techExamples>git branch task1

C:\Examples\techExamples>git branch

* master

  task1

 

C:\Examples\techExamples>

 

If you see here, we have created new branch task1 through git branch commands, and you can see task1 in branches list.

 

To work on task1 branch, first, we need to check out that branch through git checkout branch.

 

C:\Examples\techExamples>git checkout task1

Switched to branch 'task1'

 

C:\Examples\techExamples>

 

As shown above, we have made task1 as our working branch. Let’s add new file content.html to task1 branch.

 

C:\Examples\techExamples>git add content.html

 

C:\Examples\techExamples>git status

On branch task1

Changes to be committed:

  (use "git reset HEAD <file>..." to unstage)

 

        new file:   content.html

 

 

C:\Examples\techExamples>git commit -m 'AddedContentFile'

[task1 343d678] 'AddedContentFile'

 1 file changed, 8 insertions(+)

 create mode 100644 content.html

 

C:\Examples\techExamples>

 

As of now, changes are in task1 branch only, not in the master branch. We can check master branch content by checking out the master branch.

 

C:\Examples\techExamples>git checkout master

Switched to branch 'master'

Your branch is up to date with 'origin/master'.

c:\Examples\techExamples>git log

commit 3c13ce05a103a9108bd911b6f4b5e8829127334c (HEAD -> master, origin/master, origin/HEAD)

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Sun May 12 15:33:11 2019 +0100

 

    'AddTwoFiles'

 

commit 32ce97686389177d9ee7782767a77735e11ee391

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Sun May 12 01:00:08 2019 +0100

 

    'InitialChanges'

 

c:\Examples\techExamples>

 

As shown above, the master branch doesn’t have any commits related to the content file, that means the content file didn’t move to the master branch.

 

Let’s move to task1 branch and check whether it has content file related commits or not.

 

c:\Examples\techExamples>git checkout task1

Switched to branch 'task1'

 

c:\Examples\techExamples>git log

commit 343d678f1e93cf4533d757d15e8bc595cc7f725a (HEAD -> task1)

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Mon May 13 19:09:21 2019 +0100

 

    'AddedContentFile'

 

commit 3c13ce05a103a9108bd911b6f4b5e8829127334c (origin/master, origin/HEAD, master)

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Sun May 12 15:33:11 2019 +0100

 

    'AddTwoFiles'

 

commit 32ce97686389177d9ee7782767a77735e11ee391

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Sun May 12 01:00:08 2019 +0100

 

    'InitialChanges'

 

c:\Examples\techExamples>

 

As shown above, task1 branch has “AddedContentFile” commit. Now we will merge task1 branch changes to the master branch by using the git merge command.

 

c:\Examples\techExamples>git checkout master

Switched to branch 'master'

Your branch is up to date with 'origin/master'.

 

c:\Examples\techExamples>git merge task1

Updating 3c13ce0..343d678

Fast-forward

 content.html | 8 ++++++++

 1 file changed, 8 insertions(+)

 create mode 100644 content.html

 

c:\Examples\techExamples>git log

commit 343d678f1e93cf4533d757d15e8bc595cc7f725a (HEAD -> master, task1)

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Mon May 13 19:09:21 2019 +0100

 

    'AddedContentFile'

 

commit 3c13ce05a103a9108bd911b6f4b5e8829127334c (origin/master, origin/HEAD)

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Sun May 12 15:33:11 2019 +0100

 

    'AddTwoFiles'

 

commit 32ce97686389177d9ee7782767a77735e11ee391

Author: Raj <xxx.xxxx@hotmail.com>

Date:   Sun May 12 01:00:08 2019 +0100

 

    'InitialChanges'

 

c:\Examples\techExamples>

 

Now we have all our changes in the master branch. So let’s delete our task1 branch by using git branch -d command.

 

 

c:\Examples\techExamples>git branch -d task1

Deleted branch task1 (was 343d678).

 

c:\Examples\techExamples>git branch

* master

 

c:\Examples\techExamples>

 

Finally, push the changes to central repository VSO through git push command.

 

git push https://dev.azure.com/vsotech/_git/techExamples

 

c:\Examples\techExamples>git push https://dev.azure.com/vsotech/_git/techExamples

Enumerating objects: 4, done.

Counting objects: 100% (4/4), done.

Delta compression using up to 4 threads

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 362 bytes | 181.00 KiB/s, done.

Total 3 (delta 1), reused 0 (delta 0)

remote: Analyzing objects... (3/3) (7 ms)

remote: Storing packfile... done (122 ms)

remote: Storing index... done (43 ms)

To https://dev.azure.com/vsotech/_git/techExamples

   3c13ce0..cb15ab4  master -> master