Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute a Git command without being in the repository?

Tags:

git

People also ask

Can I run git commands in GitHub?

Basic Git commands To use Git, developers use specific commands to copy, create, change, and combine code. These commands can be executed directly from the command line or by using an application like GitHub Desktop.


Use -C as the first argument to git:

git -C /home/repo log

Per the docs, the effect of this is:

-C <path>

Run as if git was started in <path> instead of the current working directory. ...

This is almost equivalent to --git-dir and --work-tree without appending the usual .git folder. However, the options --git-dir and --work-tree do not exist to access the repository from outside the work tree; they are used to move the .git somewhere else, and they are much more complicated to use in some cases.

For instance, to get the log of /home/repo/subdir only:

git -C /home/repo/subdir log .

or

git -C /home/repo log subdir

It is not possible to use log . with --git-dir or --work-tree. The path must be processed to extract the subpath relative to the top of the work tree, and even in that case, git will not recognize it as a path if you do not use the -- option, so the only possible way is:

git --git-dir /home/repo/.git log -- subdir

Furthermore, --work-tree does not work at all with the log subcommand with my version (git 1.9.1). It is just ignored:

git --git-dir /home/repo/.git --work-tree /home/repo/subdir log -- subdir
git --git-dir /home/repo/.git --work-tree /home/repo/whatever log -- subdir

I do not even understand if this is a bug or a feature... as usual with many git design choices.


Try:

git --git-dir=/home/repo/.git log

It is important to give the path all the way up to the .git directory of your repository. Otherwise you will get only an error message that says something like:

fatal: Not a git repository

In fact you need to use --git-dir and --work-tree together. Here is an example:

local [] Desktop: mkdir git
local [] Desktop: cd git
local [] git: touch README.txt
local [] git: git init
Initialized empty Git repository in /Users/albert/Desktop/git/.git/
local [] git: cd ..
local [] Desktop: git --work-tree=git --git-dir=git/.git add .
local [] Desktop: git --work-tree=git --git-dir=git/.git commit -a -m 'initial commit, called from outside the git directory'
[master (root-commit) ee951b1] initial commit, called from outside the git directory
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.txt
local [] Desktop: cd git
local [] git: git log --pretty=oneline
ee951b161053e0e0948f9e2a36bfbb60f9c87abe initial commit, called from outside the git di

I have tried many times! I finally got it!

git -C dir --no-pager log --format='%an' -1 filename

do remember, please don't add .git to your

-C dir


For any git command, you can do:

git --git-dir=<PATH-TO-REPO>/.git --work-tree=<PATH-TO-REPO> <git-command>

For example, if you want to do a git status:

 git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/  status

or if you want to check the branch the repo is in:

git --git-dir=/home/myrepo/.git --work-tree=/home/myrepo/  rev-parse --abbrev-ref HEAD