[英]Git - working on wrong branch - how to copy changes to existing topic branch
I've been working on a project, but unfortunately, I forgot to switch to my branch, and as such have been working on master我一直在做一个项目,但不幸的是,我忘记切换到我的分支,因此一直在做 master
How can I copy the work (3 files) I've done here from master, to my branch (called, for example branch123 ) without comitting to master?如何将我在这里完成的工作(3 个文件)从 master 复制到我的分支(例如称为branch123 )而不提交到 master?
Sounds like all you need is the following: 听起来你需要的是以下内容:
git stash
git checkout branch123
git stash apply
Then you should be back on your own branch without touching the master branch. 然后你应该回到你自己的分支而不接触主分支。
This is an old question but it's at the top of Google for "working on the wrong branch". 这是一个古老的问题,但它是谷歌“在错误的分支上工作”的顶级问题。 I think a better answer is to simply checkout the branch you wanted to be working on.
我认为更好的答案是简单地检查您想要处理的分支机构。 No need to stash anything:
不需要藏匿任何东西:
git checkout branch123
The default behavior of checkout
is to NOT overwrite modified files in your working directory, so you won't lose anything. checkout
的默认行为是不覆盖工作目录中的已修改文件,因此您不会丢失任何内容。 Based on what I found here . 基于我在这里找到的东西 。
Edit: This method will only work if the files you have modified in the working directory are identical in both master
and branch123
. 编辑:只有在工作目录中修改的文件在
master
和branch123
都相同时,此方法才有效。 This was actually pointed out by Jefromi in the comments to the accepted answer, but I didn't understand what he was saying the first time I read it. Jefromi在对已接受答案的评论中实际指出了这一点,但我第一次看到它时并不明白他在说什么。
git stash
is what you need. git stash
是你需要的。
a full explanation can be found in Git-Tools-Stashing 可以在Git-Tools-Stashing中找到完整的解释
As it is possible to create a new branch but not possible to checkout an existing branch while having files checked out, I found the following trick using a temporary branch to work: 由于可以创建一个新分支,但在签出文件时无法检出现有分支,我发现以下技巧使用临时分支工作:
This scenario works at least with VS 2015 Git plugin but would most likely work with any git tool. 这种情况至少适用于VS 2015 Git插件,但很可能适用于任何git工具。
EDIT: I found out that you will have to perform a rebase (git rebase --onto) of the temp branch before performing the merge. 编辑:我发现你必须在执行合并之前执行temp分支的rebase(git rebase --onto)。 Otherwise the changes in master will be included in the merge.
否则,master中的更改将包含在合并中。 An extra step 3.5 above.
上面的额外步骤3.5。 See further about rebase here: https://git-scm.com/book/en/v2/Git-Branching-Rebasing
有关rebase的更多信息,请访问: https : //git-scm.com/book/en/v2/Git-Branching-Rebasing
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.