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)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With