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

我怕爱的太早我们不能终老 提交于 2019-12-06 06:06:15

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

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.

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