Move uncommitted changes from current branch to another branch that conflicts with those changes

a 夏天 提交于 2019-12-10 10:48:16

问题


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

  1. checkout some new branch, other_branch tracking master.

  2. create a new file test.txt in other_branch and open it for editing in Emacs. Commit to other_branch.

  3. back in the terminal, checkout master.

  4. 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.

How do you get those changes over into other_branch?


回答1:


Temporarily rename the file ?

$ mv test.txt test.txt.tmp
$ git checkout other_branch
$ mv test.txt.tmp test.txt

Inspect changes & commit




回答2:


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 with git 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~



回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!