Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you tell git to stash the index only?

Tags:

git

I have just used "git add -p" to add a bunch of changes to the index, and I just realised that I missed a change that should've gone into the previous commit.

I can't commit --amend now because I've added all these new changes to the index, and I don't want to use 'git reset' to remove them all from the index as it will take ages to add them all back in again.

What I need is something like 'git stash' that will only stash the index - it should leave the working files alone. Then I can stash the index, add the missing change, commit it, then pop the stash and have my index back the way it was.

It doesn't look like 'git stash' is able to do this, but am I missing something? Thanks!

like image 334
Malvineous Avatar asked Mar 12 '11 09:03

Malvineous


People also ask

How do I stash only changes in git?

To stash a specific file, use the “git stash push” command and specify the file you want to stash. However, the other tracked files that may be modified in your current working directory are untouched.

How do I select a git stash?

To pop a specific stash in git, you can use the git stash apply command followed by the stash@{NUMBER} command. command. It will show the list of stashes you have saved.

How do I stash only staged files?

Stage all your files that you need to stash. Run git stash --keep-index . This command will create a stash with ALL of your changes (staged and unstaged), but will leave the staged changes in your working directory (still in state staged). Now your "good stash" has ONLY staged files.


1 Answers

The closest thing I've found is git stash --patch. It walks you through each of the changes to working tree and index letting you choose what to stash.

http://www.kernel.org/pub/software/scm/git/docs/git-stash.html

like image 153
Leif Gruenwoldt Avatar answered Oct 03 '22 04:10

Leif Gruenwoldt