I have a large git project that I, stupidly, imported to eclipse and ran an autoformat on. Now, every file in the project is showing as modified. Rather than commit my formatted files, I would rather revert all the files that I have only been formatted and not had other changes. For instance:
$ git status
# On branch master
# 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)
# (commit or discard the untracked or modified content in submodules)
# modified: dir/file1.cpp
# modified: dir/file1.h
# modified: dir/file2.cpp
# modified: dir/file2.h
# modified: dir/file3.cpp
# modified: dir/file3.h
# modified: dir/file4.cpp
# modified: dir/file4.h
I know that file2.cpp
, file2.h
, and file3.cpp
have been modified with content (i.e., not just formatted). I want to stash the changes to these three files and then checkout an old revision, so that I can reapply the changes to these files after. I would rather avoid something like:
$ cp file2.cpp ~/tmp
$ git checkout blahblahblah
$ cp ~/tmp/file2.cpp .
If there's an obvious way to do this that doesnt involve stashing, let me know. whatever gets the job done.
You can
add
the files with changes you want to keep, thenstash
the rest of the files and clear the stash:At this point, you've stashed your unwanted changes. If you'd like to permanently get rid of them, run:
Now you have
file2.cpp
,file2.h
, andfile3.cpp
staged for commit. If you then want to stash these files (and not commit them):Now you'll be at your previous commit, with only those three files stashed.
Update:
Git 2.13 and later includes a more direct way to stash specific files with
git stash push
, as VonC explains in his answer.With Git 2.13 (Q2 2017), git stash will have officially a way to stash changes for specific files with
See commit 9e14090, commit 1ada502, commit df6bba0 (28 Feb 2017), and commit 9ca6326, commit 6f5ccd4, commit f5727e2 (19 Feb 2017) by Thomas Gummerer (
tgummerer
).(Merged by Junio C Hamano --
gitster
-- in commit 44c3f09, 10 Mar 2017)As now documented:
Note, as pointed out by medmunds in the comments, that
git stash
would use paths relative to the root folder of the git repo.A nice option is using the interactive stash mode.
It works mostly like the interactive add mode: you are going to be presented with a series of diffs showing the changes you have in your working tree and you have to choose which files (or only certain parts of a file!) you want to stash, the rest will be left intact.
From
man git-stash
:In your case, you will be able to see formatting-only hunks and stash them individually, without loosing your meaningful changes.
You can also use git stash -p. This way you can select which hunks should be added to stash, whole files can be selected as well.
You'll be prompted with a few actions for each hunk:
That's a good use for
git diff
andgit apply
IMO:After
diff
, you can inspect the patch file to make sure that all your changes are there.Note that you may need to use the
--reject
option to apply, in case the patch does not apply cleanly. Also see the man page for apply.