Stashing - biswajitsundara/gitdoc GitHub Wiki

Git stash temporarily shelves the changes on the working copy so that we can work on something else and then come back and apply the stashed changes.

The Problem Statement

  • You are on feature1 branch and you have made some changes to index.html file
  • Now if you switch to feature2 branch and work on something, then it will throw an error
git checkout feature2

error: Your local changes to the following files would be overwritten by checkout:
        index.html
Please commit your changes or stash them before you switch branches.
Aborting
  • That means we have two options, either we can commit the changes or stash them.
  • The working copies (which is not final) shouldn't be committed, those are considered dirty commits
  • Then we are left with one option that's stashing.

The Solution

  • Let's say you are on feature1
  git branch
     * feature1
       feature2
       master
  • You make some changes to contact.html file
    git status
    (use "git add ..." to update what will be committed)
        modified:   contact.html
  • Stash the changes with a name (I prefer to give a name)
    git stash push -m "phone changes"
    Saved working directory and index state On feature1: phone changes
  • Make some more changes and stash them with another name
    git stash push -m "email changes"
    Saved working directory and index state On feature1: email changes
  • Check the available stashes
    git stash list
    stash@{0}: On feature1: email changes
    stash@{1}: On feature1: phone changes
  • Now switch to another branch feature2 and work on that
    git checkout feature2
    Switched to branch 'feature2'
    Your branch is up to date with 'origin/feature2'.

We can also stash the changes on feature2 or commit the changes

  • Now switch to branch feature1
    git checkout feature1
    Switched to branch 'feature1'
    Your branch is up to date with 'origin/feature1'.
  • Now we want to apply the changes we had saved in stash
    git stash list
    stash@{0}: On feature1: email changes
    stash@{1}: On feature1: phone changes

    git stash pop 1
    This will apply the stash index 1 "phone changes" to the "feature1" branch
    and also delete the stash
  • Then the workflow is same for committing and pushing the changes
git add --all
git commit -m "phone"
git push -u origin feature1

Stash commands

Commands Description
git stash This will create a stash with no name
git stash apply Applies the most recent stash (0th index) from the stash list
git stash list Displays all the stashes
git stash apply {index} From the stash list applies the stash based on the index (0,1..)
git stash drop {index} Deletes the stash from the list
git stash push -m "desc" Creates a stash with the provided description (easy to find)
git stash pop {index} Applies the stash and deletes it from the list
git stash clear Clears the stash list

Note: Don't add curly braces with index like {0}, it should be 0,1 etc.

⚠️ **GitHub.com Fallback** ⚠️