How to check the changes between two git commits?

How to check the changes between two git commits?

Git is a widely used version control system for software development. It provides a great way to track changes made to the codebase and collaborate with other developers. One common scenario in software development is to identify what changes have been made between two git commits. This can help you understand what has been added, modified, or deleted from the codebase.

In this blog post, we will discuss how to find out what is happening between two git commits. We will cover the steps involved in identifying the changes and provide examples to make the process easier to understand.

Step 1: Identify the Commits

The first step in identifying changes between two git commits is to identify the two commits themselves. You can use the git log command to view the commit history and identify the commit hashes. For example:

git log --oneline

This will display a list of commits in a compact format, with each commit's hash and commit message displayed on a single line.

Suppose you want to compare changes between the commit with hash 123abc and the commit with hash 456def. In that case, you can use the following command:

git diff 123abc..456def

This will display the changes made between the two commits in a unified diff format.

Step 2: Analyze the Changes

Once you have identified the changes, the next step is to analyze them. The git diff command provides a lot of information, including which files were modified, which lines were added or deleted, and the content of the changes.

Suppose you want to see the changes made to a specific file between two commits. In that case, you can use the following command:

git diff 123abc..456def path/to/file

This will display the changes made to the specified file between the two commits.

You can also use other options with the git diff command to get more information. For example, the --stat option will display statistics about the changes made, such as the number of files changed, the number of lines added and deleted, and the total number of changes.

Step 3: Review the Changes

Once you have analyzed the changes, the final step is to review them. You can use the git diff command to apply the changes to a working copy of the code and review them in detail.

Suppose you want to apply the changes made between two commits to a new branch. In that case, you can use the following command:

git checkout -b new_branch 123abc
git cherry-pick 456def

This will create a new branch named new_branch based on the commit with hash 123abc. Then, it will apply the changes made in the commit with hash 456def to the new branch using the cherry-pick command.

Conclusion

In this blog post, we have discussed how to find out what is happening between two git commits. We covered the steps involved in identifying the changes, analyzing them, and reviewing them. By following these steps, you can easily track changes made to the codebase and collaborate with other developers more effectively.