git 将更改的文件移动到另一个分支以进行签入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7217894/
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
moving changed files to another branch for check-in
提问by mainsocial
This often happens to me: I write some code, go to check in my changes, and then realize I'm not in the proper branch to check in those changes. However I can't switch to another branch without my changes reverting. Is there a way to move changes to another branch to be checked in there?
这经常发生在我身上:我写了一些代码,去检查我的更改,然后意识到我不在正确的分支中检查这些更改。但是,如果我的更改没有恢复,我就无法切换到另一个分支。有没有办法将更改移动到另一个分支以在那里检查?
回答by Bill Door
git stash
is your friend.
git stash
是你的朋友。
If you have not made the commit yet, just run git stash
. This will save away all of your changes.
如果您还没有提交,只需运行git stash
. 这将保存您的所有更改。
Switch to the branch you want the changes on and run git stash pop
.
切换到您想要更改的分支并运行git stash pop
。
There are lots of uses for git stash. This is certainly one of the more useful reasons.
git stash 有很多用途。这当然是更有用的原因之一。
An example:
一个例子:
# work on some code
git stash
git checkout correct-branch
git stash pop
回答by Amber
If you haven't already committed your changes, just use git checkout
to move to the new branch and then commit them normally - changes to files are not tied to a particular branch until you commit them.
如果您尚未提交更改,只需使用git checkout
移动到新分支,然后正常提交它们 - 对文件的更改不会绑定到特定分支,直到您提交它们。
If you havealready committed your changes:
如果您已经已经提交改变:
- Type
git log
and remember the SHA of the commit you want to move. - Check out the branch you want to move the commit to.
- Type
git cherry-pick SHA
substituting the SHA from above. - Switch back to your original branch.
- Use
git reset HEAD~1
to reset back before your wrong-branch commit.
- 输入
git log
并记住要移动的提交的 SHA。 - 检查您要将提交移动到的分支。
- 键入
git cherry-pick SHA
替换上面的 SHA。 - 切换回原来的分支。
- 用于
git reset HEAD~1
在错误分支提交之前重置。
cherry-pick
takes a given commit and applies it to the currently checked-out head, thus allowing you to copy the commit over to a new branch.
cherry-pick
接受给定的提交并将其应用于当前检出的头部,从而允许您将提交复制到新分支。
回答by watashiSHUN
Sadly this happens to me quite regularly as well and I use git stash
if I realized my mistake before git commit
and use git cherry-pick
otherwise, both commands are explained pretty well in other answers
可悲的是,这也经常发生在我身上,git stash
如果我之前意识到我的错误,我会使用它git commit
,git cherry-pick
否则使用,这两个命令在其他答案中都得到了很好的解释
I want to add a clarification for git checkout targetBranch
: this command will only preserve your working directory and staged snapshot if targetBranch has the same history as your current branch
我想澄清一下git checkout targetBranch
:如果 targetBranch 与当前分支具有相同的历史记录,则此命令将仅保留您的工作目录和暂存快照
If you haven't already committed your changes, just use git checkout to move to the new branchand then commit them normally
如果您还没有提交更改,只需使用 git checkout 移动到新分支,然后正常提交它们
@Amber's statement is not false, when you move to a newBranch,git checkout -b newBranch
, a new pointer is created and it is pointing to the exact same commit as your current branch.
In fact, if you happened to have an another branch that shares history with your current branch (both point at the same commit) you can "move your changes" by git checkout targetBranch
@Amber的声明不是错误的,当您移动到newBranch 时,git checkout -b newBranch
会创建一个新指针,它指向与当前分支完全相同的提交。
事实上,如果您碰巧有一个与当前分支共享历史记录的另一个分支(都指向同一个提交),您可以通过以下方式“移动您的更改”git checkout targetBranch
However, usually different branches means different history, and Git will not allow you to switch between these branches with a dirty working directory or staging area. in which case you can either do git checkout -f targetBranch
(clean and throwaway changes) or git stage
+ git checkout targetBranch
(clean and savechanges), simply running git checkout targetBranch
will give an error:
然而,通常不同的分支意味着不同的历史,Git 不允许你在这些带有脏工作目录或暂存区的分支之间切换。在这种情况下,您可以执行git checkout -f targetBranch
(清除和一次性更改)或git stage
+ git checkout targetBranch
(清除并保存更改),只需运行git checkout targetBranch
就会出错:
error: Your local changes to the following files would be overwritten by checkout: ... Please commit your changes or stash them before you switch branches. Aborting
错误:您对以下文件的本地更改将被检出覆盖:...请在切换分支之前提交您的更改或隐藏它们。中止
回答by JSON C11
A soft git resetwill put committed changes back into your index. Next, checkout the branch you had intended to commit on. Then git commitwith a new commit message.
一个软复位的git会把提交更改回你的指数。接下来,检查您打算提交的分支。然后使用新的提交消息git commit。
git reset --soft <commit>
git checkout <branch>
git commit -m "Commit message goes here"
git reset --soft <commit>
git checkout <branch>
git commit -m "Commit message goes here"
From git docs:
从git 文档:
git reset [<mode>] [<commit>]
This form resets the current branch head to and possibly updates the index (resetting it to the tree of ) and the working tree depending on . If is omitted, defaults to --mixed. The must be one of the following:
--soft
Does not touch the index file or the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.
git reset [<mode>] [<commit>]
这种形式将当前分支头重置为并可能更新索引(将其重置为 的树)和工作树取决于 。如果省略,则默认为 --mixed。必须是以下之一:
--soft
根本不接触索引文件或工作树(但将头部重置为 ,就像所有模式一样)。这会留下所有更改的文件“要提交的更改”,正如 git status 所说的那样。