Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run `git commit` from another directory

Tags:

git

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

I edit my file:

vim /home/.../myFile

I add it using:

git add /home/.../myFile

But now, how can I commit the changes?

git commit -m '...' ???
like image 573
Ionică Bizău Avatar asked Mar 27 '14 15:03

Ionică Bizău


People also ask

How do I commit a directory in git?

Enter git add --all at the command line prompt in your local project directory to add the files or changes to the repository. Enter git status to see the changes to be committed. Enter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository.

How do you apply a commit from another repo?

In order to get commits from the other repository, You'll need to add the other repository as a remote, then fetch its changes. From there you see the commit and you can cherry-pick it.

How add and commit simultaneously?

git add -A && git commit -m' once and every time I just type: git ac 'comment' :) git config --global alias. add-commit "! git add -A && git commit" (i.e. double quotes instead of single quotes) is what works on Windows.


2 Answers

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

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

like image 134
Oded Peer Avatar answered Oct 25 '22 13:10

Oded Peer


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

These are the steps to commit a file when you are in another 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'
like image 38
l4rd Avatar answered Oct 25 '22 12:10

l4rd