In Part 1 we looked at Git configuration and in Part 2 – creating and cloning repository, commit and push files. In this part lets discuss about “branches”..
As soon as we create a new repository either in GitHub or locally or when we clone existing repository – “master” branch will be created by default.
Ideally we do not want to make changes to “master” branch as in most of the organizations I’ve worked “master” branch is “production” and for the same reason we also do not want to make multiple commits to “master” branch. I’ve also seen cases where developers do not have permission to merge changes directly to “master” branch but instead have their own working branch(es) – do development, testing and once happy raise “merge” request and the administrator can commit to master branch.
# git branch – list all branches
#git branch <branch_name> – creates branch
#git checkout <branch_name> – go to the branch
#git checkout -b <branch_name> – it creates branch and does checkout
#git merge <branch_name> – merges the branch_name to the branch we are in. For example if you want to merge branch A into B ( git checkout B && git merge A ).
#git branch -d <branch_name> – deletes branch
Branches well explained in atlassian document : https://www.atlassian.com/git/tutorials/using-branches
Lets create a “test1” branch..
You can see a * infront of the branch which indicates the branch we are in. “git status” also shows the branch we are in. Lets check files in both branches..
Now create a file in “test1” branch and commit..
Compare with “master” branch.. As you can see below “test1branch.txt” file present in “test1” branch but not in master.
Now lets merge the “test1” with “master”.. You need to be in “master” branch and run the merge.. Merging the branch won’t delete the “test1” branch. If there are any conflicts git highlights and ask the user to resolve them.
So far our changes are local and no updates done to the remote – GitHub repository. From the below screenshot we can see the hint from git – local master ahead of remote by 1 commit.
Merge explained in the document: https://www.atlassian.com/git/tutorials/git-merge