git stash temporarily shelves (or stashes) changes you"ve made khổng lồ your working copy so you can work on something else, & then come back and re-apply them later on. Stashing is handy if you need khổng lồ quickly switch context and work on something else, but you"re mid-way through a code change and aren"t quite ready to lớn commit.
Bạn đang xem: Nhàn hơn cùng git stash!
Stashing your work
The git stash command takes your uncommitted changes (both staged và unstaged), saves them away for later use, & then reverts them from your working copy. For example:
$gitstatusOnbranchmainChangestobecommitted:newfile:style.cssChangesnotstagedforcommit:modified:index.html$gitstashSavedworkingdirectoryandindexstateWIPonmain:5002d47ournewhomepageHEADisnowat5002d47ournewhomepage$gitstatusOnbranchmainnothingtocommit,workingtreeclean
At this point you"re không tính phí to make changes, create new commits, switch branches, and perform any other Git operations; then come back & re-apply your stash when you"re ready.
Note that the stash is local khổng lồ your Git repository; stashes are not transferred to the vps when you push.
Re-applying your stashed changes
You can reapply previously stashed changes with git stash pop:
$gitstatusOnbranchmainnothingtocommit,workingtreeclean$gitstashpopOnbranchmainChangestobecommitted:newfile:style.cssChangesnotstagedforcommit:modified:index.htmlDroppedrefs/stash
0(32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)
Popping your stash removes the changes from your stash & reapplies them to your working copy.
Alternatively, you can reapply the changes to your working copy and keep them in your stash with git stash apply:
$gitstashapplyOnbranchmainChangestobecommitted:newfile:style.cssChangesnotstagedforcommit:modified:index.html
This is useful if you want to lớn apply the same stashed changes to lớn multiple branches.
Now that you know the basics of stashing, there is one caveat with git stash you need lớn be aware of: by default Git won"t stash changes made khổng lồ untracked or ignored files.
Stashing untracked or ignored files
By default, running git stash will stash:
changes that have been added lớn your index (staged changes) changes made khổng lồ files that are currently tracked by Git (unstaged changes)But it will not stash:
new files in your working copy that have not yet been stagedSo if we địa chỉ a third tệp tin to our example above, but don"t stage it (i.e. We don"t run git add), git stash won"t stash it.
$script.js$gitstatusOnbranchmainChangestobecommitted:newfile:style.cssChangesnotstagedforcommit:modified:index.htmlUntrackedfiles:script.js$gitstashSavedworkingdirectoryandindexstateWIPonmain:5002d47ournewhomepageHEADisnowat5002d47ournewhomepage$gitstatusOnbranchmainUntrackedfiles:script.js
Adding the -u option (or --include-untracked) tells git stash khổng lồ also stash your untracked files:
$gitstatusOnbranchmainChangestobecommitted:newfile:style.cssChangesnotstagedforcommit:modified:index.htmlUntrackedfiles:script.js$gitstash-uSavedworkingdirectoryandindexstateWIPonmain:5002d47ournewhomepageHEADisnowat5002d47ournewhomepage$gitstatusOnbranchmainnothingtocommit,workingtreeclean
You can include changes khổng lồ ignored files as well by passing the -a option (or --all) when running git stash.

Managing multiple stashes
You aren"t limited khổng lồ a single stash. You can run git stash several times to lớn create multiple stashes, and then use git stash danh mục to view them. By default, stashes are identified simply as a "WIP" – work in progress – on vị trí cao nhất of the branch and commit that you created the stash from. After a while it can be difficult lớn remember what each stash contains:
$gitstashliststash
0:WIPonmain:5002d47ournewhomepagestash
1:WIPonmain:5002d47ournewhomepagestash
2:WIPonmain:5002d47ournewhomepage
To provide a bit more context, it"s good practice to annotate your stashes with a description, using git stash save "message":
$gitstashsave"addstyletooursite"SavedworkingdirectoryandindexstateOnmain:addstyletooursiteHEADisnowat5002d47ournewhomepage$gitstashliststash
0:Onmain:addstyletooursitestash
1:WIPonmain:5002d47ournewhomepagestash
2:WIPonmain:5002d47ournewhomepage
By default, git stash pop will re-apply the most recently created stash: stash
0
You can choose which stash to lớn re-apply by passing its identifier as the last argument, for example:
$gitstashpopstash
2
Viewing stash diffs
You can view a summary of a stash with git stash show:$gitstashshowindex.html|1+style.css|3+++2fileschanged,4insertions(+)
Or pass the -p option (or --patch) to lớn view the full diff of a stash:
$gitstashshow-pdiff--gita/style.cssb/style.cssnewfilemode100644index0000000..d92368b---/dev/null+++b/style.css
-0,0+1,3
+*+text-decoration:blink;+diff--gita/index.htmlb/index.htmlindex9daeafb..ebdcbd2100644---a/index.html+++b/index.html
-1+1,2
+
Partial stashes
You can also choose khổng lồ stash just a single file, a collection of files, or individual changes from within files. If you pass the -p option (or --patch) to lớn git stash, it will iterate through each changed "hunk" in your working copy & ask whether you wish to stash it:$gitstash-pdiff--gita/style.cssb/style.cssnewfilemode100644index0000000..d92368b---/dev/null+++b/style.css
-0,0+1,3
+*+text-decoration:blink;+Stashthishunk
-1+1,2
+Stashthishunk
/ | search for a hunk by regex |
? | help |
n | don"t stash this hunk |
q | quit (any hunks that have already been selected will be stashed) |
s | split this hunk into smaller hunks |
y | stash this hunk |
There is no explicit "abort" command, but hitting CTRL-C(SIGINT) will abort the stash process.
Creating a branch from your stash
If the changes on your branch diverge from the changes in your stash, you may run into conflicts when popping or applying your stash. Instead, you can use git stash branch lớn create a new branch lớn apply your stashed changes to:
$gitstashbranchadd-stylesheetstash
1Switchedtoanewbranch'add-stylesheet'Onbranchadd-stylesheetChangestobecommitted:newfile:style.cssChangesnotstagedforcommit:modified:index.htmlDroppedrefs/stash
1(32b3aa1d185dfe6d57b3c3cc3b32cbf3e380cc6a)
This checks out a new branch based on the commit that you created your stash from, and then pops your stashed changes onto it.
Cleaning up your stash
If you decide you no longer need a particular stash, you can delete it with git stash drop:
$gitstashdropstash
1Droppedstash
1(17e2697fd8251df6163117cb3d58c1f62a5e7cdb)
Or you can delete all of your stashes with:
$gitstashclear
How git stash works
If you just wanted lớn know how khổng lồ use git stash, you can stop reading here. But if you"re curious about how Git (and git stash) works under the hood, read on!Stashes are actually encoded in your repository as commit objects. The special ref at .git/refs/stash points to lớn your most recently created stash, and previously created stashes are referenced by the stash ref"s reflog. This is why you refer to lớn stashes by stash
n: you"re actually referring khổng lồ the nth reflog entry for the stash ref. Since a stash is just a commit, you can inspect it with git log:
$gitlog--oneline--graphstash
0*-.953dddeWIPonmain:5002d47ournewhomepage|\||*24b35a1untrackedfilesonmain:5002d47ournewhomepage|*7023dd4indexonmain:5002d47ournewhomepage|/*5002d47ournewhomepage
Depending on what you stashed, a single git stash operation creates either two or three new commits. The commits in the diagram above are:
0, a new commit khổng lồ store the tracked files that were in your working copy when you ran git stash stash
0"s first parent, the pre-existing commit that was at HEAD when you ran git stash stash
0"s second parent, a new commit representing the index when you ran git stash stash
0"s third parent, a new commit representing untracked files that were in your working copy when you ran git stash. This third parent only created if: your working copy actually contained untracked files; & you specified the --include-untracked or --all option when invoked git stash.
How git stash encodes your worktree and index as commits:
Before stashing, your worktree may contain changes to lớn tracked files, untracked files, và ignored files. Some of these changes may also be staged in the index.

Invoking git stash encodes any changes to lớn tracked files as two new commits in your DAG: one for unstaged changes, and one for changes staged in the index. The special refs/stash ref is updated to lớn point to them.

Using the --include-untracked option also encodes any changes to lớn untracked files as an additional commit.

Using the --all option includes changes khổng lồ any ignored files alongside changes to lớn untracked files in the same commit.

When you run git stash pop, the changes from the commits above are used to update your working copy và index, và the stash reflog is shuffled to remove the popped commit. Lưu ý that the popped commits aren"t immediately deleted, but vày become candidates for future garbage collection.
Ready khổng lồ learn Git?
Try this interactive tutorial.
Xem thêm: Xem Lại Bóng Đá U23 Việt Nam, Video: Xem Lại Các Bàn Thắng Trận Đt Việt Nam 3
Get started now
Next up: .gitignoreStart next tutorial
Powered By

Recommend



Want future articles?Submit
Site hosted by

creativecommons.orgExcept where otherwise noted, all content is licensed under a Creative Commons Attribution 2.5 australia License.