Chat Zalo Chat Messenger Phone Number Đăng nhập
Git stash - Saving Changes | Atlassian Git Tutorial

Git stash – Saving Changes | Atlassian Git Tutorial

Git temporarily saves the changes you’ve made to your working copy so you can work on something else, and then goes back and back and reapplies them later. Storage is useful if you need to quickly change context and work on something else, but you’re halfway through a code change and not ready to compromise.

Git Storage Save your work

    • Reapply saved changes
    • Storing untracked or ignored files
    • Managing
    • multiple caches Viewing stash differences

    • Partial guijos
    • Create a branch from the
    • stash

    • Cleaning up the
    • stash

    • How git
    • stash works

    • Save
    • your work

The git stash command takes uncommitted changes (both temporary and non-tentative), saves them for later use, and then rolls them back from the working copy. For example

: at

this point, you can make changes, create new commits, change branches, and perform any other Git operation; then go back and reapply

your stash when you’re ready. Note that the stash is

local to your Git repository; the stash is not transferred to the server when it is pushed. Reapply

saved changes You can

reapply previously saved

changes

using git stash pop:

Blowing up

your stash removes the changes from your stash and 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:

This is useful if you want to apply the same hidden changes to multiple branches.

Now that you know the basics of storage, there’s a caveat with the Git stash that you should be aware of: by default, Git will not hide changes made to untracked or ignored files.

Storage of

untracked or

ignored files

By default, running git stash will be

hidden: changes that have been

    added to the index (preconfigured changes) changes made to files that

  • Git is currently tracking (unconfigured changes)

But it won’t be hidden:

  • new files in your working copy that haven’t been configured yet. Files that have been
  • ignored

So if we add a third file to our previous example, but don’t prepare it (i.e. don’t run git add), git stash won’t hide it.

Adding the -u (or -include-untracked) option tells git stash

to save your untracked files as well:

You can also include changes to ignored files by passing the -a (or -all) option when running git stash.

Git stash options

Managing multiple caches

You’re not limited to a single stash. You can run git stash multiple times to create multiple sguardhes, and then use git stash list to view them. By default, caches are simply identified as a “WIP” (work in progress) at the top of the branch and confirm that you created the stash. After a while it can be difficult to remember what each stash contains

:

To provide a little more context, it’s good practice to annotate your stash with a description, using

git stash save “message”:

By default, git stash pop will reapply the most recently created stash: stash@{0}

You can choose which stash to reapply by passing its identifier as the last argument, For example

: Viewing stash differences You can view a summary of a

stash with git

stash show:

Or pass the -p (or -patch) option to see the full difference

of a

stash

:

Partial stash You

can also choose to save a single file, a collection of files, or individual changes from within the files. If you pass the -p (or -patch) option to git stash, it will iterate through each changed “hunk” in your working copy and ask if you want to save it:

Git Stash -p

Can you press ? for a complete list of hunk commands. Commonly useful are:

Command description / search for a piece by regex ? help n don’t stash this hunk q quit (any piece that has already been selected will be saved) s divide this piece into smaller pieces and save this piece

There is no explicit “abort” command, but pressing CTRL-C (SIGINT) will abort the hiding process.

Create a

branch from your stash If the changes in your

branch differ from the changes in your stash, You may have conflicts when blowing up or applying your stash. Instead, you can use the git

stash branch to create a new branch to apply hidden changes to:

This extracts a new branch based on the confirmation from which you created your cache, and then places the saved changes into it.

Cleaning your

stash

If you decide you no longer need a particular stash, You can remove it

with git stash drop: Or you can

delete all your stash with:

How git

stash works

If you just wanted to know how to 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!

The caches are actually encoded in your repository as commit objects. The special reference in .git/refs/stash points to your most recently created stash, and previously created stash is referenced by the stash reflog. This is why you refer to caches by stash@{n}: you actually mean the umpteenth reflog entry for the hiding place reference. Since a stash is just a commit, you can inspect it with git log:

depending on what you’ve saved, a single git stash operation creates two or three new commits. The commits in the diagram above are:

stash@{0}, a new commit to store the crawled files that were in your working copy when you ran the first git parent stash stash@{0}, the pre-existing commit that was in HEAD when you ran git stash the

  • second parent of stash@{0}, a new commit that represents the index when you ran git stash
  • The third parent of stash@{0}, a new commit representing the untracked files that were in his working copy when he ran git stash. This third parent was only created if:
    • your working copy actually contained untracked files; and
    • you

    • specified the -include-untracked or -all option when you invoked git stash.

How git stash encodes your work tree and index as commits:

  • Before saving, your work tree can contain changes to crawled files, untracked files, and ignored files. Some of these changes may also be in the index.

  • The git stash invocation encodes any changes to tracked files as two new commits to the DAG: one for non-preconfigured changes and one for interim changes to the index. The special reference refs/stash is updated to point to them.

  • Using the –

  • include-untracked option also encodes any changes to untracked

  • files as an additional confirmation. Using the -all option includes changes to

  • ignored files along with changes to untracked files in the same commit.

    When you run git stash pop, changes from previous commits are used to update your working copy and index, and the fallback record is shuffled to remove the pop-up commit.

Note that pop-up commits are not deleted immediately, but become candidates for future garbage collection.

Contact US