Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 857
asolberg Avatar asked Jul 04 '14 16:07

asolberg


People also ask

How do I exclude files from git add?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.

What is git checkout --?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.

Can I pull only one file from git?

Short Answergit checkout origin/master -- path/to/file // git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).


1 Answers

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:

  1. Put files you need to preserve to index.
  2. Stash everything (possibly with a meaningful message), but leave index untouched.
  3. Drop stashed changes (only if you sure they are useless).
  4. 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:

  1. Command git to ignore some files.
  2. Checkout everything in the current directory.
  3. Tell git to treat files from 1 as usual again.
like image 84
xaizek Avatar answered Oct 04 '22 23:10

xaizek