且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不改变当前工作目录的情况下添加文件?

更新时间:2023-08-25 13:28:22

您错过了一个选项: - work-tree 。如果您不在存储库中,则需要同时提供 - git-dir :



$ b

- work-tree =<路径>

树。它可以是相对于当前工作目录的绝对路径或路径。这也可以通过设置GIT_WORK_TREE环境变量和core.worktree配置变量来进行控制(有关更详细的讨论,请参阅git-config(1)中的core.worktree。)


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).