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
addthe files with changes you want to keep, thenstashthe 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.cppstaged 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.