如何将更改从一个分支移动到另一个分支 git?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8085838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:16:43  来源:igfitidea点击:

How to move the changes from one branch to another branch git?

git

提问by Ramy

How can I move my work and changes from the masterbranch to a newly created branch and leave the master branch intact after the move?

如何将我的工作和更改从master分支移动到新创建的分支并在移动后保持主分支完好无损?

采纳答案by poke

You can create a new branch pointing to the currentcommit using git branch branchname(or git checkout -b branchnameif you want to check it out directly). This will basically duplicate your master branch, so you can continue working on there.

您可以使用(或者如果您想直接签出)创建一个指向当前提交的新分支。这基本上会复制你的主分支,所以你可以继续在那里工作。git branch branchnamegit checkout -b branchname

If you have successfully copied the branch, you can reset masterto its original point by using git reset --hard commitwhere commitis the hash of the commit that should be the last one on master.

如果你已经成功复制了分支,你可以master使用git reset --hard commitwhere commitis the hash of the commit that should be the last one on master重置到它的原始点。

So for example you have a situation like this:

例如,您有这样的情况:

---- 1 ---- 2 ---- 3 ---- 4 ---- 5 ---- 6
                   ^                    ^
              original                master
            master commit

So you have checked out masteron commit 6, and you want to create a new branch ticketpointing to that 6while resetting masterto 3:

所以,你已经签出master的承诺6,并希望创建一个新的分支ticket指向该6复位时master3

git branch ticket
git reset --hard 3
git checkout ticket

And then you're on ticketpointing to commit 6, while masterpoints to 3.

然后你ticket指向 commit 6,而master指向3.

回答by xpioneer

If the changes are not commited.

如果未提交更改。

you can stash the changes in the master branch .

您可以将更改存储在 master 分支中。

git stash

then checkout the branch

然后结帐分支

git checkout -b newbranchname

and pop the changes here

并在此处弹出更改

git stash pop

If the changes are commited :

如果提交更改:

then create a branch :

然后创建一个分支:

git checkout -b newbranch

checkout back to master branch:

结帐回到主分支:

git checkout master

reset to previous commit :

重置为之前的提交:

git reset --hard head^1

回答by Mark Fisher

If you have commit (say) 2 times after you realised you should have been in a branch then simply do

如果你在意识到你应该在一个分支之后提交(比如)2次,那么只需执行

git branch work_branch
git reset --hard HEAD~2

replace the 2 with the number of commits back you want to go. You'll still be on master at this point, if you want to move to the branch to continue work, just git checkout work_branch

将 2 替换为您想要返回的提交次数。此时你仍然在 master 上,如果你想移动到分支继续工作,只需git checkout work_branch

see git rev-parse --helpif you want to understand the syntax for how to traverse back up your commit tree with references like HEAD~2

看看git rev-parse --help您是否想了解如何使用类似的引用遍历备份提交树的语法HEAD~2

回答by WillD

Create a newbranch from where you should have, and then cherry pick the changes on the incorrect branch into the new branch.

从您应该拥有的位置创建一个分支,然后将错误分支上的更改挑选到新分支中。

You can then delete the badbranch, assuming you haven't pushed it elsewhere and other changes have been made against it.

然后您可以删除分支,假设您没有将它推送到其他地方并且已经对其进行了其他更改。