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
300 views
in Technique[技术] by (71.8m points)

git branch - How do I move a commit between branches in Git?

I'm sure this is a simple thing that has been asked and answered, but I don't know what terms to search for. I have this:

    /--master--X--Y
A--B
    --C--D--E

Where I commited C, D, and E (locally only) on a branch, but then I realized that D and E are really independent of C. I want to move C to its own branch, and keep D and E for later. That is, I want this:

                   /--C
    /--master--X--Y
A--B
    --D--E

How do I yank C out from under D and E?

question from:https://stackoverflow.com/questions/3710192/how-do-i-move-a-commit-between-branches-in-git

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

1 Reply

0 votes
by (71.8m points)

You can use git cherry-pick to grab C, and put it on Y. Assuming Y exists as the tip a branch called branch-Y:

$ git checkout branch-Y
$ git cherry-pick C

So now C is on top of Y. But D and E also still contain C (cherry picking doesn't move a commit, it just makes a copy of it). You'll have to rebase D and E on top of B. Assuming E is the tip of branch-E and B is branch-B, you can:

$ git checkout branch-E
$ git rebase --interactive branch-B

This will open up an interactive rebase session. Just remove commit C entirely, and leave D and E intact. You'll then have D and E rebased on top of B without C.


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

...