Reputation: 5993
How to redo or undo squash some selected commits on a branch, in simple word my question is
commits is A-B-C-D-E-F-G
commits is AB-C-D-EF-G
commits is A-B-CD-E-FG
Is there something I am wrong to let me correct. I am just trying to simplify my query
Upvotes: 0
Views: 88
Reputation: 51988
The action you did to go from 1.
to 2.
has rewritten your history, so G
in 2.
does not have the same hash as G
in 1.
Re-taking your notation :
- first time
commits is A-B-C-D-E-F-G
- after squash some commits they are
commits is AB-C'-D'-EF-G'
To undo the rewrite, you need to get the hash of your original G
commit (not G'
).
Your local git repo should still have it in its reflog :
# in the following list of commits, spot the one that would match your initial commit :
git reflog
# you will have a shorter list if you look at the reflog of your working branch :
git reflog my/branch
Once you have that hash :
git reset --hard <that hash>
,mandatory warning about git reset --hard
:
git reset --hard
is one of those few destructive git commands : if you have changes on your disk (on tracked files) that aren't stored in git, this command will forcefully drop these changes, and re-set the files content to the target commit's content.
This is why it is advised to look at your local changes first, and know for sure that the changes you discard are not important. The way to keep them is easy : just create a commit or a stash -- they will at least be reachable from the reflog afterwards.
Upvotes: 3