Open In App

How to Perform a Diff Between Two Stashes in Git?

Last Updated : 28 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git stashing is a handy feature that allows developers to temporarily shelve changes without committing them. However, there might be scenarios where you need to compare the changes stored in different stashes. Git does not offer a built-in command to directly compare stashes, but there are several approaches you can take to achieve this. In this article, we'll explore how to perform a diff between two stashes in Git.

Approach 1: Using Apply and Diff

One approach to comparing stashes is to apply them one by one and then use the git diff command to compare the changes. Here's how you can do it:

Step 1: List your stashes to identify the stashes you want to compare

git stash list

Note down the indices of the stashes you want to compare.

Step 2: Apply the first stash

git stash apply stash@{<index>}

Replace <index> with the index of the first stash.

Step 3: View the changes made by the first stash

git diff

Step 4: Apply the second stash

git stash apply stash@{<index>}

Replace <index> with the index of the second stash.

Step 5: View the changes made by the second stash

git diff

This method allows you to compare the changes introduced by each stash individually.

1

Approach 2: By Creating a Temporary Branch

Another approach is to create a temporary branch for each stash and then use standard Git diff commands to compare the branches. Here's how you can do it:

Step 1: Create a temporary branch for the first stash

git stash branch temp-branch1 stash@{<index>}

Replace <index> with the index of the first stash.

Step 2: View the changes made by the first stash

git diff master..temp-branch1

Step 3: Repeat steps 1 and 2 for the second stash, creating a separate temporary branch (temp-branch2)

Step 4: Compare the two temporary branches:

git diff temp-branch1..temp-branch2

This method provides a more structured approach, allowing you to compare the changes between the two stashes directly.

Conclusion:

Performing a diff between two stashes in Git requires a bit of manual intervention since Git doesn't provide a direct command for this purpose. However, by applying one of the methods outlined above, you can effectively compare the changes stored in different stashes and gain insights into the differences between them. Choose the method that best suits your workflow and preferences to streamline your development process.


Next Article
Article Tags :

Similar Reads