'git: Automatically stash+pop on checkout

If I do git checkout my-super-branch git tells me:

error: Your local changes to the following files would be overwritten by checkout:
    somedir/somefile.py
Please, commit your changes or stash them before you can switch branches.
Aborting

Is there a simple way to tell git to do stash+pop automatically?



Solution 1:[1]

There is no command-line option for autostash on checkout. I created a script checkout.sh like this:

#!/bin/bash
git stash && git fetch && git checkout $1 && git stash pop

So I can use it with my chosen branch: checkout.sh my-branch

Solution 2:[2]

Git allows Pipelining operations:

git stash && git fetch && git checkout your-branch && git stash apply

Your changes are still in stash as I used git stash apply.

Resolve conflicts if any.

In case you wish to remove your changes from stash after above call below:

git stash drop

Otherwise you could just use git stash pop

Solution 3:[3]

GIT >= 2.27.0

Having changes not staged for commit:

git switch other_branch

and now we are on other_branch with those changes.

Solution 4:[4]

To stash your changes and move them onto another branch, add this to your .gitconfig:

[alias]
    stashonto = "!f() { if [ -z \"$1\" ] ; then echo 'Error: stashonto requires destination branch.\nExample: git stashonto master\nExample: git stashonto -b new-branch 98e7f99e' ; else git stash --include-untracked && git checkout $* && git stash apply ; fi;  }; f"

That creates a git alias that calls a shell function so we can chain several git commands. Here's the function broken down so it's easier to read:

f() {
  if [ -z \"$1\" ] ; then
    echo 'Error: stashonto requires destination branch.\nExample: git stashonto master\nExample: git stashonto -b new-branch 98e7f99e' 
  else
    # stash everything
    (git stash --include-untracked
    # switch branch
    && git checkout $*
    # apply stash (you must 'git stash drop' to delete the stash if everything goes well)
    && git stash apply)
  fi
}
f

Warning: It's not fancy enough to detect whether stash saved anything, so if you don't have any changes to stash it will print "No local changes to save" and then apply your previous stash. That's why I'm using apply instead of pop.

Usage:

# stash changes and apply on master
git stashonto master
# stash changes and apply on new branch 'project' off of commit 98e7f99e
git stashonto -b project 98e7f99e

Solution 5:[5]

I made this "smart switch" command. Before to switch, it tries to stash uncommitted changes, leaving a mark in the message.
When you are back, it looks for any marked stash associated to that branch and pop it.

https://gist.github.com/mgaitan/d9a3523d79cd5f9fbfd626f646f0560b

I hope it'll be useful for somebody else.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 franbenz
Solution 2 Vinay Prajapati
Solution 3 suside
Solution 4 idbrii
Solution 5 tin_nqn