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

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

问题描述:

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

How can I do the following in Git?

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

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 中提交:

Since your files are not yet committed in branch1:

git stash
git checkout branch2
git stash pop

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






评论,由 benjohn (请参见 git隐藏手册页):


As commented by benjohn (see git stash man page):


也可以隐藏currentl y个未跟踪(新添加)的文件,添加参数 -u ,因此:



git stash -u