Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

Reverting a git merge while allowing for the same merge later)

What's the best way to revert a committed git merge while keeping the option of merging the same branch at a later point?

This is for a situation when I merge a branch into HEAD, then decide that I do not want these changes at the present, but still want the option to merge them into HEAD at some later point.

"git revert -m 1" reverts the code to the pre-merge state, but the branch is still somehow marked as "already merged". So a repeated merge with the same branch at a later point does not work.

This should be a very common problem (and git is an evolved tool), yet I cannot find a simple clean solution (apart from destroying the git repository and pulling it from remote again). Am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sorry to say that you have wasted your branch.

But there is a workaround. The trick is to "rewrite" the merge commit temporarily so that it forgets that the branch is a parent. Assume X is the merge commit:

git replace --graft X X^   # pretend that there is just one parent
git merge branch           # merge the branch again
git replace --delete X     # remove the replacement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...