git pull 而不是在 git 目录中 [英] git pull while not in a git directory

查看:37
本文介绍了git pull 而不是在 git 目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个目录 /X/Y,它是一个 git 存储库.是否可以以某种方式从 /X 内部调用像 git pull 这样的命令,但针对 /X/Y 目录?

Let's say I have a directory, /X/Y, which is a git repository. Is it possible to somehow call a command like git pull from inside /X, but targeting the /X/Y directory?

我想我特别想知道:是否可以使用 git 命令执行此操作,但无需更改目录?

I guess I was wondering specifically: is it possible to do this using the a git command, but without having to change directories?

注意:我已接受 VonC 的答案 因为它比以前的选项优雅得多.对于运行 Git 1.8.5 以上版本的用户,请参阅 bstpierre 的回答如下.

NOTE: I've accepted VonC's answer as it's much more elegant than previous options. For people running Git older than 1.8.5, please see bstpierre's answer below.

推荐答案

git 1.8.5(2013 年第 4 季度),您将能够使用 Git 命令,但无需更改目录".

Starting git 1.8.5 (Q4 2013), you will be able to "use a Git command, but without having to change directories".

就像 "make -C ", "git -C ..." 告诉 Git 之前去那里做其他事情.

Just like "make -C <directory>", "git -C <directory> ..." tells Git to go there before doing anything else.

参见 commit 44e1e4 Nazri Ramliy:

See commit 44e1e4 by Nazri Ramliy:

在不离开当前目录的情况下,在不同目录中调用 Git 命令需要更多的按键:

It takes more keypresses to invoke Git command in a different directory without leaving the current directory:

  1. (cd ~/foo && git status)
    git --git-dir=~/foo/.git --work-tree=~/foo 状态
    GIT_DIR=~/foo/.git GIT_WORK_TREE=~/foo git status
  2. (cd ../..; git grep foo)
  3. for d in d1 d2 d3;做 (cd $d && git svn rebase);完成

上面显示的方法对于脚本编写是可以接受的,但对于快速命令行调用来说太麻烦了.

The methods shown above are acceptable for scripting but are too cumbersome for quick command line invocations.

有了这个新选项,可以用更少的按键完成上述操作:

With this new option, the above can be done with fewer keystrokes:

  1. git -C ~/foo status
  2. git -C ../.. grep foo
  3. for d in d1 d2 d3;做 git -C $d svn rebase;完成

<小时>

自 Git 2.3.4(2015 年 3 月)以来,提交 6a536e2href="https://github.com/KarthikNayak" rel="noreferrer">Karthik Nayak (KarthikNayak),git 会处理git -C ''" 作为 no-op 当 为空时.


Since Git 2.3.4 (March 2015), and commit 6a536e2 by Karthik Nayak (KarthikNayak), git will treat "git -C '<path>'" as a no-op when <path> is empty.

'git -C ""' 以错误Cannot change to ''"而无用地死亡,而 shell 将 cd ""' 视为无操作.
以 shell 的行为为先例,教导 git 将 -C ""' 也视为无操作.

'git -C ""' unhelpfully dies with error "Cannot change to ''", whereas the shell treats cd ""' as a no-op.
Taking the shell's behavior as a precedent, teach git to treat -C ""' as a no-op, as well.

<小时>

4 年后,Git 2.23(2019 年第 3 季度)证明git -C """有效且不会更改目录

从 6a536e2 开始就一直这样(git:treat "git -C '<path>'"当 为空时作为无操作,2015-03-06,Git v2.3.4).

It's been behaving so since 6a536e2 (git: treat "git -C '<path>'" as a no-op when <path> is empty, 2015-03-06, Git v2.3.4).

这意味着文档现在(最后)包括:

That means the documentation now (finally) includes:

如果 '' 存在但为空,例如-C "",则当前工作目录保持不变.

If '<path>' is present but empty, e.g. -C "", then the current working directory is left unchanged.

<小时>

您可以看到 git -C 与 Git 2.26(2020 年第一季度)一起使用.


You can see git -C used with Git 2.26 (Q1 2020), as an example.

参见 commit b441717提交 9291e63, commit 9291e635236fce、提交 10812c2commit 62d58cd, 提交 b87b02c提交 9b92070commit 3595d10, commit f511bc0, 提交 f6041ab提交 f46c243commit 99c049b, 提交 3738439提交 7717242commit b8afb90(2019 年 12 月 20 日)由 刘丹顿 (Denton-L).
(由 Junio C Hamano 合并 -- gitster --commit 381e8e9,2020 年 2 月 5 日)

See commit b441717, commit 9291e63, commit 5236fce, commit 10812c2, commit 62d58cd, commit b87b02c, commit 9b92070, commit 3595d10, commit f511bc0, commit f6041ab, commit f46c243, commit 99c049b, commit 3738439, commit 7717242, commit b8afb90 (20 Dec 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit 381e8e9, 05 Feb 2020)

签字人:Denton Liu

之前,我们运行 test_must_fail full_name.但是,test_must_fail 应该只用于 git 命令.
内联 full_name() 以便我们可以直接在 git 命令上使用 test_must_fail.

Before, we were running test_must_fail full_name. However, test_must_fail should only be used on git commands.
Inline full_name() so that we can use test_must_fail on the git command directly.

full_name()28fb84382b (引入 @{upstream} 符号",2009-09-10,Git v1.7.0-rc0 -- merge),git -C 选项尚不可用(因为它是在 44e1e4d67d(git:在使用 -C 选项给出的目录中运行",2013-0, Git v1.8.5-rc0 -- merge 列在batch #5)).
因此,辅助函数消除了每次手动cd 的需要.但是,由于 git -C 现在可用,我们可以直接使用它并内联 full_name().

When full_name() was introduced in 28fb84382b ("Introduce <branch>@{upstream} notation", 2009-09-10, Git v1.7.0-rc0 -- merge), the git -C option wasn't available yet (since it was introduced in 44e1e4d67d ("git: run in a directory given with -C option", 2013-09-09, Git v1.8.5-rc0 -- merge listed in batch #5)).
As a result, the helper function removed the need to manually cd each time. However, since git -C is available now, we can just use that instead and inline full_name().

这篇关于git pull 而不是在 git 目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆