Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I git add a file without changing the current working directory?

Tags:

git

I want to add and commit a file in git without changing my current working directory. Is this possible?

> pwd
/tmp 

> git --git-dir=/tmp/git_test/.git init
Initialized empty Git repository in /tmp/git_test/.git/

> ls /tmp/git_test
commit1

> git --git-dir=/tmp/git_test/.git add /tmp/git_test/commit1
fatal: '/tmp/git_test/commit1' is outside repository

> git --git-dir=/tmp/git_test/.git add commit1
fatal: pathspec 'commit1' did not match any files

(git add -A seems to use the current working directory, rather than the argument to --git-dir)

like image 954
dnw Avatar asked Mar 21 '12 01:03

dnw


1 Answers

You missed an option: --work-tree. If you're outside the repository, you need to supply both that and --git-dir:

--work-tree=<path>

Set the path to the working tree. It can be an absolute path or a path relative to the current working directory. This can also be controlled by setting the GIT_WORK_TREE environment variable and the core.worktree configuration variable (see core.worktree in git-config(1) for a more detailed discussion).

like image 92
Cascabel Avatar answered Oct 06 '22 17:10

Cascabel