如何在不改变当前工作目录的情况下添加文件? [英] How can I git add a file without changing the current working directory?
问题描述
我想在git中添加并提交一个文件,而无需更改当前的工作目录。这可能吗?
> pwd
/ tmp
> git --git-dir = / tmp / git_test / .git init
在/tmp/git_test/.git/中初始化为空的Git存储库
> ls / tmp / git_test
commit1
> git --git-dir = / tmp / git_test / .git add / tmp / git_test / commit1
致命:'/ tmp / git_test / commit1'在外部存储库
> git --git-dir = / tmp / git_test / .git add commit1
fatal:pathspec'commit1'与任何文件不匹配
(git add -A似乎使用当前工作目录,而不是参数--git-dir)
您错过了一个选项: - work-tree
。如果您不在存储库中,则需要同时提供
$ b树。它可以是相对于当前工作目录的绝对路径或路径。这也可以通过设置GIT_WORK_TREE环境变量和core.worktree配置变量来进行控制(有关更详细的讨论,请参阅git-config(1)中的core.worktree。)
- work-tree =<路径>
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).
这篇关于如何在不改变当前工作目录的情况下添加文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!