git cmds outside present directory | git -C option

Git Commands | From Another Folder

When you need to execute git commands with absolute paths (from another folder) – you turn to 3 command switches.

--git-dir --work-tree and git -c

  1. --git-dir – specify the .git directory to use as the repository
  2. --work-tree – specify the git working copy path
  3. git -C – specify file or http url (no need for .git)

These switches can be used within most git commands. These git dir commands are most commonly used within scripts. When humans type git commands from within a working copy directory assumptions are made. Use these commands when you can’t (or won’t) make assumptions about the present working directory.

Git Command Examples | Absolute Paths

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


git clone http://host-name/commons/project_name ~/path/to/working/copy/root


git clone ~/path/to/working/copy/.git ~/path/to/working/copy/root


git --git-dir=~/path/to/working/copy/.git --work-tree=c/b status

Her the git checkout command is told about the source and target.
git --git-dir=~/path/to/working/copy/.git --work-tree=./foo-copy checkout branch_name

Git | The -C Option

The git -C option negates the need to specify the .git extension.

However – you must put git -C before any other commands.

git -C ~/path/to/working/copy checkout master

or

git -C http://host-name/commons/prj_name/ checkout master

Git Branch Command

You can query the branch of a local git repo folder from outside.

Leave a Reply

Your email address will not be published. Required fields are marked *