Prevent 'git checkout' if uncommitted changes
I frequently have a bad time because I checkout a branch with uncommitted changes that I should have committed before changing branches but forgot. Is there a way using some git config I can have the
git checkout fail if there are any pending changes or new files? I’ve recently gotten into the habit of running
git status before
git checkout, but that’s inconvenient and still prone to forgetfulness. I can probably use a Bash alias to chain those two commands together in some way, but I’m hoping there’s either something I can throw in my
.gitconfig or a flag that I’m not seeing in
git config‘s man page.
2 Solutions collect form web for “Prevent 'git checkout' if uncommitted changes”
git checkout will not run if the checkout would cause any changes to get lost during its process. So even if you use
git checkout with a dirty working directory, all your changes are carried over to the other branch. And you can also just switch back to the previous branch to get in the old state.
If the branches are incompatible (for example a file you modified was deleted),
git checkout should automatically fail, preventing you from losing any changes.
YOu can use
to save your uncomitted changes on a stack.
git stash list (to see which stashed you have stored)
git stash apply (to apply your changes stored in stash)
You can even create branch from the stash. Refer gitscm book for more information (http://git-scm.com/book)