Stash only some of the currently modified files

时间:2018-04-18 18:04:45

标签: 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.

1 个答案:

答案 0 :(得分:3)

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