GIT: Adding Local Changes to Non-Current Branch

可紊 提交于 2019-12-06 19:27:05

问题


It usually happens to me that I make some local changes, only to discover that I made it on the wrong branch, so I need to switch branch before committing. The problem is that I cannot switch branch when there are local changes. Is there anyway to do that?

Obviously, I can copy the updated files, switch branch, then copy them back, but this doesn't really seem clever!


回答1:


You can switch branches while you have local modifications unless your local changes conflict with the difference between the two branches. In this case you can use the -m or --merge option to checkout to perform the checkout anyway and perform a merge betwee changes and the changes caused by switching branches.

git checkout -m other-branch



回答2:


I use git stash when this happens. It creates a temporary commit of the current state of the working copy (both cached and uncached files) and reverts the working copy to the current HEAD. Then you can switch to the other branch and do git stash pop.




回答3:


As said by other you can use stash or checkout --merge. Those option however will cause a change in the timestamp of some file. If you're working on a large project where compilation can take a long time (our current project take half an hour to compile with distributed builds), this may not be optimal.

In this situation, you can use another repository to move the commit to the correct branch. First, you'll need to clone your current repository (this need to be done only once):

$ git clone /path/to/repository repository.clone
$ cd repository.clone
$ git remote add origin repository.clone
$ git fetch origin

Then in your current repository, you commit your changes:

$ cd /path/to/repository
$ git add path/to/modified/files
$ git commit -m 'Commit message'

On the other repository, you fetch the new commit, and move it to the correct branch:

$ cd ../repository.clone
$ git fetch origin
$ git checkout correct-branch
$ git reset --hard origin/correct-branch
$ git cherry-pick origin/current-branch
$ # resolve conflicts if any, commit with -c option in this case
$ git push origin correct-branch:correct-branch

Then on the original repository, you remove the temporary commit, and remove the associated modification (except if you want to keep them in both branches).

$ cd /path/to/repository
$ git reset HEAD^
$ # edit file and remove modifications moved to other branch

This is more complex and involve history rewriting, but when your project are really large, and compilation time is a limiting factor, it can be great to know the technique. Note that you can reuse the cloned repository, so there is no need to delete / recreate it each time (if compilation time is long, then repository is probably large, and cloning it can take some time).



来源:https://stackoverflow.com/questions/4623952/git-adding-local-changes-to-non-current-branch

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