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.

  • Git: remove earlier commit but keep recent changes
  • Develop on windows, run unicorn in production on heroku
  • Hudson + Poll SCM - build upstream projects first?
  • How to merge two versions of the same project in xcode?
  • Resolving Git conflicts in SourceTree
  • git clone results in: “fatal: The remote end hung up unexpectedly”
  • Error finding tcl.init when running Git GUI
  • Xcode 5 - remove source control for project
  • removing untacked file and directories with git/info/exclude
  • Git for Windows: .bashrc or equivalent config files for Git Bash shell
  • How do I manage conflicts with git submodules?
  • Does increasing the number of unified diff context lines have any downsides?
  • 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

    git stash

    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 (

    Git Baby is a git and github fan, let's start git clone.