简体   繁体   中英

Git remove commit without removing merge commit

I have this:

*   ea626d4 (HEAD -> main) Merging
|\
| * 27069d1 (origin/main, origin/HEAD) 
* | 5b02356 
|/
* 8978bec

I want to remove commit 5b02356 but preserve commit ea626d4

* ea626d4 (HEAD -> main) Merging, maybe with changed name
|
* 27069d1 (origin/main, origin/HEAD) 
|
* 8978bec

How can I do it? I tried git rebase -i 8978bec , but it removes last commit,

git reset --soft 27069d1
git commit

With --soft , the reset command jumps back to the given commit, but without changing the working directory, and leaving the files that differed between the two commits marked as "to be committed". The new commit will have the same content as the original one, but will be a regular non-merge commit.

Note that it is not actually possible to modify a commit in any way (changing its content, message, author, timestamp, parent(s), whether it's a merge, etc.) - whenever git lets you "modify" a commit, it's actually creating a new commit with a different SHA (and typically dropping the reference to the original commit, so it looks like you made an in-place modification). So your resulting non-merge commit will have a different SHA; there is no way to make the change you want and retain the commit SHA ea626d4 .

I want to remove commit 5b02356 but preserve commit ea626d4

If you remove 5b02356 there's no need for a merge.

*   ea626d4 (HEAD -> main) Merging
|\
| * 27069d1 (origin/main, origin/HEAD) 
| | 
|/
* 8978bec

It might as well be...

* 27069d1 (origin/main, origin/HEAD) 
|
|
* 8978bec

You can force an empty merge with git merge --no-ff and that is useful to retain "feature bubbles"; indications that work was done in a branch.

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