是否需要在`git checkout`之前运行`git stash push`? [英] Is it necessary to run `git stash push` before `git checkout`
问题描述
git checkout
将工作树目录中未提交的更改保留下来.那么是否有必要在git checkout
之前运行git stash push
(然后在git checkout
之后的某个稍后时间运行git stash pop
)?或者什么时候有必要,什么时候没有?
git checkout
by default leaves alone the uncommitted changes in the working tree directory. Then is it necessary to run git stash push
before git checkout
(and then run git stash pop
at some later point after git checkout
)? Or when is it necessary, and when is it not?
我的问题来自 https://stackoverflow.com/a/48156644/156458 ,并且更为笼统. /p>
My question comes from https://stackoverflow.com/a/48156644/156458 and is more general.
推荐答案
如果存在未提交的更改,这些更改将被git checkout
覆盖,该命令将抱怨并中止.在这种情况下,必须使用git commit
或git stash
.
If there are uncommitted changes that will be overwritten by git checkout
the command will complain and abort. In that case git commit
or git stash
is necessary.
如果存在未提交的更改,且不会被git checkout
覆盖,则命令将成功执行,并且更改将溢出到新分支中.如果不想在新分支中看到这些更改,请在git checkout
之前使用git commit
或git stash
.
If there are uncommitted changes that will not be overwritten by git checkout
the command succeeds and the changes spilled into the new branch. If you don't want to see these changes in the new branch use git commit
or git stash
before git checkout
.
与其他命令(例如git pull
和git rebase
)不同,git checkout
没有选项--autostash
,因为它没有意义:在大多数情况下,当您切换到新命令时,您不想自动执行git stash pop
分支.
Unlike other commands (like git pull
and git rebase
) git checkout
doesn't have option --autostash
because it's meaningless: in most cases you don't want to automatically do git stash pop
when switching to a new branch.
这篇关于是否需要在`git checkout`之前运行`git stash push`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!