This tutorial is an introduction to Git Branches, it is dedicated to
Kenth Hagström who submitted a request throw our Request a Tutorial page for
GIT branching and team development. For readers, we assume you have already read and understand our first tutorial for Introduction to Git commands and Version control system. This is the second tutorial of the Git commands series.
Introduction to Git Branch
Git branch is a general branch management tool from Git, it lets you create, list, rename and delete branches. There is no storage and memory overhead issue when you making many branches so it is easier to logically divide up your work than have big bulky branches. Now let’s dig into the Git Branches commands.
Prior to creating a new branch it is advisable to view all branches you have. You can view all branches by simply typing following command:
$ git branch
When you initialize your repository/working directory with command
git init git automatically creates one default branch named
Now lets assume you want to add a new image to your working directory (or you want to fix a bug in your project/working directory or something else, we will use adding image to project/working directory in our tutorial) and you want to do that in separate branch, later want to merge it with master branch. Let create separate branch for adding new image, we will named it
addnewimage with following command.
$ git branch addnewimage
Switching between branches
After creating new branch you need to move or switch into that new branch to perform git actions like add/commit same as you do into your master branch, you can switch with following command:
$ git checkout addnewimage
For above command
addnewimage is our newly created branch.
Git Add/Commit to New Branch
Assume you have now copy the image file into your working directory/project now lets add and commit that file to our new branch where we just moved. If you now use command
$ git status
You will notice that the pointer (*/star) is now moved to our newly created branch. Pointer always shows you in which branch you working.
You can simply use
git add and
git commit command which you already used in master branch. To understand those all basic command you need to read our pervious tutorial Introduction to Git commands and Version control system please refer to that tutorial before moving further here.
In above Git bash image I have draw pointers and explained at each and every point in red font.
Most of the Git command users stuck at this point. Git merge is very simple and easy to understand it works like it named
merge means combine, it only used with branches. When you happy with your new branch’s committed changes you can merge it with the master branch. To merge the new branch you first need to move back to master.
$ git checkout master
now merge our new branch named
addnewimage with following command,
$ git merge addnewimage
After merging new branch if you wish to delete you can simply use following command:
$ git branch –d addnewimage
You can now pull and push all your changes from/to remote repo to update your new branch changes which you just merged into master branch. It will shows you in you github repository.
you can see myimage.jpg which is pushed to my remote repo from my local repository via
git push command.
# Start a new feature
git branch addnewimage
# Move to that branch
git checkout addnewimage
git checkout -b addnewimage
# Add new image file
git add myimage.jpg
git commit -m “Added new image my image.jpg"
# move to the master branch
git checkout master
# Merge in the addnewimage branch to the master branch
git merge addnewimage
# If you not require your new created branch or you finish changes add already merged you can Delete the branch.
git branch -d addnewimage
In short branches are to work separately from your master branch, to avoid damage to your current repository/project, also with the beauty of branch many developers can work together in different branches for different points/bugs and later can merge each and every resolved bugs/new changes to master branch.
Hence branch especially says I want to include the work of this commits and all parents commits to master.