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!
显然,我可以复制更新的文件,切换分支,然后将它们复制回来,但这看起来并不聪明!
3 个解决方案
#1
16
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.
除非本地更改与两个分支之间的差异发生冲突,否则您可以在进行本地修改时切换分支。在这种情况下,您可以使用-m或--merge选项结帐以执行结帐,并在更改和切换分支引起的更改之间执行合并。
git checkout -m other-branch
#2
9
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
.
发生这种情况时我会使用git stash。它创建工作副本的当前状态(缓存和未缓存文件)的临时提交,并将工作副本还原为当前HEAD。然后你可以切换到另一个分支并执行git stash pop。
#3
2
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.
正如其他人所说,你可以使用stash或checkout --merge。但是,这些选项会导致某些文件的时间戳发生变化。如果您正在处理一个大型项目,其中编译可能需要很长时间(我们当前的项目需要半小时才能使用分布式构建进行编译),这可能不是最佳的。
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).
这更复杂并且涉及历史重写,但是当您的项目非常庞大并且编译时间是一个限制因素时,了解该技术可能会很棒。请注意,您可以重用克隆的存储库,因此无需每次都删除/重新创建它(如果编译时间很长,那么存储库可能很大,克隆它可能需要一些时间)。
#1
16
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.
除非本地更改与两个分支之间的差异发生冲突,否则您可以在进行本地修改时切换分支。在这种情况下,您可以使用-m或--merge选项结帐以执行结帐,并在更改和切换分支引起的更改之间执行合并。
git checkout -m other-branch
#2
9
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
.
发生这种情况时我会使用git stash。它创建工作副本的当前状态(缓存和未缓存文件)的临时提交,并将工作副本还原为当前HEAD。然后你可以切换到另一个分支并执行git stash pop。
#3
2
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.
正如其他人所说,你可以使用stash或checkout --merge。但是,这些选项会导致某些文件的时间戳发生变化。如果您正在处理一个大型项目,其中编译可能需要很长时间(我们当前的项目需要半小时才能使用分布式构建进行编译),这可能不是最佳的。
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).
这更复杂并且涉及历史重写,但是当您的项目非常庞大并且编译时间是一个限制因素时,了解该技术可能会很棒。请注意,您可以重用克隆的存储库,因此无需每次都删除/重新创建它(如果编译时间很长,那么存储库可能很大,克隆它可能需要一些时间)。