Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...