Run `git commit` from another directory

How to run git commit -m '...' command from another directory?

I am editing a file:

 vim /home/.../myFile 

I add it using:

 git add /home/.../myFile 

But now, how can I commit changes?

 git commit -m '...' ??? 
+7
source share
3 answers

You can also use the $GIT_DIR and $GIT_WORK_TREE environment variables for the same effect as --git-dir and --work-tree

These are the steps to commit the file when you are in a different directory:

 git --git-dir=/path/to/my/directory/.git/ --work-tree=/path/to/my/directory/ add myFile git --git-dir=/path/to/my/directory/.git/ --work-tree=/path/to/my/directory/ commit -m 'something' 
+15
source

You can use git -C <path> . From https://git-scm.com/docs/git/2.4.4

git -C <path>: Run as if git was running in <path> instead of the current working directory.

+6
source

After fixing everything with the --git-dir option, where several files were deleted from --git-dir , I had to restore them. For me, the best solution was in a script to make cd to another folder and then execute git commands. There was much less code to write without parameters in each command.

0
source

All Articles