Git stash storage unfinished work

Source: Internet
Author: User

Sometimes, you do not want to submit the job, but you have to switch to another branch to use stash.

"'Store'" can get the intermediate state of your working directory-that is, the tracked file you modified and temporary changes-and save it to a stack of unfinished changes. medium, applications can be re-applied at any time.

Notes

Note the definition of the scope of action in the above sentence. If your file is not added, that is, there is no "tracked" or "temporary storage", stash will not save it for you. For example, I added two files, readme and readme. I didn't add the two new files in git, and then input the following command:

Dark @ dark-LAPTOP :~ /Box_4.2 $ git stash
Will prompt:

No local changes to save
Indicates stash

Body

Run git status. You can see your intermediate status:

$ git status# On branch master# Changes to be committed:#(use "git reset HEAD <file>..." to unstage)##modified:index.html## Changed but not updated:#(use "git add <file>..." to update what will be committed)##modified:lib/simplegit.rb

Now you want to switch the branch, but you do not want to submit your ongoing work; so you store these changes. To push a new storage to the stack, just run git Stash:

$ git stashSaved working directory and index state \"WIP on master: 049d078 added the index file"HEAD is now at 049d078 added the index file(To restore them type "git stash apply")

Your working directory is clean:

$ git status# On branch masternothing to commit (working directory clean)

At this time, you can easily switch to other branch jobs; your changes are saved on the stack. To view the existing storage, you can use git stash list:

$ git stash liststash@{0}: WIP on master: 049d078 added the index filestash@{1}: WIP on master: c264051... Revert "added file_size"stash@{2}: WIP on master: 21d80a5... added number to log

In this case, two storages have been stored before, so you can access three different storages. You can re-apply the storage you just implemented. The command used is mentioned in the Help output of the original stash command.
Git stash apply. If you want to apply the earlier storage, you can specify it by name, like this: git stash apply stash @ 2. If you do not specify it, git uses the latest storage by default and tries to apply it:

$ git stash apply# On branch master# Changed but not updated:#(use "git add <file>..." to update what will be committed)## modified: index.html# modified: lib/simplegit.rb

You can see that git re-modified the files you stored that were not submitted at the time. In this case, the working directory you try to store applications is clean and belongs to the same branch, but a clean working directory and Application
The same branch is not a necessary condition for application storage. You can save a copy of the storage on one branch, switch to another branch, and apply the changes again. When the working directory contains modified and uncommitted files, you can also apply the storage-Git will give a merge conflict if any change cannot be cleanly applied.

Changes to files are re-applied, but temporary files are not re-saved. In that case, you must include the -- Index option when running the GIT stash apply command to tell the command to re-apply the temporary changes.
If you do this, you should have returned to your original position:

$ git stash apply --index# On branch master# Changes to be committed:#(use "git reset HEAD <file>..." to unstage)##modified:index.html## Changed but not updated:#(use "git add <file>..." to update what will be committed)##modified:lib/simplegit.rb

The apply option only attempts to store applications. The stored content is still on the stack. To remove it, you can run git stash drop and add the name of the storage you want to remove:

$ git stash liststash@{0}: WIP on master: 049d078 added the index filestash@{1}: WIP on master: c264051... Revert "added file_size"stash@{2}: WIP on master: 21d80a5... added number to log$ git stash drop stash@{0}Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

You can also run git stash pop to re-store the application and immediately remove it from the stack.

Shortcut-create branch from storage

If you save some work, ignore it for the moment, and continue to work on the branch of your storage work, you may encounter some problems when re-applying the work. If you try to apply a change to a file you modified later,

You will encounter a merge conflict and must resolve it. If you want to reinspect your storage changes in a more convenient way, you can run git stash Branch, which will create a new branch to check the location of your storage work.
Submit and re-apply your work. If it succeeds, the storage will be discarded.

$ git stash branch testchangesSwitched to a new branch "testchanges"# On branch testchanges# Changes to be committed:#(use "git reset HEAD <file>..." to unstage)##modified:index.html## Changed but not updated:#(use "git add <file>..." to update what will be committed)##modified:lib/simplegit.rb#Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)

This is a great shortcut to restore the storage and continue the work on the new branch.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.