如何将本地未提交的更改合并到另一个 Git 分支?

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

How do I merge my local uncommitted changes into another Git branch?

gitbranch

提问by solsberg

How can I do the following in Git?

我如何在 Git 中执行以下操作?

My current branch is branch1 and I have made some local changes. However, I now realize that I actually meant to be applying these changes to branch2. Is there a way to apply/merge these changes so that they become local changes on branch2 without committing them on branch1?

我当前的分支是 branch1,我做了一些本地更改。但是,我现在意识到我实际上打算将这些更改应用于 branch2。有没有办法应用/合并这些更改,以便它们成为 branch2 上的本地更改而无需在 branch1 上提交它们?

回答by VonC

Since your files are not yet committed in branch1:

由于您的文件尚未提交branch1

git stash
git checkout branch2
git stash pop

or

或者

git stash
git checkout branch2
git stash list       # to check the various stash made in different branch
git stash apply x    # to select the right one


As commentedby benjohn(see git stashman page):

正如benjohn评论的(参见git stash手册页):

To also stash currently untracked (newly added) files, add the argument -u, so:

要存储当前未跟踪(新添加)的文件,请添加参数-u,因此:

git stash -u

回答by CB Bailey

Stashing, temporary commits and rebasing may all be overkill. If you haven't added the changed files to the index, yet, then you may be able to just checkout the other branch.

存储、临时提交和变基可能都有些矫枉过正。如果您还没有将更改的文件添加到索引中,那么您可以只检出另一个分支。

git checkout branch2

This will work so long as no files that you are editing are different between branch1 and branch2. It will leave you on branch2 with you working changes preserved. If they are different then you can specify that you want to merge your local changes with the changes introduced by switching branches with the -moption to checkout.

只要您正在编辑的文件在 branch1 和 branch2 之间没有不同,这就会起作用。它会将您留在 branch2 上,并保留您的工作更改。如果它们不同,那么您可以指定要将本地更改与切换分支引入的更改合并,并-m选择检出。

git checkout -m branch2

If you've added changes to the index then you'll want to undo these changes with a reset first. (This will preserve your working copy, it will just remove the staged changes.)

如果您对索引添加了更改,那么您需要先通过重置来撤消这些更改。(这将保留您的工作副本,它只会删除分阶段的更改。)

git reset

回答by rbento

A shorter alternative to the previously mentioned stash approach would be:

前面提到的 stash 方法的一个更短的替代方法是:

Temporarily move the changes to a stash.

暂时将更改移至 stash。

  1. git stash
  1. git stash

Create and switch to a new branch and then pop the stash to it in just one step.

创建并切换到一个新分支,然后只需一步即可将 stash 弹出到它。

  1. git stash branch new_branch_name
  1. git stash branch new_branch_name

Then just addand committhe changes to this new branch.

然后只是addcommit这个新分支的变化。

回答by chakrit

WARNING:Not for git newbies.

警告:不适用于 git 新手。

This comes up enough in my workflow that I've almost tried to write a new git command for it. The usual git stashflow is the way to go butis a little awkward. I usually make a new commit first since if I have been looking at the changes, all the information is fresh in my mindand it's better to just start git commit-ing what I found (usually a bugfix belonging on master that I discover while working on a feature branch) right away.

这在我的工作流程中出现得足够多,我几乎试图为它编写一个新的 git 命令。通常的git stash流程是要走的路,有点尴尬。我通常首先进行新的提交,因为如果我一直在查看更改,所有信息在我的脑海中都是新鲜的,最好开始 -git commit我发现的内容(通常是我在开发时发现的属于 master 的错误修复)功能分支)马上。

It is also helpful—if you run into situations like this a lot—to have another working directoryalongside your current one that always have the masterbranch checked out.

如果您经常遇到这样的情况,那么在您当前的目录旁边有另一个工作目录也很有帮助,该目录始终master检出分支。

So how I achieve this goes like this:

所以我如何实现这一点是这样的:

  1. git committhe changes right away with a good commit message.
  2. git reset HEAD~1to undo the commit from current branch.
  3. (optional) continue working on the feature.
  1. git commit立即通过良好的提交消息进行更改。
  2. git reset HEAD~1从当前分支撤消提交。
  3. (可选)继续研究该功能。

Sometimes later (asynchronously), or immediately in another terminal window:

有时稍后(异步),或立即在另一个终端窗口中:

  1. cd my-project-masterwhich is another WD sharing the same .git
  2. git reflogto find the bugfix I've just made.
  3. git cherry-pick SHA1of the commit.
  1. cd my-project-master这是另一个共享相同的WD .git
  2. git reflog找到我刚刚制作的错误修正。
  3. git cherry-pick SHA1的提交。

Optionally (still asynchronous) you can then rebase (or merge) your feature branch to get the bugfix, usually when you are about to submit a PR and have cleaned your feature branch and WD already:

可选地(仍然是异步的),您可以重新设置(或合并)您的功能分支以修复错误,通常是在您即将提交 PR 并且已经清理您的功能分支和 WD 时:

  1. cd my-projectwhich is the main WD I'm working on.
  2. git rebase masterto get the bugfixes.
  1. cd my-project这是我正在处理的主要 WD。
  2. git rebase master得到错误修正。

This way I can keep working on the feature uninterrupted and not have to worry about git stash-ing anything or having to clean my WD before a git checkout(and then having the check the feature branch backout again.) and still have all my bugfixes goes to masterinstead of hidden in my feature branch.

通过这种方式,我可以不间断地继续处理该功能,而不必担心git stash-ing 任何事情或必须在 a 之前清理我的 WD git checkout(然后再次检查功能分支退出。)并且仍然有我所有的错误修正去master而不是隐藏在我的功能分支中。

IMO git stashand git checkoutis a real PIA when you are in the middle of working on some big feature.

IMOgit stashgit checkout是一个真正的PIA,当你在一些大的功能工作的中间。

回答by claf

If it were about committed changes, you should have a look at git-rebase, but as pointed out in comment by VonC, as you're talking about local changes, git-stash would certainly be the good way to do this.

如果是关于已提交的更改,您应该查看 git-rebase,但正如 VonC 在评论中指出的那样,当您谈论本地更改时,git-stash 肯定是执行此操作的好方法。

回答by Vladimir Kornea

The answers given so far are not ideal because they require a lot of needless work resolving merge conflicts, or they make too many assumptions which are frequently false. This is how to do it perfectly. The link is to my own site.

到目前为止给出的答案并不理想,因为它们需要大量不必要的工作来解决合并冲突,或者它们做出了太多经常错误的假设。这就是如何完美地做到这一点。链接是我自己的网站。

How to Commit to a Different Branch in git

如何在 git 中提交到不同的分支

You have uncommited changes on my_branchthat you want to commit to master, without committing all the changes from my_branch.

您有未提交的更改my_branch要承诺master,不作全部来自变化my_branch

Example

例子

git merge master
git stash -u
git checkout master
git stash apply
git reset
git add example.js
git commit
git checkout .
git clean -f -d
git checkout my_branch
git merge master
git stash pop

Explanation

解释

Start by merging masterinto your branch, since you'll have to do that eventually anyway, and now is the best time to resolve any conflicts.

从合并master到您的分支开始,因为无论如何您最终都必须这样做,现在是解决任何冲突的最佳时机。

The -uoption (aka --include-untracked) in git stash -uprevents you from losing untracked files when you later do git clean -f -dwithin master.

中的-u选项(又名--include-untrackedgit stash -u可防止您以后git clean -f -dmaster.

After git checkout masterit is important that you do NOT git stash pop, because you will need this stash later. If you pop the stash created in my_branchand then do git stashin master, you will cause needless merge conflicts when you later apply that stash in my_branch.

之后git checkout master重要的是你不要git stash pop,因为你以后会需要这个藏匿处。如果您弹出在 in 中创建的 stashmy_branch然后git stash在 in 中执行master,当您稍后在 中应用该 stash 时,您将导致不必要的合并冲突my_branch

git resetunstages everything resulting from git stash apply. For example, files that have been modified in the stash but do not exist in masterget staged as "deleted by us" conflicts.

git reset取消由git stash apply. 例如,已在 stash 中修改但不存在的文件master被暂存为“由我们删除”冲突。

git checkout .and git clean -f -ddiscard everything that isn't committed: all changes to tracked files, and all untracked files and directories. They are already saved in the stash and if left in masterwould cause needless merge conflicts when switching back to my_branch.

git checkout .git clean -f -d丢弃所有未提交的内容:对跟踪文件的所有更改,以及所有未跟踪的文件和目录。它们已经保存在 stash 中,如果留在里面master会在切换回my_branch.

The last git stash popwill be based on the original my_branch, and so will not cause any merge conflicts. However, if your stash contains untracked files which you have committed to master, git will complain that it "Could not restore untracked files from stash". To resolve this conflict, delete those files from your working tree, then git stash pop, git add ., and git reset.

最后一个git stash pop将基于原始my_branch,因此不会导致任何合并冲突。但是,如果您的 stash 包含您已提交给 master 的未跟踪文件,git 会抱怨它“无法从 stash 恢复未跟踪的文件”。为了解决这个矛盾,从你的工作树,然后删除这些文件git stash popgit add .git reset