Git Stash
git stash
is a Git command that allows you to temporarily store changes that you have made to your working directory, so that you can switch branches or restore your repository to a previous state. It is useful when you want to switch to a different branch to work on something else, but you don't want to commit your current changes.
Here is a detailed tutorial on how to use git stash
:
Open a terminal window and navigate to the local repository that you want to stash changes in.
Run the following command to stash your changes:
git stash
This will store your changes in a temporary area, and restore your repository to the state it was in before you made the changes.
- If you want to include a message with your stash, you can use the
-m
flag followed by the message. For example:
git stash -m "Work in progress on new feature"
If you want to stash only certain changes, and not all of them, you can use the
git stash push
command with the--patch
flag. This will allow you to interactively select which changes to stash and which ones to leave behind.To view the stashes that you have saved, you can use the
git stash list
command. This will show you a list of all the stashes that you have created, along with the messages that you included with each stash.To apply a stash to your working directory, you can use the
git stash pop
command. This will restore the changes that you stashed and remove the stash from the list. If you want to apply the stash without removing it from the list, you can use thegit stash apply
command instead.To permanently remove a stash, you can use the
git stash drop
command followed by the stash name. You can also use thegit stash clear
command to remove all stashes at once.
Here are a few more things you might want to know about using git stash
:
By default,
git stash
will stash all of the changes in your working directory, including both modified and untracked files. If you want to stash only the changes to tracked files, you can use the--keep-index
flag. This will leave untracked files in your working directory.If you want to stash only the changes to a particular file or directory, you can specify the path as an argument to the
git stash
command. For example:
git stash src/main.c
If you have made changes to multiple branches and want to stash them separately, you can use the
--branch
flag. This will create a stash for each branch, and restore your repository to the state it was in before you made any changes to that branch.If you have multiple stashes, you can apply them one at a time by specifying the stash name. For example:
git stash apply stash@{2}
This will apply the third stash in the list (since the stash list is zero-indexed).
- If you want to create a new branch from a stash, you can use the
git stash branch
command followed by the name of the new branch. This will create a new branch and apply the stash to it. git stash
stores your changes in a stack, with the most recent stash at the top. When you apply a stash, Git will pop it off the top of the stack and apply the changes. If you have multiple stashes, you can use thegit stash pop stash@{2}
command to apply the third stash in the list (since the stash list is zero-indexed).If you want to create a new branch and apply a stash to it at the same time, you can use the
git stash branch <branch-name>
command. This will create a new branch and apply the most recent stash to it.If you want to stash only the changes to tracked files, you can use the
git stash --keep-index
command. This will stash all of the changes in your working directory except for those that have been staged for the next commit.If you want to stash only the changes to untracked files, you can use the
git stash --include-untracked
command. This will stash all of the changes in your working directory, including untracked files, but it will leave your staged changes unchanged.If you want to stash only the changes to ignored files, you can use the
git stash --include-ignored
command. This will stash all of the changes in your working directory, including ignored files, but it will leave your staged and untracked changes unchanged.