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.

  • How can I translate trunk/branch concepts from Subversion to Git?
  • Using git with android source repository retrieved with repo
  • Cloning Android sources to a local repository server
  • Get tags below a particular tag in git
  • Is git only good for text files/source code?
  • Github referring sites traffic number varies
  • git push force, phpFog, and random file not in my repo showing up
  • Which version is this? Compare a bare source tree with the repository
  • How to avoid re-formatting of file in GitHub browser editor?
  • How does git handle cloning another repository into a subdirectory?
  • i want to display the git version on my site
  • Git ignore local file changes
  • 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 (http://git-scm.com/book)

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