Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stash only some of the currently modified files [duplicate]

Tags:

git

git-stash

I have many changed files and would like to stash only some of the modified files.

As an example, my repository looks something like:

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

      modified: main.cpp
      modified: MyClass.h
      modified: MyClass.cpp

And I would like to stash MyClass.h and MyClass.cpp only.

I found this question but the accepted answer appears to be wrong and the most voted answer requires each hunk/file to be interactively added.

Is there an easy way to stash a specific set of files? Preferably in a single command without having to interactively select files one by one or stage/unstage things.

like image 970
Increasingly Idiotic Avatar asked Apr 18 '18 18:04

Increasingly Idiotic


1 Answers

I found git stash push can be used to easily stash multiple files.

To stash the files from the example the command would be

 git stash push MyClass.h MyClass.cpp

Which stashes MyClass.h and MyClass.cpp but leaves main.cpp alone.

Found in an edit to this answer

like image 193
Increasingly Idiotic Avatar answered Oct 17 '22 20:10

Increasingly Idiotic