This the second part of the series on Git. I hope that you have gone through the commands mentioned in the first part “Introduction To Code Version Control”. In this part, I will discuss the concept of branches and the various operations you could perform in Git to manage branches. To round things off, I will highlight Git commands that will help you manage branches and resolve branch conflicts.
Understanding Branches
Branches are separate working environments complete with working directories and staging environment. Every branch has its record of commits that could be merged with the master branch upon the successful completion of the project.
By default, every Git has a master branch. However, the developer(s) could create any number of branches. The developer could move across branches easily using commands. To manipulate branches (create, delete and rename) Git offers the command git branch. However, this command will not allow merging of the code and moving to another branch.
Why We Create Branches
At this point, an important question arises: Why do I need other branches, if I have a master branch for the project?
Let suppose you have a project where several developers produce several thousand lines of code. Every developer commits changes to the master branch. Now suppose, you wish to test specific features of the project without interfering with the main branch of the project. To do this, you need to create a separate branch of the code that you could safely test features without breaking the main code.
Working With Branches In Git
Working with branches in Git involves several commands:
git branch – List All Branches
This command shows a list of branches. Run the git branch in your terminal.
The * indicates that I am currently on the master branch.
git branch <branch> – Create New Branch
It is easy to create new branches. Remember that a git branch is only a pointer placed on the the master branch when committing a new change. For creating branches, pass the name of branch with command:
git branch <branch name>. In the case of the example, the commands would look like: git branch newbranch
Now, When I run git branch again, the results will show that a new branch is added to the list.
Creating branches does not change the history of the repository. Instead, they add a pointer to the commit.
This command could also be used to change the name of the branch. To do this, Pass the new and old name of the branch in the following command:
git branch -m <old name> <new name>. For instance, the command git branch -m example title_branch will rename example branch to title_branch.
git branch -d <branch name> – Delete a Branch
Deleting a branch is easy enough!
Remember that before deleting a branch, make sure that you have merged it in the master branch. Once done, pass the branch name with git command to delete it.
git branch -d <branch name>. For instance, I have used git branch -d demobranch
If you do not merge the branch before deletion, An exception will be thrown:
error: The branch ‘crazy-experiment’ is not fully merged.
If you are sure you want to delete it run ‘git branch -D <branch name>’
The branch could be forced-deleted through the command git branch -D demobranch
git checkout <branch> – Switching Between Branches
Switching between branches is easy! The command is:
git checkout <branch name>. For instance, git checkout newbranch
At this stage, when I run the command: cat .git/HEAD. You should see that the reference Head has been also been set to this branch.
git checkout -b <branch> – Create & Switch Branches at the Same Time
You could create and switch to a newly created branch at the same time by this command.
git checkout -b newbranch
The checkout command tells Git to fetch the latest changes of the branch and ensure that the working directory has the latest code of that branch.
git checkout – Switching Branches with Uncommitted Changes
Git does not allow you to switch branches when you have uncommitted changes in the code.
Suppose, I have made changes in index.php. And, now I want to switch to newbranch from master. The command will show uncommitted file like this:
The results shows that there is an uncommitted file index.php.
git diff – Comparing Branches
You can also compare branches to see the changes between the two. For this, Git provides a comparison operator (..). Remember, for this operator to work, all changes should be committed in all the branches. To see the differences, run the command: git diff <branch name>..<branch name>. For instance: git diff master..newbranch
You can see the different versions of same code in the two branches. You need to choose which version is the old one.
If you want to see the differences in same div, you can run the following command:
git diff –color-words master..newbranch. The result is:
You can also check whether the branches have the same version of code or that the branches differ in commits. For instance, you can check which branch has (or hasn’t) all the commits of other branches. Run the following command:
git branch –merged. In my case, the result looks like:
This means that the master branch has all the commits of the example branch, but it does not have all the commits of the newbranch.
git clone – Cloning Branches From the Repository
You probably know about cloning GitHub or Bitbucket projects using the HTTP URL: The command is:.
git clone https://github.com/shahroznawaz/testdemo.git
However, remember that when you clone a repository, it creates a local copy of the master branch.
The local copy also has information of other branches, but remember that you need to pull individual branches from the repository. Run git branch -a and you will see the branches highlighted in red. The Head is placed on the master branch which was pulled.
Now to pull up the second branch, which is BR-02. Run the checkout command:
git checkout -b BR-02 origin/BR-02
This command will pull up the branch BR-02 and, additionally, create a local copy with the same name.
Now if you want to clone every branch from GitHub, you need to run some commands. First clone the repository with this clone command with the suffix .git
git clone –mirror https://github.com/shahroznawaz/testdemo.git .git
At this point, you will only see the .git folder because this is just an bear repository. Next run the command:
git config –bool core.bare false
The final command is:
git reset –hard. Now you will see all the branches from the GitHub repo.
Related: A Cheat Sheet For Git Commands
Merge Branches & Resolve Conflicts
While working on several branches, you need to merge the commits of individual branches into the master. This should be done when you have successfully tested the new feature code in a separate branch and now you are ready to merge it with the master branch.
Merge Two Branches
So far, I was working with two branches: master and newbranch. I will now add one more branch package and next, add some commits to it. I will now show how to merge the package branch.
First switch to the master branch and run the following Git command:
git merge <branch>. In my example, the command would be git merge package
Do the git log command to see whether the commits merged in the master. Merging branches is a complex process and often leads to merge conflicts. I will now define what conflicts are how to resolve these conflicts!
Why Conflicts Occur
In simple terms, conflicts occur when changes are made to the same line by two different people. Similarly, conflicts could also arise if one person edits a file and another deletes it.
To demonstrate the concept of conflicts, I have made changes in the same line of the index.php file in both branches. Now, when I merge these branches, a conflict message popups like this.
At this stage, when I open index.php, I discover that it contains extra message lines.
Changes in branch master: “amount” -> “total_amount”
Changes in branch newbranch: 10000 -> 50000
It adds the <<<<<<<Head line to indicate that the Head contains this change in master. And >>>>>>>newbranch showing the change in itself.
In this scenario, Git is not smart enough to merge changes itself. It will ask you to determine the latest copy of the code yourself.
Resolving Merge Conflicts
Git provides three ways of resolving conflicts:
- Aborting the merge
- Resolving conflicts manually
- Using Tools
If you don’t want to merge the branches, you can simply abort the merge process by following command:
git merge –abort
This command will abort the merge process and get you out of the conflict.
For resolving conflicts manually, you need to look at the file and remove the unwanted version changes in the file. Also remove >>>>>>Head & ====== from the lines and make sure that the file has proper code syntax. Save and commit the file.When you are between merge states, you do not need to write a commit message. Just type git commit and the pop-up window will indicate the conflict in the files. Remove the lines to resolve the conflict.
Now press Ctrl+O to overwrite the file. The command will ask you to enter the file’s name and then save it. Now check whetherthe two branches, master and newbranch will merge. Type git log –oneline and it will show all the commits in the master. Check the top most commit.
This means that the two branches have been successfully merged. Reconfirm this by running the command git branch –merge. The output will be all the branches that are merged in the master.
Several open source tools focus on working with Git and especially conflict resolution using a GUI that greatly simplifies matters. I cover some of these tools in a separate article. Git has a command to list all tools which can resolve conflicts. Simply type git mergetool and you will see thee list of available tools.
Stop Wasting Time on Servers
Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.
Final Words
In this article I highlighted the concept of branches and how you could carry out branch related operations (creating, deleting, renaming and switching) using commands. I also discussed conflicts that arise during working with branches and how you could resolve these issues. I hope, by now, you could manage branches and resolve conflicts on your own. If you want to contribute to the article or have a question about the Git branches, feel free to comment below.
Shahroze Nawaz
Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling.