views:

40

answers:

2

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?

e.g

git-parent
  - dir1
    - db-backup1
  - dir2
    - db-backupA

thanks,

r.

A: 

git checkout <filename> works fine for me even if there are modifications to the file. What exactly are you getting as an error, and what command are you trying to run? This should probably work:

find . -name 'db-backup*' | xargs git checkout --
Amber
+2  A: 

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 <paths>...
    
  • 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 <paths>...
    

    To recover your changes, use git stash pop (or 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 git show for that:

    git show <file>
    
  • If you want to checkout some directory or subset of files in some other place, you can either use git archive 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 --temp option of git checkout-index, though this can be considered hackery.

HTH

Jakub Narębski