In this article you learn about git stash. like what is git stash? how git stash can help us and what is the use of git stash? how add some specific file to git stash?
git stash
is a command in Git that allows you to temporarily save changes that have not been committed or staged, so that you can switch to a different branch or work on something else, and then later return to the stashed changes.
Here is how to use git stash
:
git stash
command with an optional message describing the stash: git stash save "Stash message"
git stash apply
command.git stash list
command.
Note: git stash
does not affect your repository's commit history, and the changes are stored only in memory until they are reapplied. It is a useful tool for temporarily saving changes while you work on something else, but it should not be used as a substitute for committing changes to your repository's history.
git stash can be useful in several scenarios:
git stash
, you can save those changes, switch to the other branch, and then reapply the stashed changes later.
Note: git stash
should not be used as a substitute for committing changes to your repository's history, as stashes can be lost or forgotten. It is a useful tool for temporarily saving changes while you work on something else, but you should eventually commit the changes to your repository's history to ensure they are preserved and shared with others.
To stash a specific file in Git, you need to first stage the changes you made to that file using the git add
command. Then, you can use the git stash
command with the --keep-index
option:
git add
git stash save --keep-index "Stash message"
where is the name of the file you want to stash, and "Stash message"
is an optional message describing the stash
The --keep-index
option tells Git to stash only the changes that have not been staged, while keeping the staged changes in the working tree. This allows you to stash a specific file while preserving your changes to other files.
Name a Git stash by including a stash message and apply a git stash with specific message or stash ID ?
You can name a Git stash by including a stash message with the git stash save
command. The stash message acts as the name of the stash. To retrieve a stash by name, you can use the git stash apply
command with the stash message:
# save a stash with a message
git stash save "stash name"
# apply a stash with a specific message
git stash apply "stash name"
You can also list all stashes and their messages using the git stash list
command:
# list all stashes and their messages
git stash list
Then, you can apply a stash by specifying the stash ID in the git stash apply
command:
# apply a stash by ID
git stash apply stash@{ID}
where ID
is the stash ID you want to apply, listed in the git stash list
command.