[英]Move uncommitted changes from current branch to another branch that conflicts with those changes
Suppose I am on branch master
and I start making some changes. 假设我在分支
master
并且开始进行一些更改。
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). 我对已经在Emacs中打开的文件进行了更改(因此,在实际操作中,随着结帐的发生,除非我不断还原缓冲区,否则Emacs不会意识到)。
The file did exist in branch other_branch
which was intended to be merged into master
later on. 该文件确实存在于分支
other_branch
,该分支other_branch
打算合并到master
中。 But the file did not exist in master
until I accidentally saved it from Emacs. 但是,直到我不小心从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
. 我不想丢失当前的工作,但也不想将其提交给
master
。
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
: 我已经尝试过使用
git stash
然后使用git checkout other_branch
但这给我一个错误,说由于未提交的更改,不允许将分支切换到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
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? 我开始(偶然地)在
master
进行修改的文件已经存在于other_branch
,因此仅检出other_branch
会破坏我的工作目录副本,并进行更改,对吗?
I tried stash
, which did seem to stash the changes, but then I got the error message above when I tried to checkout. 我尝试了
stash
,它似乎确实隐藏了更改,但是当我尝试结帐时,出现了以上错误消息。
Summary 摘要
checkout some new branch, other_branch
tracking master
. 结帐一些新分支,
other_branch
跟踪master
。
create a new file test.txt
in other_branch
and open it for editing in Emacs. 在
other_branch
创建一个新文件test.txt
,然后将其打开以在Emacs中进行编辑。 Commit to other_branch
. 提交到
other_branch
。
back in the terminal, checkout master. 回到终端,结帐主人。
Back in Emacs, you forget you're now on master, start making some changes to test.txt
and save the file. 回到Emacs,您会忘记自己现在已经掌握了,开始对
test.txt
进行一些更改并保存文件。 It's a totally new file as far as master
is concerned. 就
master
而言,这是一个全新的文件。
How do you get those changes over into other_branch
? 您如何将这些更改转移到
other_branch
?
If the untracked files are an issue during the checkout, maybe you can try: 如果在结帐期间未跟踪的文件是一个问题,也许您可以尝试:
git stash --include-untracked
From the git stash
man page : 从
git stash
手册页 :
If the
--include-untracked
option is used, all untracked files are also stashed and then cleaned up withgit clean
, leaving the working directory in a very clean state如果使用了
--include-untracked
选项,则所有未跟踪的文件也会被保存,然后使用git clean
,使工作目录处于非常干净的状态
Since that doesn't work either (on the stash pop), you could imply: 由于这也不起作用(在隐藏弹出窗口中),您可能暗示:
HEAD~
HEAD~
Temporarily rename the file ? 临时重命名文件?
$ mv test.txt test.txt.tmp
$ git checkout other_branch
$ mv test.txt.tmp test.txt
Inspect changes & commit 检查变更并提交
What worked for me was the following: 对我有用的是:
From master
do git add
for the untracked, conflicting file. 从
master
做git add
为未跟踪的冲突文件git add
文件。 Now that it is tracked, do git stash
and checkout other_branch
. 现在已经跟踪了,执行
git stash
并检出other_branch
。 Now, git stash pop
will attempt to merge the tracked-in-stash version with the tracked-in- other_branch
version. 现在,
git stash pop
将尝试将other_branch
-in- other_branch
版本与other_branch
-in- other_branch
版本other_branch
。 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
. 这可能会导致合并冲突,因此您必须打开文件并解决所有冲突,并确保在
other_branch
上git add
并commit
它们。 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. 这样,就可以将它们视为已在存储中进行了跟踪,而无需实际从master提交和合并。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.