Suppose I am on branch master
and I start making some changes.
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).
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.
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.
I don't want to lose the current work, but also don't want to commit it to 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
:
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?
I tried stash
, which did seem to stash the changes, but then I got the error message above when I tried to checkout.
Summary
checkout some new branch,
other_branch
trackingmaster
.create a new file
test.txt
inother_branch
and open it for editing in Emacs. Commit toother_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. It's a totally new file as far asmaster
is concerned.
How do you get those changes over into other_branch
?
Temporarily rename the file ?
$ mv test.txt test.txt.tmp
$ git checkout other_branch
$ mv test.txt.tmp test.txt
Inspect changes & commit
If the untracked files are an issue during the checkout, maybe you can try:
git stash --include-untracked
From the git stash
man page:
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
Since that doesn't work either (on the stash pop), you could imply:
- Make the commmit (in master branch)
- checkout develop and cherry-pick that commmit
- checkout to master and reset it to
HEAD~
What worked for me was the following:
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.
This allows them to be treated as if they were tracked within the stash, but without needing to actually commit and merge from master.
来源:https://stackoverflow.com/questions/23022702/move-uncommitted-changes-from-current-branch-to-another-branch-that-conflicts-wi