[英]How do I merge my local uncommitted changes into another Git branch?
How can I do the following in Git?我如何在 Git 中执行以下操作?
My current branch is branch1 and I have made some local changes.我当前的分支是 branch1,我做了一些本地更改。 However, I now realize that I actually meant to be applying these changes to branch2.但是,我现在意识到我实际上打算将这些更改应用于 branch2。 Is there a way to apply/merge these changes so that they become local changes on branch2 without committing them on branch1?有没有办法应用/合并这些更改,以便它们成为 branch2 上的本地更改而无需在 branch1 上提交它们?
Since your files are not yet committed in branch1
: 由于您的文件尚未在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 commented by benjohn (see git stash
man page ): 由benjohn 评论 (参见git stash
手册页 ):
To also stash currently untracked (newly added) files, add the argument
-u
, so: 要隐藏当前未跟踪(新添加的)文件,请添加参数-u
,以便:
git stash -u
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. 只要您编辑的文件在branch1和branch2之间不同,这将起作用。 It will leave you on branch2 with you working changes preserved. 它会让你在branch2上保留工作变更。 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. 如果它们不同,那么您可以指定要将本地更改与通过将-m
选项切换到checkout所引入的更改进行合并。
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
A shorter alternative to the previously mentioned stash approach would be: 前面提到的藏匿方法的一个较短的替代方案是:
Temporarily move the changes to a stash. 暂时将更改移动到存储区。
git stash
Create and switch to a new branch and then pop the stash to it in just one step. 创建并切换到新分支,然后只需一步即可将存储区弹出。
git stash branch new_branch_name
Then just add
and commit
the changes to this new branch. 然后只需add
更改并将更改commit
到此新分支。
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. 这在我的工作流程中已经足够了,我几乎试图为它编写一个新的git命令。 The usual git stash
flow is the way to go but is a little awkward. 通常的git stash
流程是要走的路, 但有点尴尬。 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 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 commit
-ing我发现的东西(通常是我在工作时发现的属于master的bugfix)在功能分支上)马上。
It is also helpful—if you run into situations like this a lot—to have another working directory alongside your current one that always have the
master
branch checked out. 它也很有用 - 如果你经常遇到这样的情况 - 在你当前的工作目录旁边有一个总是有master
分支签出的工作目录 。
So how I achieve this goes like this: 所以我如何实现这一点是这样的:
git commit
the changes right away with a good commit message. git commit
使用良好的提交消息立即git commit
更改。 git reset HEAD~1
to undo the commit from current branch. git reset HEAD~1
撤消当前分支的提交。 Sometimes later (asynchronously), or immediately in another terminal window: 有时候稍后(异步),或者立即在另一个终端窗口中:
cd my-project-master
which is another WD sharing the same .git
cd my-project-master
这是另一个WD共享相同的.git
git reflog
to find the bugfix I've just made. git reflog
找到我刚刚创建的bugfix。 git cherry-pick SHA1
of the commit. 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时:
cd my-project
which is the main WD I'm working on. cd my-project
这是我正在研究的主要WD。 git rebase master
to get the bugfixes. 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 master
instead of hidden in my feature branch. 通过这种方式,我可以不间断地继续处理该功能,而不必担心git stash
任何东西或者必须在git checkout
之前清理我的WD(然后再次检查功能分支退出。)并且仍然拥有我的所有错误修正转到master
而不是隐藏在我的功能分支中。
IMO git stash
and git checkout
is a real PIA when you are in the middle of working on some big feature. 当您处理一些重要功能时,IMO git stash
和git checkout
是一个真正的PIA。
如果它是关于承诺的更改,你应该看看git-rebase,但正如VonC在评论中指出的那样,当你谈论本地更改时,git-stash肯定是这样做的好方法。
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. 该链接指向我自己的网站。
You have uncommited changes on my_branch
that you want to commit to master
, without committing all the changes from my_branch
. 您在my_branch
上有未提交的更改要提交给master
,而不提交my_branch
所有更改。
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
Start by merging master
into your branch, since you'll have to do that eventually anyway, and now is the best time to resolve any conflicts. 首先将master
合并到您的分支中,因为无论如何您最终都必须这样做,现在是解决任何冲突的最佳时机。
The -u
option (aka --include-untracked
) in git stash -u
prevents you from losing untracked files when you later do git clean -f -d
within master
. git stash -u
的-u
选项(又名--include-untracked
)可以防止你以后在master
执行git clean -f -d
时丢失未跟踪的文件。
After git checkout master
it is important that you do NOT git stash pop
, because you will need this stash later. 在git checkout master
,重要的是你不要git stash pop
,因为你以后需要这个存储。 If you pop the stash created in my_branch
and then do git stash
in master
, you will cause needless merge conflicts when you later apply that stash in my_branch
. 如果你弹出在my_branch
创建的存储,然后在master
执行git stash
,当你稍后在my_branch
应用该存储时,将导致不必要的合并冲突。
git reset
unstages everything resulting from git stash apply
. git reset
unstages由git stash apply
产生的一切。 For example, files that have been modified in the stash but do not exist in master
get staged as "deleted by us" conflicts. 例如,已经在存储中修改但在master
不存在的文件将被暂存为“由我们删除”冲突。
git checkout .
and git clean -f -d
discard everything that isn't committed: all changes to tracked files, and all untracked files and directories. 和git clean -f -d
丢弃未提交的所有内容:对跟踪文件的所有更改以及所有未跟踪的文件和目录。 They are already saved in the stash and if left in master
would cause needless merge conflicts when switching back to my_branch
. 它们已经保存在存储中,如果留在master
,在切换回my_branch
时会导致不必要的合并冲突。
The last git stash pop
will be based on the original my_branch
, and so will not cause any merge conflicts. 最后一个git stash pop
将基于原始的my_branch
,因此不会导致任何合并冲突。 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". 但是,如果您的存储包含您已提交掌握的未跟踪文件,git会抱怨它“无法从存储中恢复未跟踪的文件”。 To resolve this conflict, delete those files from your working tree, then git stash pop
, git add .
要解决此冲突,请从工作树中删除这些文件,然后git stash pop
, git add .
, and git reset
. ,和git reset
。
Create a new branch
首先, Create a new branch
add your changes
其次, add your changes
commit to your changes with a beautiful message
最后, commit to your changes with a beautiful message
git checkout -b branch_name
git add file_one_name file_two_name file_three_name
git commit -m commit_message
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:如果“git branch”显示master,并且您想创建+移动到另一个分支:
Check branch again using "git branch" It should now show that you are in the new branch.使用“git 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.我希望它可以帮助遇到类似情况的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.