Search code examples
gitgit-branchgit-stash

Git stash and apply to a branch


So I make changes in 'branch - A', and pushed that branch remotely. Now I checkout a new 'branch - B' and begin work on it. But, now I need to work on branch-A again.

This is what i plan to do:

  1. While my current branch is B I will do: git stash.

  2. git checkout branch-A

After I am done working on branch A, I would do git stash ( to save changes of branch-A).

Now here is my question, If I need to work on branch-B again, how should i do git stash pop ?

I dont want A's stash to be applied on branch B ?


Solution

  • You can see the stash list (follow stack). git stash apply default to stash@{0}. If you want another previous stash then, just mention stash@<number>.

    $ git checkout B
    # do some change here
    $ git add .
    $ git stash save 'stash of B'
    
    $ git checkout A
    # do some change here
    $ git add .
    $ git stash save 'stash of A'
    
    $ git checkout B  
    $ git stash list                # see the stash list with message
    $ git stash apply stash@{1}     # B's stash will be applied
    
    $ git stash drop stash@{1}      # remove B's stash from stash stack