tags:

views:

247

answers:

4

I'm new to git so this may be a silly question:

how do i checkout just one file from a git repo?

A: 

First clone the repo with the -n option, which suppresses the default checkout of all files

git clone -n git://path/to/the_repo.git

Then check out just the file you want like so:

cd the_repo
git checkout HEAD name_of_file
invariant
While literally this does check out a single file, it's almost certainly not what the OP wants to do, since they will have all of the files (and the checkout is a no-op anyway).
Jefromi
Noted, and updated.
invariant
I don't think this even works - with `-n` the work tree and index end up in sync. That is, all content shows up as deleted. You have to either `git reset HEAD` or `git checkout HEAD file`. It's also really difficult to work with the repository at this point unless you really understand how git works.
Jefromi
Yes, you are quite right. I gave the answer before I tried it myself. I was going to delete it, but with your useful additions I shall leave it now.
invariant
If it’s a one-time-only operation, you can say `--depth 1` in your clone call.
Debilski
All right, so with Debilski's addition, you can definitely do this, only fetching enough data to construct... the entire work tree in its current state. I think stefanB is probably closest to what the OP actually needs to know, namely, how to make changes to a single file with git.
Jefromi
A: 

It sounds like you're trying to carry over an idea from centralized version control, which git by nature is not - it's distributed. If you want to work with a git repository, you clone it. You then have all of the contents of the work tree, and all of the history (well, at least everything leading up to the tip of the current branch), not just a single file or a snapshot from a single commit.

 git clone /path/to/repo
 git clone git://url/of/repo
 git clone http://url/of/repo
Jefromi
A: 

In git you do not 'checkout' files before you update them - it seems like this is what you are after.

Many systems like clearcase, csv and so on require you to 'checkout' a file before you can make changes to it. Git does not require this. You clone a repository and then make changes in your local copy of repository.

Once you updated files you can do:

git status

To see what files have been modified. You add the ones you want to commit to index first with (index is like a list to be checked in):

git add .

or

git add blah.c

Then do git status will show you which files were modified and which are in index ready to be commited or checked in.

To commit files to your copy of repository do:

git commit -a -m "commit message here"

See git website for links to manuals and guides.

stefanB
And if your goal is to patch this single file and submit it back, you'll need to either push (but probably you don't have push access for this project?) or use `git format-patch` to create a patch for submission (`git format-patch -1` will create a patch for just your most recent commit).
Jefromi
+1  A: 

As mentioned in the other answers:

  • you must clone first the repo, meaning you get the full history:

    • in the .git repo
    • in the working tree.
  • But then you can do a sparse checkout (if you are using Git1.7+),:

    • adding what you want to see in the .git/info/sparse-checkout file
    • re-reading the working tree to only display what you need

To re-read the working tree:

$ git read-tree -m -u HEAD

That way, you end up with a working tree including precisely what you want (even if it is only one file)

VonC