简体   繁体   中英

How to redo squash some selected commits on branch

How to redo or undo squash some selected commits on a branch, in simple word my question is

  1. first time

commits is ABCDEFG

  1. after squash some commits they are

commits is AB-CD-EF-G

  1. but now i need to redo this second step, and squash commits like this way step
 commits is AB-CD-E-FG

Is there something I am wrong to let me correct. I am just trying to simplify my query

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:

  1. first time

commits is ABCDEFG

  1. 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:

  • make sure you have a clean repo (if you have local changes: stash them or discard them),
  • run git reset --hard <that hash> ,
  • and re-do your squashing.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM