git stash command in Git is used to temporarily save changes that you have made in your working directory but are not ready to commit. It allows you to store your modifications in a “stash” and revert your working directory back to a clean state, so you can switch branches, pull changes from remote repositories, or perform other operations without committing the changes. Later, you can apply the stash and continue working on your changes. Here’s how you can use git stash:
- Stashing Changes:
- Run
git stash save
to create a new stash. You can also include a message to describe the changes you’re stashing, likegit stash save "Work in progress"
. - Git will stash your changes and revert your working directory to a clean state, removing the modifications.
- Run
- Switching Branches:
- Once your changes are stashed, you can switch to a different branch using
git checkout
or perform any other Git operation without interference from your uncommitted changes.
- Once your changes are stashed, you can switch to a different branch using
- Applying the Stashed Changes:
- To apply your stashed changes, use
git stash apply
orgit stash pop
.git stash apply
reapplies the changes from the most recent stash without removing it from the stash list.git stash pop
reapplies the changes from the most recent stash and removes it from the stash list.
- You can also specify a specific stash to apply by using
git stash apply stash@{n}
, wheren
is the index of the stash in the stash list.
- To apply your stashed changes, use
- Delete Stashes:
- If you no longer need a stash, you can delete it from the stash list using
git stash drop stash@{n}
. - To remove all stashes, you can use
git stash clear
.
- If you no longer need a stash, you can delete it from the stash list using
- View Stashes:
- To view the list of stashes and their details, you can use
git stash list
. It shows the stash index, description, and branch where the stash was created.
- To view the list of stashes and their details, you can use
Leave a Reply