Stash means to hide in English. The purpose of git Stash is also to hide unfinished code and prevent it from interfering with the work of others or new branches.
The background,
1.1 We often come across situations like this
You’re working on a new feature in the Dev branch, and halfway through, someone comes along with a bug that needs to be fixed immediately, but halfway through, you don’t want to commit.
You can use the git stash command to save the current progress and then switch to another branch to fix bugs. After making the commit changes, we cut back to the dev branch and use git Stash pop to resume development.
There is no need to fix a BUG, just switch branches, modify the submission and then cut back to the original branch.
1.2 Is it really so much trouble?
- For example, there are scenarios as follows:
- in
dev
Create a file under the branchdev_file.txt
And,add
To make itstage
; - Then cut to
master
Branching, you’ll see thisdev_file.txt
Even in themaster
In the branch. Isn’t he supposed to just bedev
Branch?
- If you try you try again:
- Cut back
dev
Branch, executegit stash
; - Now you’re cutting back
master
Branch,dev_file.txt
It just disappeared.
At this point, I can’t help but say: Git stash, YYDS!
Second, the git stash
This will remind you when you implement git Stash:
Saved working Directory and index state WIP on newF2: b63fbcb add dev_file. TXT HEAD is now at b63fbcb add dev_file. TXTCopy the code
It already saves dev_file.txt.
2.1 What does git Stash do
It saves the current work progress, and stores the staging area and workspace changes in an unfinished change stack; After executing this command, you can run git status and see that your workspace is currently clean and unchanged.
git stash
It is local and cannot be uploaded to the server;- You can use
git stash save 'message... '
You can add some comments.
2.2 Git stash Commands
The command name | role |
---|---|
git stash | Hide the current workspace, at which point git status is clean |
git stash list | View all concealment, and the string before the colon on each line is the ID that identifies the concealment |
git stash apply | Redisplay hide identified as ID |
git stash drop | After git Apply restores hiding, you need to manually delete the records in the list list |
git stash pop | Restores the latest progress to the workspace |
git stash pop stash@[stash_id] | Restores the specified progress to the workspace |
Git stash pop stash@{1} Stash_id is obtained from the git stash list command;
3. Git Stash Application Scenario
3.1 Someone changes the same branch with me
When I change it locally, I find out that the remote branch has been changed, and then I change it locally, causing a conflict. I can’t push or pull.
At this point, you can handle it with git Stash
Git pull // add your stash changes back to the local branch and you can continue with your changes. If it's done, it's add,commit,push, Git, Stash, popCopy the code
3.2 Other branches were accidentally changed
For example, if I forget to switch and write the code in the wrong branch, I will make the change on the master branch. Let’s assume that my branch is feature/category_vechice.
// Switch to the branch that needs to be changed. // Pop the change to your current branch. Git Stash popCopy the code
Four, summary
As the name implies, Stash is a stack. Usually we store the files that need to be temporarily stored in the stack and restore the code to the state of last pull for operation.
As far as I am concerned, git Stash pop already meets my daily needs. After all, there are frequent online bugs or forgetting to cut code. What we should consider is not the problem of this command, but the quality of development.