I frequently find myself wanting to checkout only certain files in a tree but cant because I have modified them locally and dont want the hassle of trying to figure out merge (I dont want to merge anything – I just want the git version of certain files).
So how can I force a checkout of, for example, “db-backup*” where these files are scattered over a directory structure?
git-parent - dir1 - db-backup1 - dir2 - db-backupA
There are a few possible solutions, depending on what you want to do
If you want to discard your changes
, you can use
git checkout -f ...
If you want to save your changes for later, but not worry about them for now, you can stash away your changes
and then do a checkout:
git stash git checkout ...
To recover your changes, use
git stash pop
git stash apply
If you want to simply view Git version of a file
(and perhaps save it in temporary file, or under other name), you can use
If you want to checkout some directory
or subset of files in some other place
, you can either use
as described in “Examples” section of git-archive homepage:
git archive --format=tar --prefix=subdir/ HEAD:subdir | (cd /var/tmp/ && tar xf -)
Note that it would use version from HEAD (last commit), not from the index, though usually there would be no difference.
Another way of achieving that is to use
, though this can be considered hackery.