Stash away changes

git stash

Check stash list

git stash list

Apply the most recent stash and leave it on the list

git stash apply

Apply specific stash and leave it on the list

git stash apply stash@{0}

Apply the most recent stash and clear it from the list

git stash pop

Apply specific stash and clear it from the list

git stash pop stash@{0}

Delete the most recent stash from the list

git stash drop

Delete specific stash from the list

git stash drop stash@{0}

Apply stash to a new branch called feature

git stash branch feature

Exercise

  1. Create index.html file with content <h1>Hello World!</h1> and blank test.html file
    touch index.html
    vi index.html
    touch test.html
  2. Add all to staging area and commit to master branch
    git add .
    git commit -m "Initial commit"
  3. Create new branch named 'feature-branch'
    git checkout -b feature-branch
  4. Edit file test.html and add content <h2>This is a test</h2>
    vi test.html
  5. Stash the changes
    git stash
  6. Checkout master branch
    git checkout master
  7. Edit file index.html and change <h1> tags to <h3> tags
    vi index.html
  8. Add and commit changes
    git add .
    git commit -m "Change header tags"
  9. Checkout branch feature-branch
    git checkout feature-branch
  10. Check stash list
    git stash list
  11. Apply stash with 'pop' command
    git stash pop
  12. Add and commit changes to feature-branch
    git add .
    git commit -m "Changes applied"
  13. Checkout master branch and merge feature-branch
    git checkout master
    git merge feature-branch