Git Compare Two Branches
1. Introduction
This is an in-depth article related to Compare Two Branches of Git. Git was developed by Vincent Driessen in 2010. Git typically has two branches in its repositories like master and develop. The development branches can be feature-specific, hot fix-specific, release-specific, and trunk. Most of the time, developers want to compare two branches to know the changes which were committed by the developer.
2. Git Compare
A typical Git branching strategy depends on the stage of the development lifecycle, and release plan. Developers can have their own branches for development. Features are developed during the sprint in the releases. Branches are created for different features. Feature-specific branches are very common and developers want to know the changes before merging to the master or leads want to review the code through merge requests or branch comparison tools.
2.1 Prerequisites
Java 7 or 8 is required on the Linux, windows, or Mac operating system. Maven 3.6.1 is required for building this application. A GitHub account is required.
2.2 Download
You can download Java 8 can be downloaded from the Oracle website. Apache Maven 3.6.1 can be downloaded from the Apache site. GitHub can be accessed at this link. Gitkraken can be downloaded from this site. Git can be downloaded from this link.
2.3 Setup
You can create an account on GitHub account using the link. You can create a GitHub repository with a Calculator application Java code. You can add the file manually to get started. The code is available at the git repository.
2.4 Checking the status of the repository
In git main branch is the main (master in initial versions of git). The main branch has the production-ready release. The main branch is created first. Development branches are created till the development release completes the test stage. This code is pre-production and deployed in the acceptance stage for acceptance testing. Once the testing is complete, this branch is merged into the main through pull requests.
Developers would like to see the status of their repositories related to the addition, modification, and deletion of files in that branch. Let us see the command to check the status of the repository.
2.4.1 Using the “git status” command
git status helps in finding the status of the current branch or the code base.
git status command
(base) apples-MacBook-Air:git_compare bhagvan.kommadi$ git clone https://github.com/bhagvank/gitexamples.git Cloning into 'gitexamples'... remote: Enumerating objects: 22, done. remote: Counting objects: 100% (22/22), done. remote: Compressing objects: 100% (9/9), done. remote: Total 22 (delta 1), reused 19 (delta 1), pack-reused 0 Unpacking objects: 100% (22/22), done. (base) apples-MacBook-Air:git_compare bhagvan.kommadi$ ls gitexamples (base) apples-MacBook-Air:git_compare bhagvan.kommadi$ cd gitexamples (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ ls MavenHello README.md (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git status On branch main Your branch is up-to-date with 'origin/main'. nothing to commit, working tree clean (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.4.2 Ensuring that the repository is clean before comparing branches
Before comparing the two branches, we need to have the repository clean. git clean removes the untracked files in the repository. git clean has flags, -n for checking the files which will be deleted and -f for forcefully removing the untracked files.
git clean command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git clean fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git clean -n (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git clean -f (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.5 Choosing the branches to compare
New features will be coming out of the development release branches. On the main, tags can be created for different releases. After production, you can have branches specific to release and hotfix. During development, feature-specific branches are created. Feature branches are merged into the development release through a pull request. The pull request is reviewed for merge approval. A release branch is for production release preparation. Defects found on testing will be resolved on the release branch before merging into the main branch. The Hotfix branch is used for handling important fixes or patches to the releases. They are merged into the main and tags can be created for hotfixes.
In many release scenarios, comparisons of release and hotfix branches are common. Similarly, a comparison of a feature-specific branch with the release branch happens in every project.
2.5.1 Identifying the names of the branches
Branches can be identified by the names specified. You can use the git branch to find the list of the local and remote branches. Below are the commands and the output for those commands:
git branch command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git branch * main (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git branch -r origin/HEAD -> origin/main origin/main (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git branch -a * main remotes/origin/HEAD -> origin/main remotes/origin/main (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ vi checkbranch.txt (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git checkout -b newbranch Switched to a new branch 'newbranch' (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git add . (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git commit -m "created new branch" [newbranch a0451ef] created new branch 1 file changed, 1 insertion(+) create mode 100644 checkbranch.txt (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git push origin newbranch Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 330 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: remote: Create a pull request for 'newbranch' on GitHub by visiting: remote: https://github.com/bhagvank/gitexamples/pull/new/newbranch remote: To https://github.com/bhagvank/gitexamples.git * [new branch] newbranch -> newbranch (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git branch main * newbranch (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git branch -r origin/HEAD -> origin/main origin/main origin/newbranch (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git branch -a main * newbranch remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/newbranch (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.5.2 Determining which branch is the base branch and which is the comparison branch
The base branch is the branch over which the changes were committed. The comparison branch is the one that has the changes committed. A head branch is the one where the changes will be integrated. You can use git branch -a to find the local and remote branches. The one with HEAD specified as the origin is the parent branch of the current branch. You can see the output of the command below:
git branch -a command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git branch -a main * newbranch remotes/origin/HEAD -> origin/main remotes/origin/main remotes/origin/newbranch (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.6 Comparing the branches
A comparison of branches is done to identify the changes and to find where the merge conflicts or issues.
2.6.1 Using the “git diff” command
Let us try git diff of the new branch with the old branch -main and see the output.
git diff command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git diff newbranch main diff --git a/checkbranch.txt b/checkbranch.txt deleted file mode 100644 index 9f70100..0000000 --- a/checkbranch.txt +++ /dev/null @@ -1 +0,0 @@ -check branch (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.6.1.1 Syntax and options of the “git diff” command
Now let us look at git diff command, the syntax and the options.
git diff syntax & options
git diff
git diff command shows the changes between the working directory and the staged directory mapped to the branch. If you do not have any staged commits, git diff will show blank. You can see the output below:
git diff command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git diff diff --git a/checkbranch.txt b/checkbranch.txt index 695e2ed..773b69b 100644 --- a/checkbranch.txt +++ b/checkbranch.txt @@ -1,2 +1,2 @@ check branch -second line +second line2 (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.6.1.2 Specifying the branches to compare with “git diff”
git diff command with branches as the options will compare branch1 to branch2.
git diff syntax & options
git diff
2.6.2 Using the “git log” command
git log command shows the log of the committed changes from the origin main branch to the current version of the branch. Below is the output after the second change in the new branch which originated from the main branch.
git log command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git log commit ba232398f8afa4759496bac1414b92a90ca59f87 Author: bhagvank Date: Tue Apr 11 09:06:02 2023 +0530 made the second change commit a0451ef07bff806a78b02d541642726198b28538 Author: bhagvank Date: Tue Apr 11 08:50:06 2023 +0530 created new branch commit 115e991d329a4e747bb4e7303728705f714a60bd Author: bhagvank Date: Sun Feb 5 17:24:12 2023 +0530 added mavenhello commit 928203dd7fe12fd519287f0dd4e3ba5fccc92d64 Author: Bhagvan Kommadi Date: Wed Oct 5 23:19:18 2022 +0530 Initial commit (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.6.2.1 Syntax and options of the “git log” command
git log syntax and options
git log --oneline git log --stat git log --patch git log -p git log --graph git log --graph --oneline git log --grep=" Commit message." git log -author="@gmail.com" git log --after="2019-11-01" --before="2019-11-08 " git log --graph --oneline git log -p git log --patch
2.6.2.2 Specifying the branches to compare with “git log”
git log can be used to compare two branches, the main and the new branch. Let us say you want to see the differences between the new branch and the main. You can use the following command and the output is shown below:
git log to compare branches
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git log newbranch --not main commit ba232398f8afa4759496bac1414b92a90ca59f87 Author: bhagvank Date: Tue Apr 11 09:06:02 2023 +0530 made the second change commit a0451ef07bff806a78b02d541642726198b28538 Author: bhagvank Date: Tue Apr 11 08:50:06 2023 +0530 created new branch (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.6.3 Using a graphical user interface (GUI) Tool
There are many GUI tools to manage code in Git. Tools are available for Windows, macOS, and Linux. Developers and other software professionals can use the tools without any commands. GUI is easy to view, compare, and delete code in the repository. Version, Tag, and branch management is also easy with GUI
2.6.3.1 Overview of Git GUI Tools
Some of the popular tools available are listed below:
- GitKraken
- GitHub Desktop
- Fork
- Sourcetree
- Tortoise Git
- SmartGit
- GitForce
- Git Cola
- Aurees
- Magit
2.6.3.2 Comparing branches with a GUI tool
GitKraken can be used if you are new to Git for quick deployment and code development. You can productive and efficient by using Gitkraken tools for git workflows. GitKraken GUI helps in managing the repositories. The risk comes down in the resolution of conflicts and undoes feature helps in getting things back to the previous state. The Merge tool is provided in the Gitkraken application for resolving conflicts. You can also see the diffs using this tool between different branches. Gitkraken is cross-platform and makes Gitflow possible for code development.
You can use it for comparing branches as shown below:
2.7 Reviewing the differences
Differences in the branches after comparison are analyzed as modifications, news, and deletes. This helps to find the impact of the branch if merged into a release branch. Similarly, a hot fix or patch comparison with the release branch helps in reviewing the code and also helps in assessing if there is any risk of breaking the build.
2.7.1 Analyzing the output of the “git diff” command
git diff command helps in analyzing the differences between two branches. You can see the differences between the new branch and the main below:
git diff command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git diff diff --git a/checkbranch.txt b/checkbranch.txt index 695e2ed..773b69b 100644 --- a/checkbranch.txt +++ b/checkbranch.txt @@ -1,2 +1,2 @@ check branch -second line +second line2 (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.7.2 Reviewing the commit history with the “git log” command
You can review the commit history using the git log command. You can see the output below:
git log command
(base) apples-MacBook-Air:gitexamples bhagvan.kommadi$ git log commit ba232398f8afa4759496bac1414b92a90ca59f87 Author: bhagvank Date: Tue Apr 11 09:06:02 2023 +0530 made the second change commit a0451ef07bff806a78b02d541642726198b28538 Author: bhagvank Date: Tue Apr 11 08:50:06 2023 +0530 created new branch commit 115e991d329a4e747bb4e7303728705f714a60bd Author: bhagvank Date: Sun Feb 5 17:24:12 2023 +0530 added mavenhello commit 928203dd7fe12fd519287f0dd4e3ba5fccc92d64 Author: Bhagvan Kommadi Date: Wed Oct 5 23:19:18 2022 +0530 Initial commit (base) apples-MacBook-Air:gitexamples bhagvan.kommadi$
2.7.3 Identifying and understanding the differences between the branches
Identifying and understanding the differences between the branches is very important for the health of the build and product. If the changes and differences are analyzed, it will be helpful to analyze the defects and state of the build.
2.8 Resolving conflicts
Resolving conflicts between branches is about ensuring the branches can be merged without any issues. Conflict arises because of small changes which are done on the same file by the developer.
2.8.1 Understanding conflicts
Understanding conflicts and realizing the value of clean code and no conflicts is very important. Conflicts can be analyzed while merging the code from two different branches.
2.8.2 Resolving conflicts manually
Manually is to take backup of the branch which has issues. Issues are resolved after observing the sequence and logic associated with the commits.
2.8.3 Using Git Tools to resolve conflicts
Git tools like gitkraken can be used for resolving conflicts.
2.9 Conclusion
The git compare command helps in analyzing two branches and their differences. It will be good to know the commits done to two different branches with history
2.9.1 Summary of comparing branches in Git
Comparing branches in Git helps in identifying the impact of merging a branch into a release branch. The merge conflicts can be resolved manually or automated tools can be used for it.
2.9.2 Importance of comparing branches in Git for collaboration and version control
Git tools like Gitkraken provide a user interface that is helpful for collaboration and version control management.
3. Download the Source Code
You can download the full source code of this example here: Git Compare Two Branches