Reputation: 27825
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?
Upvotes: 37
Views: 13072
Reputation: 321
git checkout -m target-branch
or
git checkout --merge target-branch
https://git-scm.com/docs/git-checkout#Documentation/git-checkout.txt---merge
Upvotes: 32
Reputation: 11
Hey i was facing the same issue and I didn't really like the other solutions people used to get around this problem so I created a vscode extension that mimics the built in "Git: Checkout to" command but automatically stashes and pops your changes Marketplace link. I am still working on the extension so it is still a WIP but it works well enough to be actually used. More functionality like creating branches etc coming soon.
Upvotes: 0
Reputation: 106
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.
Upvotes: 1
Reputation: 695
GIT >= 2.27.0
Having changes not staged for commit:
git switch other_branch
and now we are on other_branch
with those changes.
Upvotes: -3
Reputation: 11916
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
Upvotes: 1
Reputation: 712
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
Upvotes: 5
Reputation: 7504
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
Upvotes: 2