Git Stash Changes

Git Stash Changes

Git Stash Changes: A Complete Guide

What is Git Stash?

In Git, stashing allows you to temporarily save your uncommitted changes (both staged and unstaged) in a "stash," so you can work on something else without losing your progress. This is particularly useful when you need to switch branches but don’t want to commit unfinished work yet.

Think of stashing as a way to "shelve" your current work, allowing you to apply it later when you’re ready.

Why Use Git Stash?

  1. Switching Branches Without Committing: If you’re working on a feature or bugfix and need to quickly switch to another branch, stashing saves your work so you can return to it later.

  2. Temporary Storage: If you’re not ready to commit your changes but want to clear your working directory to test something or pull the latest changes from the repository, stashing can temporarily save your progress.

  3. Experimental Changes: Sometimes, you want to test an idea but don’t want to commit it yet. Stashing allows you to save the experiment and return to your clean state.

Basic Git Stash Commands

Git provides a few commands to stash your changes, apply them, and manage the stashed changes.

Stash Changes

  1. Stash Uncommitted Changes: To stash both staged and unstaged changes, use the following command:

    git stash

    This will save the current state of your working directory and leave you with a clean working directory.

  2. Stash Only Staged Changes: If you only want to stash the staged changes (not the ones you’ve made but haven't staged), use:

    git stash --staged
  3. Stash Changes with a Message: It’s often helpful to add a message to your stash to remind yourself of what changes were saved. You can do this by adding a -m option followed by your message:

    git stash save "WIP: Feature X development"

List Stashes

To see a list of all stashed changes, use:

git stash list

This will display all the stashes, along with their names (e.g., stash@{0}, stash@{1}, etc.) and any messages you provided when creating the stash.

Apply Stashed Changes

Once you’re ready to apply the changes from your stash, use the git stash apply command. By default, this will apply to the most recent stash:

git stash apply

If you want to apply a specific stash, use its identifier:

git stash apply stash@{1}

Git Stash Pop

The a git stash pop command is similar to git stash apply, but with one key difference: it removes the stash from the stash list after it has been applied. This is useful when you’re done with the stash and don’t want to keep it around.

git stash pop

Or, apply a specific stash and remove it from the list:

git stash pop stash@{1}

Stash Changes and Switch Branches

One of the most common use cases for stashing is when you need to switch branches without committing your work. Here’s how you can do that:

  1. Stash Changes:

    git stash
  2. Switch Branches: You can now safely switch branches without worrying about uncommitted changes:

    git checkout <other-branch>
  3. Apply Stash When Ready: After switching branches, you can apply the stash to bring your previous work back:

    git stash pop

Stash Untracked or Ignored Files

By default, Git only stashes tracked files. However, if you want to stash untracked or ignored files as well, you can use the -u or -a options, respectively:

  1. Stash Untracked Files:

    git stash -u

    This will stash both staged and unstaged changes, including untracked files.

  2. Stash Ignored Files:

    git stash -a

    This will stash all files, including ignored ones.

Drop a Stash

If you no longer need a particular stash, you can remove it using the git stash drop command:

git stash drop stash@{0}

This will remove the specific stash from the list.

Clear All Stashes

If you want to delete all stashed changes, use:

git stash clear

This will remove all stashes from the list, so be careful when using this command!

Apply a Stash to a Different Branch

You can stash changes on one branch and apply them to another branch if needed. Here’s how:

  1. Stash the Changes on the Current Branch:

    git stash
  2. Switch to the Target Branch:

    git checkout <target-branch>
  3. Apply the Stash to the New Branch:

    git stash apply

This allows you to carry over changes between branches without committing them.

Stash Workflow Example

Let’s go through a common workflow to illustrate stashing in action:

  1. You’re working on featureA and make some changes, but you need to quickly fix a bug on master.

    git stash
  2. Switch to the master branch to fix the bug:

    git checkout master
  3. After fixing the bug, commit the changes on master:

    git commit -am "Fixed bug"
  4. Now switch back to the featureA branch:

    git checkout featureA
  5. Apply your stashed changes to continue working on featureA:

    git stash pop
  6. Once you’re done with the feature, commit your changes:

    git commit -am "Completed feature A"

Conclusion

Git stash is a powerful tool that allows you to temporarily save your work and return to it later, without the need for creating intermediate commits. It’s particularly useful when you need to switch branches or test something without losing your progress. By mastering stashing, you can improve your workflow and avoid interruptions in your development process.

This guide should provide a complete understanding of Git stashing for your website readers. Let me know if you need further clarification or additional examples!

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close