In this article, we’ll guide you through the process of comparing differences between two Git branches, and we’ll also show you how to exclude specific directories from this comparison.
Prerequisites:
Before you proceed, ensure:
- You have Git installed on your machine.
- You’re located within the Git repository where your branches exist.
1. Comparing differences between two Git branches:
To see the differences between your current branch and the master branch, you can use the git diff
command. Here’s a general structure of the command:
git diff <branch1>..<branch2> --name-only
For your specific scenario:
git diff master..freshers-in-JIRA-1988 --name-only
This command will provide a list of files that differ between the master branch and the freshers-in-JIRA-1988 branch.
2. Ignoring changes in specific directories:
If you’d like to ignore changes in a specific directory while comparing two branches, you can utilize the ‘:(exclude)path_to_directory/’ notation with the git diff command.
Here’s how you can compare differences while ignoring changes made in the /temp/files/ directory:
git diff master..freshers-in-JIRA-1988 --name-only ':!temp/files/'
This command will give you the differences between the two branches, but it will exclude any changes present in the /temp/files/ directory.
3. Compare file changes between branches for a specific directory
If you’re aiming to compare the freshers-in-JIRA-1988 branch with the master branch, specifically to see file changes inside the freshers/credentials/ directory, you can use the git diff command.
Ensure you have the latest information from your remote repository:
Before you start comparing, it’s a good idea to make sure you have the most recent updates from your remote repository.
git fetch origin
Compare file changes between branches for a specific directory:
Navigate to your repository’s root directory, then use the git diff command with the required path:
git diff master..freshers-in-JIRA-1988 -- freshers/credentials/
This command will show the detailed differences between the two branches for files within the freshers/credentials/ directory.
List only the changed files:
If you’re only interested in seeing which files have changed, rather than the specific lines that have changed, you can use the –name-only option:
git diff master..freshers-in-JIRA-1988 --name-only -- freshers/credentials/
This will give you a list of files within freshers/credentials/ that differ between the freshers-in-JIRA-1988 branch and the master branch.