将未提交的更改从当前分支移动到与那些更改冲突的另一个分支 [英] Move uncommitted changes from current branch to another branch that conflicts with those changes
问题描述
假设我在分支master
上,然后开始进行一些更改.
Suppose I am on branch master
and I start making some changes.
我对已经在Emacs中打开的文件进行了更改(因此,在实际情况下,由于检出发生,除非我不断地进行还原缓冲,否则Emacs不会意识到).
I make the changes to a file which is already opened in Emacs (so under the hood, as checkouts happen, Emacs is unaware unless I revert-buffer constantly).
该文件确实存在于分支other_branch
中,该分支随后打算合并到master
中.但是该文件在master
中不存在,直到我不小心将其从Emacs中保存了.
The file did exist in branch other_branch
which was intended to be merged into master
later on. But the file did not exist in master
until I accidentally saved it from Emacs.
更改未提交,但我意识到我不应该在master
上进行更改,而是打算在开始更改之前签出其他分支.
The changes are uncommitted, but I realize that I shouldn't have been making the changes on master
and had intended to checkout a different branch before starting on the changes.
我不想丢失当前工作,但也不想将其提交给master
.
I don't want to lose the current work, but also don't want to commit it to master
.
我尝试使用git stash
,然后使用git checkout other_branch
,但这给我一个错误,说由于未提交的更改,不允许将分支切换到other_branch
:
I have tried using git stash
followed by git checkout other_branch
but this gives me an error saying that, because of the uncommitted changes, I am not allowed to switch branches to other_branch
:
ems@computer:~$ git checkout other_branch
error: The following untracked working tree files would be overwritten by checkout:
some_file
Please move or remove them before you can switch branches.
Aborting
我在master
中开始(偶然地)修改的文件在other_branch
中已经存在,因此仅检出other_branch
会破坏我的工作目录副本,并进行更改,对吗?
The files that I began (accidentally) modifying in master
did already exist in other_branch
, so just checking out other_branch
would destroy my working directory copy, with the changes, correct?
我尝试了stash
,它似乎确实存储了更改,但是当我尝试结帐时,我得到了上面的错误消息.
I tried stash
, which did seem to stash the changes, but then I got the error message above when I tried to checkout.
摘要
-
签出一些新分支,
other_branch
跟踪master
.
在other_branch
中创建一个新文件test.txt
,然后将其打开以在Emacs中进行编辑.提交到other_branch
.
create a new file test.txt
in other_branch
and open it for editing in Emacs. Commit to other_branch
.
回到终端,结帐主机.
回到Emacs,您忘记了自己现在是master用户,开始对test.txt
进行一些更改并保存文件.就master
而言,这是一个全新的文件.
Back in Emacs, you forget you're now on master, start making some changes to test.txt
and save the file. It's a totally new file as far as master
is concerned.
您如何将这些更改转移到other_branch
中?
How do you get those changes over into other_branch
?
推荐答案
对我有用的是:
从master
对未跟踪的,冲突的文件执行git add
.现在已对其进行跟踪,请执行git stash
并检出other_branch
.现在,git stash pop
将尝试将跟踪的存储版本与跟踪的other_branch
版本合并.这可能会导致合并冲突,因此您必须打开文件并解决所有冲突,并确保在other_branch
上分别git add
和commit
进行处理.然后返回到master
并仅验证文件未暂存以进行提交且该分支中不存在这些文件.
From master
do git add
for the untracked, conflicting file. Now that it is tracked, do git stash
and checkout other_branch
. Now, git stash pop
will attempt to merge the tracked-in-stash version with the tracked-in-other_branch
version. This probably will create a merge conflict, so you have to open the file(s) and resolve any conflicts, and be sure to git add
and commit
them on other_branch
. Then go back to master
and just verify that the files are not staged for commit and are not present in that branch.
这使它们可以像在存储库中被跟踪一样被对待,而无需实际从master提交和合并.
This allows them to be treated as if they were tracked within the stash, but without needing to actually commit and merge from master.
这篇关于将未提交的更改从当前分支移动到与那些更改冲突的另一个分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!