bobroxsox bobroxsox - 1 year ago 203
Git Question

Git stash pop only if successfully stashed before

Part of my workflow involves doing a lot of this:

  • git stash changes

  • git pull

  • pop stashed changes

  • launch mergetool to resolve conflicts

I am trying to write a script to do all of these things at once, so I can just call it from the terminal.


# First stash our local changes
git stash

# Then git pull to update our repo
git pull

# Pop the stash
git stash pop

# Launch mergetool if necessary
git mergetool

The problem I'm running into is that if I run this accidentally, and there are no changes to stash, the
git stash pop
applies some (usually super old) stash. What I want to do is run
git stash pop
only if I actually stashed something before. Is there a way to do this?

Answer Source

Reading your explanation of why do you do what you do I'd probably go for a completely different approach. First, I'd fetch the remote you want to use:

git fetch <remote> (e.g. git fetch origin)

And then, I'd carry out a rebase against a specific branch of that remote:

git rebase <remote>/<branch> (e.g. git rebase origin/master)

This would merge your changes and you'd still be able to solve any conflicts.

If you don't like this approach, you might want to use git pull with the --no-commit flag instead:

git pull --no-commit

This way no autocommit would be performed after the merge.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download