Junior — Middle
Explain the purpose and working principle of the git stash command in the Git version control system.
sobes.tech AI
Answer from AI
The git stash command is used to temporarily save the current changes in the working directory and index (staged changes), so you can switch to another branch or perform other actions without committing unfinished work.
Working principle:
git stashsaves all uncommitted changes (including changes in tracked files and the index) to a special stack.- After that, the working directory and index are reverted to the state of the last commit.
- These changes can later be restored using
git stash applyorgit stash pop.
Example usage:
# Save current changes
$ git stash
# Switch to another branch
$ git checkout feature-branch
# Return and restore changes
$ git checkout main
$ git stash pop
Thus, git stash helps to temporarily set aside work without creating commits, which is convenient when switching development contexts.