Bostonian
Bostonian

Reputation: 687

How to temporarily remove the un-staged changes and then recover them afterwards

I have some un-staged changes and I would like to temporarily discard them to start a build without them.

After the build, I would like to recover those un-staged changes, what could I achieve this? Thank you.

Upvotes: 0

Views: 152

Answers (2)

CodeWizard
CodeWizard

Reputation: 142164

Note: You can use stash@{<revision>} to execute the command on any desired any stash

Stash commands

git stash (no parameters)

Stash with untracked files

$ git stash -u
$ git stash [save] -u

git stash pop

# Remove a single stashed state from the stash list and apply it on top
# of the current working tree stat

$ git stash pop

# Pop a specific stash    
$ git stash pop stash@{2}

git stash list

# List all stashes     
$ git stash list

git stash drop

# Drop a specific stash
$ git stash drop stash@{1}

# Drop all stashes
$ git stash clear

git stash apply

# Like pop, but **does not remove** the stash from the stash list

git stash show

# Show the changes recorded in the stash as a diff between 
# the stashed state and its original parent

$ git stash show

For example:

View the stash content

# display on the stash content
git stash show stash@{1} -u

# checkout the desired commit and choose the right action you want
git checkout -p stash@{0} -p <path to file>

enter image description here

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521427

One option would be to use git stash, but in this case just create a stash commit based only on the working directory:

git stash save --keep-index

This would not touch your stage, should you already have any files in it. After you have finished the build and you wish to apply the stash to your working directory, use:

git stash apply

Upvotes: 2

Related Questions