3

Is there a way to get git to automatically warn you when switching to a branch that has stashed changes?

I'm looking for something like:

$ git checkout my-branch
Switched to branch 'my-branch'
Stashed changes present: stash@{0}: WIP on my-branch: 836b45a My HEAD commit

Just so I don't forget about work that I already started on a branch when coming back to a project after a few days.

zool
  • 808
  • 8
  • 20

3 Answers3

5

A branch does not have "a stash".

A stash is simply a list of patches, that you can apply wherever you want.

How about appending the warning to a more branch-neutral command like git status then?

You have commit 2414b45 (git 1.6.4, Jun 2009)

Show presence of stashed changes in bash prompt.

Add a '$' in the __git_ps1 output to show stashed changes are present, when GIT_PS1_SHOWSTASHSTATE is set to a nonempty value.

The code for checking if the stash has entries is taken from 'git-stash.sh'.

So try:

export GIT_PS1_SHOWSTASHSTATE=1 # Unix
set GIT_PS1_SHOWSTASHSTATE=1 # Windows

git stash was initially added to git in commit f2c66ed (git 1.5.3, June 2007) by Nanako Shiraishi.

It is only a very recent git (2.4.2, Apr. 2014, commit ed178ef) which attempted to display a warning, only regarding your current index (irrespective of your current branch):

Cannot apply stash: Your index contains uncommitted changes.

stash: require a clean index to apply

If you have staged contents in your index and run "stash apply", we may hit a conflict and put new entries into the index. Recovering to your original state is difficult at that point, because tools like "git reset --keep" will blow away anything staged. We can make this safer by refusing to apply when there are staged changes.

It's possible we could provide better tooling here, as "git stash apply" should be writing only conflicts to the index (so we know that any stage-0 entries are potentially precious).
But it is the odd duck; most "mergy" commands will update the index for cleanly merged entries, and it is not worth updating our tooling to support this use case which is unlikely to be of interest (besides which, we would still need to block a dirty index for "stash apply --index", since that case would be ambiguous).

This was reverted in commit 1937610 (git 2.4.6, June 2015) though.


VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
4

No, that's not possible, simply because in Git stashed changes are not associated with a branch. I.e. you can easily stash uncommitted changes while being on branch A, and stash pop them while being on branch B. You probably need to resolve conflicts in such a case, but still it proves that the stash is global to the repository, not local to the branch.

Also see the questions Is git stash branch-specific or for the whole repository? or Why isn't the git stash unique per branch?

Community
  • 1
  • 1
sschuberth
  • 28,386
  • 6
  • 101
  • 146
  • Good to know. How about appending the warning to a more branch-neutral command like `git status` then? Something like `nothing to commit, working directory clean, but stashed changes present`. – zool Nov 08 '15 at 16:51
  • 1
    Well, you could come up with a `git-prompt` based solution for this, or simply use an existing solution like [bash-git-prompt](https://github.com/magicmonty/bash-git-prompt#examples) which shows the number of stashed entries. – sschuberth Nov 08 '15 at 16:55
2

How about using some bash for this?

You can add to your ~/.bashrc something like:

gco() {
    git checkout $@
    local CURRENT_BRANCH=$(git branch | grep '*' | awk '{ print $2 }')
    git stash list | grep $CURRENT_BRANCH
    if [ $? -eq 0 ]; then
        echo Hello! The current branch has stashed content!
    fi
}

Note: aliases take precedence over functions. Make sure gco is unaliased first.

If which gco shows something like gco: aliased to git checkout you'll have to use something else.

Diego Milán
  • 701
  • 7
  • 10