如何将本地未提交的更改合并到另一个 Git 分支中?
我如何在 Git 中执行以下操作?
我当前的分支是branch1,我做了一些本地更改。 然而,我现在意识到我实际上打算将这些更改应用到branch2。 有没有办法应用/合并这些更改,以便它们成为branch2上的本地更改,而无需在branch1上提交它们?
How can I do the following in 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果是关于已提交的更改,您应该看看 git-rebase,但正如 VonC 的评论中指出的那样,当您谈论本地更改时,git-stash 肯定是执行此操作的好方法。
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分支”显示master,并且您想创建+移动到另一个分支:
使用“git分支”再次检查分支
现在应该显示您位于新分支中。
现在添加、提交并推送:
当我在移动到新的本地分支之前进行更改或在移动到新分支之后进行更改时,上述步骤对我来说都适用。
我希望它可以帮助遇到类似情况的人们。
These are the steps I follow:
You can check the status and which branch you are on using:
Note: Here if you make changes in your local repo before moving to the new branch, the following steps should still work.
If "git branch" shows master, and you want to create+move to another branch:
Check branch again using "git branch"
It should now show that you are in the new branch.
Now add, commit and push:
The above steps work for me in both the situation when I have made changes before moving to the new local branch or making changes after moving to the new branch.
I hope it helps people running into similar situations.
如果未提交的更改是未跟踪和跟踪更改的混合
什么是未跟踪更改?
当您创建例如新文件时。 例如,VSCode 在文件资源管理器中的文件旁边显示一个
U
。什么是跟踪变更?
当您对之前提交到存储库的文件进行更改时(在之前的提交中)。
过程的快速解释
假设您位于分支
A
上,但您只想将对现有文件的更改提交到分支A
,而新创建的文件(未跟踪)应该是提交到新分支B
。 可以通过一些技巧来使用隐藏,逐步解释。将脚本添加到
.git/config
在
.git
文件夹内有一个config
文件。 打开它,您将看到类似这样的内容:将配置文件更改为:
现在,您将能够在分支 A 上使用以下命令。
您将看到新文件消失了,如果您正在使用像 VSCode 这样的编辑器(它现在已隐藏),
当仍在分支 A 阶段并将更改提交到现有文件时:
下一步是创建一个新的分支 B(使用
checkout -b
您访问立即)当使用
stash pop
时,隐藏的更改将添加到您当前的分支中。剩下的唯一事情就是在新分支 B 上暂存并提交更改
If the uncommitted changes are a mix of untracked and tracked changes
What are untracked changes?
When you created e.g a new file. VSCode e.g displays a
U
next to the file in the file explorer.What are tracked changes?
When you make changes to files that were committed to the repo earlier (in previous commits).
Quick explanation of procedure
So imagine you are on branch
A
, but you want to commit only changes to existing files to branchA
, while the newly created file (untracked) should be committed to a new branchB
. It's possible to use stashing with a few tricks, explained step by step.Add script to
.git/config
Inside the
.git
folder there is aconfig
file. Open it up and you will see something like this:change the config file to:
Now you will be able to use the following command while you are on branch A.
You will see that the new file disappeared, if you are using a editor like VSCode (it's now stashed)
While still on branch A stage and commit the changes to the existing files:
Next step is creating a new branch B (with
checkout -b
you visit it immediately)When using
stash pop
the stashed changes get added to your current branch.The only thing left is to stage and commit the changes on the new branch B
我发现这个答案很有用。
然而,由于该线程已关闭并且无法发表评论,我对该答案有疑问。
当我应用 git checkout other_branch 时,我收到以下错误
因此,我没有应用该命令,而是使用它解决了我的问题
I have found this answer useful.
However, as that thread is closed and is unable to make comments I have an issue with that answer.
When I applied
git checkout other_branch
I got the following errorSo instead of applying the command, I rather use which fixed my problem
警告:不适合 git 新手。
与 chakrit 的回答类似,我经常遇到这种情况:在功能分支上工作,我发现一个错误并想要修理它。 但修复属于
main
分支,而不是my-feature
。 将更改更改为main
的整个序列是 7 个或更多git
命令,这确实非常烦人且容易出错,因为我找不到执行此操作的脚本,我自己写的。 只需将其放在
$PATH
中的某个位置(例如/usr/local/bin
或/$HOME/.local/bin
或其他内容),然后您可以执行以下操作:用法
然后它将打印一些进度消息:
脚本
这是文件
git-commit-branch
的源代码。 将其放入$PATH
后,不要忘记执行chmod +x
。 该脚本也在 github 上: https://github.com/fritzw/git-utils 。 请随意提出改进建议。它的工作方式如下:
如果任何命令失败,它只会在那里停止并打印一些信息以帮助您恢复情况。 如果您想了解更多详细信息,请查看脚本末尾的注释和 git 命令序列。
WARNING: Not for git newbies.
Similar to chakrit's answer, I run into this situation quite often: Working on a feature branch, I find a bug and want to fix it. But the fix belongs on the
main
branch, notmy-feature
. The whole sequence to get the changes intomain
is 7 or moregit
commands, which is really quite annoying and easy to get wrongSince I could not find a script to do it, I wrote it myself. Simply place it somewhere in
$PATH
(e.g./usr/local/bin
or/$HOME/.local/bin
or something), then you can do the following:Usage
It will then print some progress messages:
The script
Here's the source code for the file
git-commit-branch
. Don't forget to dochmod +x
after placing it in$PATH
. The script is also on github: https://github.com/fritzw/git-utils. Feel free to suggest improvements.The way it works is as follows:
If any command fails it will simply stop there and print some info to help you recover the situation. If you want more details, look at the comments and the sequence of git commands at the end of the script.
由于您的文件尚未在
branch1
中提交:或
以上是 rbento 的答案:
它使用:
正如评论的作者:benjohn(请参阅
git stash
手册页):Since your files are not yet committed in
branch1
:or
Above is the longer more explicit version of rbento's answer:
It uses:
As commented by benjohn (see
git stash
man page):隐藏、临时提交和变基可能都有些过头了。 如果您尚未将更改的文件添加到索引中,那么您也许可以只签出另一个分支。
只要您正在编辑的文件在branch1 和branch2 之间不存在不同,此操作就有效。 它将让您留在branch2,并保留您的工作更改。 如果它们不同,那么您可以指定要将本地更改与通过使用
-m
选项切换分支来签出而引入的更改合并。如果您已对索引添加了更改,那么您需要首先通过重置来撤消这些更改。 (这将保留您的工作副本,只会删除暂存的更改。)
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.
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
-m
option to checkout.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 存储
git stashbranch new_branch_name
然后只需
添加
并提交
对此新分支的更改。A shorter alternative to the accepted answer would be:
git stash
git stash branch new_branch_name
Then just
add
andcommit
the changes to this new branch.警告:不适合 git 新手。
这在我的工作流程中出现得足够多,以至于我几乎尝试为其编写一个新的 git 命令。 通常的 git stash 流程是可行的方法,但是有点尴尬。 我通常会先进行新的提交,因为如果我一直在查看更改,所有信息都在我的脑海中记忆犹新,并且最好开始
git commit
- 执行什么我立即发现了(通常是我在处理功能分支时发现的属于 master 的错误修复)。因此,我如何实现这一点是这样的:
git commit
立即进行更改并提供良好的提交消息。git reset HEAD~1
撤消当前分支的提交。有时稍后(异步),或立即在另一个终端窗口中:
cd my-project-master
这是另一个共享相同.git
git reflog
的 WD > 找到我刚刚修复的错误。gitcherry-pick SHA1
。或者(仍然是异步),您可以重新设置(或合并)您的功能分支来修复错误,通常是在您准备提交 PR 并已经清理了您的功能分支和 WD 时:
cd my-project
这是我正在研究的主要 WD。git rebase master
来修复错误。这样我就可以不间断地继续开发该功能,而不必担心
git stash
-ing 任何东西,或者必须在git checkout
之前清理我的 WD(然后让再次检查功能分支退出。)并且我的所有错误修复仍然会转到master
而不是隐藏在我的功能分支中。在我看来,当您正在开发某些重要功能时,
git stash
和git checkout
是真正的 PIA。WARNING: Not for git newbies.
This comes up enough in my workflow that I've almost tried to write a new git command for it. The usual
git stash
flow is the way to go but is 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 mind and it's better to just startgit commit
-ing what I found (usually a bugfix belonging on master that I discover while working on a feature branch) right away.So how I achieve this goes like this:
git commit
the changes right away with a good commit message.git reset HEAD~1
to undo the commit from current branch.Sometimes later (asynchronously), or immediately in another terminal window:
cd my-project-master
which is another WD sharing the same.git
git reflog
to find the bugfix I've just made.git cherry-pick SHA1
of the commit.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:
cd my-project
which is the main WD I'm working on.git rebase master
to get the bugfixes.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 agit checkout
(and then having the check the feature branch backout again.) and still have all my bugfixes goes tomaster
instead of hidden in my feature branch.IMO
git stash
andgit checkout
is a real PIA when you are in the middle of working on some big feature.