Working on git repo without cd into directory

How would I run git commands on a repo when I am not cd’d into that directory?
i.e. git branch /repos/myrepo.git

  • Gitignore ignoring file that I want included
  • Get the output of git show-ref --tags in a hash using Perl
  • delete first git commit
  • Git commit policy that isolates changes to specific files as separate commits
  • Unable to remove files recursively from Git
  • Undo a merge after time, what happens?
  • How to get 'build when a change is pushed to github' option when creating new job
  • Heroku Create Command Yielding “ENOENT” Error
  • Git - Forgot to switch branch
  • Accidentally merged github repo into personal project
  • Managing hotfixes when develop branch is very different from master?
  • Project architecture with CocoaPods - Private main-project and public sub-project
  • 3 Solutions collect form web for “Working on git repo without cd into directory”

    --git-dir=<path>

    Set the path to the repository. This can also be controlled by setting the GIT_DIR environment variable. It can be an absolute path or relative path to current working directory.

    http://www.kernel.org/pub/software/scm/git/docs/git.html

    Note that <path> above means the path to the actual git directory (project_dir/.git) not just the project directory (project_dir).

    Starting with git 1.8.5, use the -C option.

    git -C "/Users/michael/Development/Projects/opensource/dvsc-backup" status
    

    Otherwise, you have to specify --work-dir as well as --git-dir

    git --work-dir="/Users/michael/Development/Projects/opensource/dvsc-backup" --git-dir="/Users/michael/Development/Projects/opensource/dvsc-backup/.git" status
    

    –work-tree might do better 😉

    Git Baby is a git and github fan, let's start git clone.