2

In a repository with squash-merge practice, can I go to the state of the repository in one of the squashed commits?

For the example below, I want to find commit m1 by checking out commit r1.

m1 - m2 ------- m3   <- master (r1, r2, and r3 are squashed into m3)
  \            
   r1 - r2 - r3  <- deleted branch
Guildenstern
  • 2,179
  • 1
  • 17
  • 39
Erdem Tuna
  • 500
  • 4
  • 18

2 Answers2

1

You can use git reflog --all. It lists all recent actions and related commit hashes. If you find the commit there, you can git checkout <commit_hash>.

Note: --all option is for listing reflogs of all references, not just the HEAD.

  • Thank you. When I tried it for a squash-merged PR two years before, this command did not yield the desired output. Am I missing any details? – Erdem Tuna May 23 '22 at 15:09
  • 2
    Unreferenced commits will most probably expire and removed by garbage collector after two years. Also those commits will not be pushed, pulled or cloned, so they will only be available in the repository it previously existed. – Özgür Murat Sağdıçoğlu May 23 '22 at 15:39
  • 2
    The notes about the garbage collector (`git gc`) and lack-of-clone are correct but since the OP mentions GitHub specifically and a "squash-merge practice", we might guess that the squash operation was done *on* GitHub. As GitHub never GC any commits by default, you may be able to recover them via GitHub's direct-commit-access URLs, if you know their hash IDs, or via the original PR if you have that information. Finding *those* can be problematic too of course. – torek May 23 '22 at 18:46
  • I am not sure but does bare repo clone help in this situation? like: `git clone --bare ....` – Özgür Murat Sağdıçoğlu May 25 '22 at 07:10
-2

in git when you squash commits actually you are deleting them from the history, so, you don't have any chance to rollback (as I know) but try the following command :

git log

if you find your commit you can open it in temp branch by using this command :

git checkout COMMIT_HASH
  • Not true. `git` doesn't delete anything. Not reachable (from branches and tags and its children) commits are just not shown and can be restored. `git reflog` can show all commits including those "deleted". When you find hash of commit you can cherry-pick it or checkout it. "Deleted" commits are garbage collected after some period (depends on configuration). – Marek R May 23 '22 at 11:36
  • Thank you, but this answer does not hold at all for the case described in the question. – Erdem Tuna May 23 '22 at 15:11