Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

Is it necessary to run `git stash push` before `git checkout`

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?

My question comes from https://stackoverflow.com/a/48156644/156458 and is more general.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...