Git - 在错误的分支上工作 - 如何将更改复制到现有主题分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5964118/
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 10:53:20  来源:igfitidea点击:

Git - working on wrong branch - how to copy changes to existing topic branch

git

提问by Alex

I've been working on a project, but unfortunately, I forgotto 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?

回答by gnab

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.

然后你应该回到你自己的分支上,而不要触及主分支。

回答by Russel Dirks

The accepted answer is the most thorough, but there is a special case where you can simplify. If the files you have modified in the working directory are identical in both masterand branch123you can simply do

接受的答案是最彻底的,但有一种特殊情况可以简化。如果您在工作目录中修改的文件在两者中都相同masterbranch123您可以简单地执行

git checkout branch123

No need to stash anything, since the default behavior of checkoutis to NOT overwrite modified files in your working directory, so you won't lose anything. (This was actually mentioned in the comments first by Cascabel)

无需存储任何内容,因为 的默认行为checkout是不覆盖工作目录中的已修改文件,因此您不会丢失任何内容。(这实际上是 Cascabel 在评论中首先提到的)

As other people have mentioned in the comments, if branch123doesn't exist yet, you can do

正如其他人在评论中提到的,如果branch123还不存在,你可以做

git checkout -b branch123

Based on what I found here.

根据我在这里找到的内容。

回答by Roee Gavirel

git stashis what you need.

git stash是你所需要的。

a full explanation can be found in Git-Tools-Stashing

完整的解释可以在Git-Tools-Stashing 中找到

回答by Pasi

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 工具。

  1. checkout and make changes to files in master (ups!, wrong branch)
  2. create a new branch "temp" (or any unused name you choose) from master. Checked out files will now be checked out in temp and not in master.
  3. check in changes to temp (master is untouched)
  4. 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
  5. merge temp to the wanted branch. Now the changes are in the correct branch.
  6. delete the temp branch as it is not needed any more
  1. 结帐并更改 master 中的文件(ups!,错误的分支)
  2. 从 master 创建一个新的分支“temp”(或您选择的任何未使用的名称)。检出的文件现在将在 temp 而不是 master 中检出。
  3. 签入对 temp 的更改(master 未受影响)
  4. 现在一切都已签入,并且可以签出现有分支。查看想要的分支(我想要进行更改的分支) 3.5 Git Rebase
  5. 将 temp 合并到想要的分支。现在更改位于正确的分支中。
  6. 删除临时分支,因为不再需要它

EDIT: I found out that you will have to perform a rebase (git rebase --onto) of the temp branch before performing the merge. Otherwise the changes in master will be included in the merge. An extra step 3.5 above. See further about rebase here: https://git-scm.com/book/en/v2/Git-Branching-Rebasing

编辑:我发现您必须在执行合并之前执行临时分支的变基(git rebase --onto)。否则 master 中的更改将包含在合并中。上面的额外步骤 3.5。在此处查看有关 rebase 的更多信息:https: //git-scm.com/book/en/v2/Git-Branching-Rebasing