git checkout branch from outside

eistrati picture eistrati · May 20, 2011 · Viewed 17.5k times · Source

Problem: I need somehow to checkout an existing branch of a project that is already cloned locally on my file system without being in that particular folder of this project.

Solution: I'm trying to do the following:

  1. git clone 'github-project-url' 'file-system-folder'
  2. git checkout 'existing-branch' 'file-system-folder'

I do realize that second step is not quite right, but I also am trying to avoid to cd 'file-system-folder'.

Answer

Brian Campbell picture Brian Campbell · May 20, 2011

You can use --git-dir to specify the .git directory to use as the repository, and --work-tree to specify the working tree to to the checkout in. See the git man page for details.

git --git-dir=file-system-folder/.git --work-tree=file-system-folder checkout existing-branch