I squash some commits in Git using git rebase -i origin/master
as mentioned in ReinH.com.
After squashing some commits, is there a way to see the original commits? Is it possible to get "diff" of the commits? Can I get the SHA?
If it's possible, is that still possible after running git gc
?
Using Interactive Rebase in Tower In case you are using the Tower Git client, using Interactive Rebase to squash some commits is very simple: just select the commits you want to combine, right-click any of them, and select the "Squash Revisions..." option from the contextual menu.
Even when a commit is squashed , it's still recoverable as the data is in the commit history. Once you know the SHA of the blob you want to recover, use: $ git cat-file -p <SHA> > recover_file.
The point of squashing commits is to rewrite history, replacing the original commits with a single commit.
That said, it's hard to make things actually disappear in git. The easiest way to get at those commits will be via git reflog
. Try git reflog <branch>
for the previous positions of the branch which you rebased.
You should be able to find the SHA1 of the tip of the branch just before your interactive rebase. (If the branch no longer exists, try git reflog show
to see the reflog of HEAD
. It should be there too, just more other activity to sort through.)
Once you have the SHA1, you're golden - use git log -p
or gitk
to view the commits and see their diffs. (If you want to do a lot with it, create a branch there, so you don't have to paste the SHA1 over and over again.)
This will still be possible after running git gc
, as long as it hasn't been a long time since you squashed those commits. gc
only prunes unreachable dangling objects over a certain age.
Commits are considered reachable if they're reachable from anything in the reflogs, and reflogs take 90 days to expire, so you can generally count on those original commits hanging around for three months.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With