In git, how can I checkout out the HEAD version of my remote/tracking branch? Basically, I want to do a 'svn checkout ' in git.

I think the closest thing I find is 'git fetch', but from the man page, I don't know how to checkout 1 particular file using that?

+1  A: 

Git doesn't have a notion of retrieving a single file. It always moves commits across the network.

Once you've done a git clone of some remote repository, you can git checkout remotename/branch to have the latest file versions on that branch checked out in your working copy. If you want to modify them, you should git checkout -b <yourbranchname> remotename/branch to start a local branch. This might be better done as

git branch --track mybranchname remotename/branchname
git checkout mybranchname

So that your local branch will "track" the remote branch, and default to pushing and pulling from that branch.

+3  A: 

First, a note: remote repository can have more than one branch; also HEAD means (for local development and local branches) currently checked out branch, and for remote the remote-tracking branch (symbolic reference) remote/HEAD means default branch on remote. There isn't such thing like 'HEAD of branch'; HEAD is a pointer to branch (or sometimes to a commit).

Second, Git operates (as Novelocrat wrote) on whole tree level (all files in repository). In Git you checkout a branch (although you can checkout a file version from some branch to working directory too, it is less commkon operation).

In Git you can create new commit only on top of some local branch. You can't create commits directly on remote-tracking branches, as they are meant to follow branches of remote repository (and you would lose your work on fetch).

Therefore simple "git checkout origin" (assuming that remote is called origin), which is shortcut for "git checkout origin/HEAD", which is usually "git checkout origin/master" would checkout a state of remote-tracking branch into unnamed branch, so called detached HEAD. This is a good solution if you only want to see / browse remote-tracking branch state.

If you use "git checkout --track origin/master" would create local branch master, which is meant to follow (track) remote-tracking branch origin/master. Note that git-clone automatically sets up such local branch (usually master) for the default branch (origin/HEAD) of remote origin (usually origin/master), so you wouldn't have to do this. Then simple "git pull" when on branch master would fetch (if needed), and try to merge your local changes with the changes in respective branch in remote repository. If there were no changes in remote, it is "up to date" state and your local branch would not change; if there were no changes in your local branch but were in remote, then local branch is simply advanced to the state of remote-tracking branch, which is called "fast-forward". You can think of "git pull" here as of very, very rough equivalent of "svn update".

You can also force your local branch to rewind to state of remote-tracking branch with "git reset --hard origin". Note that --hard means here to force overwrite of working directory, so any uncomitted changes would be lost! Use with care.

Finally if you want to check out single file from remote-tracking branch (i.e. to have in working directory version of file as it is in remote-tracking branch), you can simply use the 'pathspec' form of git-checkout, namely "git checkout origin -- file". If you want to only see how the file looks like in remote-tracking branch, use "git show origin:path/to/file".


Jakub Narębski