Git practical skills: git stash

Time:2019-11-8

We often encounter the following situations:

BeingdevThe branch develops new functions, and when it’s half done, someone comes to give feedbackbug, let it be solved immediately, but when the new function is half finished, you don’t want to submit it, then you can use itgit stashThe command saves the current progress first. Then switch to another branch to modifybug, after modification and submission, switch backdevBranch, usinggit stash popTo restore the previous progress and continue to develop new functions.

1. Current modification of stash

  • git stash
    Saving the current work progress saves changes to the staging area and workspace. After executing this command, rungit statusCommand, you will find that the current is a clean workspace, no changes.
  1. stashIt is local and will not be uploaded to the server;
  2. Usegit stash save 'message...'You can add some comments.

2. View existingstash

  • git stash list
    Displays a list of saved progress. Which means,git stashThe command can be executed multiple times.

3. Reapply cachedstash

  • git stash pop [–index] [stash_id]
    Recover the previously cached working directory.
  1. git stash pop: restore the latest progress to the workspace.gitBy default, changes to the workspace and staging area will be restored to the workspace, and the progress will be deleted;
  2. git stash pop [email protected][stash_id]: restore the specified progress to the workspace, such as:git stash pop [email protected]{1}stash_idIt is throughgit stash listOrdered; ordered;
  3. adoptgit stash popAfter the command restores the progress, the current progress will be deleted;
  4. git stash apply [email protected]{1}: after the progress is restored, the current progress will not be deleted, and the latest stash is used by default[email protected]{0})。

4. removestash

  • git stash drop [stash_id]
    Delete a stored progress, such as:git stash drop [email protected]{0}
  1. If not specifiedstash_id:, the latest storage progress is deleted by default;
  2. git stash clear: delete all stored progress.