Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the information of squashed commits in Git

Tags:

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?

like image 807
TK. Avatar asked Aug 29 '10 12:08

TK.


People also ask

How do you get squashed commits?

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.

Can you recover squashed commits?

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.


1 Answers

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.

like image 141
Cascabel Avatar answered Sep 28 '22 18:09

Cascabel