Is there a way in git to checkout all files except 1 or 2?

git

Solution

I want to do the same occasionally and usually go with the following set of commands (assumes than there is nothing in the index):

git add file-to-preserve-1 file-to-preserve-2
git stash save --keep-index "Changes discarded because ..."
# git stash drop # think twice before running this command
git reset

In words:

- Put files you need to preserve to index.

- Stash everything (possibly with a meaningful message), but leave index untouched.

- Drop stashed changes (only if you sure they are useless).

- Reset the index.

Another way I can think of (didn't try it actually, the first one works fine for me):

git update-index --skip-worktree file-to-preserve-1 file-to-preserve-2
git checkout .
git update-index --no-skip-worktree file-to-preserve-1 file-to-preserve-2

Explained:

- Command git to ignore some files.

- Checkout everything in the current directory.

- Tell git to treat files from `1` as usual again.

Problem

Sometimes I find myself in the position where I want to dump everything in my working tree except for 1 or 2 files. Is there an easy way to do this? As it is I've been manually typing git checkout .... for all the files I want to checkout from the index and don't include the files I want to keep but that's pretty laborious. Another way I could think of doing this would be to stash 1 or 2 files I want to keep, then "checkout ." then restore the stash. Would that be a good way to do this?

Original source

Related problems