本文翻译自: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 我一直在做一个项目,但不幸的是,我忘了切换到我的分支,因此一直在做主人
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 )而不需要掌握?
#1楼
参考:https://stackoom.com/question/P1XS/Git-在错误的分支上工作-如何将更改复制到现有主题分支
#2楼
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在对已接受答案的评论中实际指出了这一点,但我第一次看到它时并不明白他在说什么。
#3楼
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工具。
- checkout and make changes to files in master (ups!, wrong branch) 签出并对master中的文件进行更改(ups!,错误分支)
- create a new branch "temp" (or any unused name you choose) from master. 从master创建一个新的分支“temp”(或您选择的任何未使用的名称)。 Checked out files will now be checked out in temp and not in master. 现在,签出的文件将在temp中检出,而不是在master中检出。
- check in changes to temp (master is untouched) 检查temp的更改(master未触及)
- Everything is now checked in and it is possible to check out an existing branch. 现在一切都已签入,可以查看现有分支。 Check out the wanted branch (the branch I wanted to make the changes to begin with) 3.5 Git Rebase 查看想要的分支(我想要进行更改的分支)3.5 Git Rebase
- merge temp to the wanted branch. 将temp合并到有用的分支。 Now the changes are in the correct branch. 现在更改位于正确的分支中。
- delete the temp branch as it is not needed any more 删除临时分支,因为不再需要它
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
#4楼
git stash
is what you need. git stash
是你需要的。
a full explanation can be found in Git-Tools-Stashing 可以在Git-Tools-Stashing中找到完整的解释
#5楼
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. 然后你应该回到你自己的分支而不接触主分支。