将更改的文件移动到另一个分支以进行签入
这经常发生在我身上:我编写一些代码,去检查我的更改,然后意识到我不在正确的分支中检查这些更改。但是,如果不恢复更改,我就无法切换到另一个分支。有没有办法将更改移动到另一个分支以在那里签入?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新的答案
不需要使用
stash
命令。未提交的更改不属于任何分支,因此只需使用git checkout -b
原始答案
git stash
是你的朋友。如果您尚未提交,只需运行 git stash 即可。这将保存您的所有更改。
切换到您想要进行更改的分支并运行 git stash pop 。
git stash 有很多用途。这当然是更有用的原因之一。
一个例子:
Updated Answer
No need to use
stash
command. uncommitted changes do not belong to any branch so just usegit checkout -b <new-branch>
Orginal Answer
git stash
is your friend.If you have not made the commit yet, just run
git stash
. This will save away all of your changes.Switch to the branch you want the changes on and run
git stash pop
.There are lots of uses for git stash. This is certainly one of the more useful reasons.
An example:
如果您尚未提交更改,只需使用
git checkout
移动到新分支,然后正常提交它们 - 对文件的更改不会绑定到特定分支直到你提交它们。如果您已经已经提交了更改:
git log
并记住您要移动的提交的 SHA。Cherry-pick 接受给定的提交并将其应用到当前签出的头,从而允许您将提交复制到新分支。
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.If you have already committed your changes:
git log
and remember the SHA of the commit you want to move.git cherry-pick SHA
substituting the SHA from above.git reset HEAD~1
to reset back before your wrong-branch commit.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.可悲的是,这种情况也经常发生在我身上,如果我在
git commit
之前意识到自己的错误,我会使用git stash
,否则使用gitcherry-pick
,这两个命令在其他答案中都得到了很好的解释,我想为 git checkout targetBranch 添加说明:如果 targetBranch 与您的历史记录相同,则此命令只会保留您的工作目录和暂存快照当前分支
@Amber的说法不假,当你移动到newBranch时,
git checkout -b newBranch
,创建一个新指针,它指向与当前分支完全相同的提交。事实上,如果您碰巧有另一个分支与当前分支共享历史记录(两者都指向同一个提交),您可以通过 git checkout targetBranch 来“移动您的更改”
但是,通常不同的分支意味着不同的历史记录,Git 不允许你在这些具有脏工作目录或暂存区域的分支之间切换。在这种情况下,您可以执行
git checkout -f targetBranch
(干净和一次性更改)或git stage
+git checkout targetBranch
(干净和 < strong>保存更改),只需运行git checkout targetBranch
就会给出错误:Sadly this happens to me quite regularly as well and I use
git stash
if I realized my mistake beforegit commit
and usegit cherry-pick
otherwise, both commands are explained pretty well in other answersI 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@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
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) orgit stage
+git checkout targetBranch
(clean and save changes), simply runninggit checkout targetBranch
will give an error:软 git 重置会将提交的更改放回到索引中。接下来,检查您打算提交的分支。然后使用新的提交消息git commit。
git reset --soft
git checkout;
git commit -m “提交消息在此处”
来自 git 文档:
A soft git reset will put committed changes back into your index. Next, checkout the branch you had intended to commit on. Then git commit with a new commit message.
git reset --soft <commit>
git checkout <branch>
git commit -m "Commit message goes here"
From git docs:
如果您创建了新文件,您可以执行以下操作:
If you created new files you can do this:
更现代的答案
由于
checkout
可能是 Git 最不直观的瓷器命令,并且负责太多的功能,因此请使用更容易记住、更直观的switch
(在2019 年作为 Git 的一部分2.23):如果您的分支彼此不同步,第一个
git switch
命令会给您一个如下错误:此时您将不得不使用替代的
git stash
方法:不要忘记将
NEW BRANCH
替换为您要切换到的分支,并将ORIGINAL_BRANCH
替换为您原来所在的分支(通常 <代码>主控或main
)。要查看存储库所有分支的列表,请运行 gitbranch -l 。A more modern answer
Since
checkout
is probably Git's least intuitive porcelain command and responsible for way too much functionality, use the easier-to-remember, more intuitiveswitch
instead (introduced in 2019 as part of Git 2.23):If your branches are not in sync with each other the first
git switch
command will give you an error like this:At this point you will have to use the alternative
git stash
method:Don't forget to replace
NEW BRANCH
with the branch you want to switch to andORIGINAL_BRANCH
with the branch you were originally in (usuallymaster
ormain
). To see a list of all your repository's branches, rungit branch -l
.