Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stashing only staged changes in git - is it possible?

Tags:

git

git-stash

Is there a way I can stash just my staged changes? The scenario I'm having issues with is when I've worked on several bugs at a given time, and have several unstaged changes. I'd like to be able to stage these files individually, create my .patch files, and stash them away until the code is approved. This way, when it's approved I can stash my entire (current) session, pop that bug and push the code.

Am I going about this the wrong way? Am I misunderstanding how git can work in other ways to simplify my process?

like image 270
MrDuk Avatar asked Feb 07 '13 19:02

MrDuk


People also ask

What does stashing changes mean in git?

git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on.

What kind of uncommitted changes can be stashed?

The stash command takes the uncommitted changes in your working directory, both the updated tracked files and staged changes, and saves them. However, the changes aren't finished, and you need to switch to a different branch to quickly fix a bug before continuing on with the current feature.

How do you stash only changes?

Stash a specific file Using the previous commands, you have stashed all the tracked files in your current working directory. In some cases, you may want to stash a specific file in order to retrieve it later on. To stash a specific file, use the “git stash push” command and specify the file you want to stash.

Does git commit only commit staged changes?

The staging area When using git commit -a from the previous chapter, git commits all changes at once. To be more careful with committing files, git allows changes to be "staged" before saving them as a commit. That's what the git add actually does; it adds a file to the staging area.


2 Answers

Yes, It's possible with DOUBLE STASH

  1. Stage all your files that you need to stash.
  2. 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).
  3. Run git stash push -m "good stash"
  4. Now your "good stash" has ONLY staged files.

Now if you need unstaged files before stash, simply apply first stash (the one created with --keep-index) and now you can remove files you stashed to "good stash".

Enjoy

like image 85
Bartłomiej Semańczyk Avatar answered Sep 21 '22 13:09

Bartłomiej Semańczyk


With latest git you may use --patch option

git stash push --patch   # since 2.14.6  git stash save --patch   # for older git versions 

And git will ask you for each change in your files to add or not into stash.
You just answer y or n

UPD
Alias for DOUBLE STASH:

git config --global alias.stash-staged '!bash -c "git stash --keep-index; git stash push -m "staged" --keep-index; git stash pop stash@{1}"' 

Now you can stage your files and then run git stash-staged.
As result your staged files will be saved into stash.

If you do not want to keep staged files and want move them into stash. Then you can add another alias and run git move-staged:

git config --global alias.move-staged '!bash -c "git stash-staged;git commit -m "temp"; git stash; git reset --hard HEAD^; git stash pop"' 
like image 36
Eugen Konkov Avatar answered Sep 17 '22 13:09

Eugen Konkov