本文翻译自:git pull while not in a git directory

Let's say I have a directory, /X/Y , which is a git repository. 假设我有一个目录/X/Y ,它是一个git存储库。 Is it possible to somehow call a command like git pull from inside /X , but targeting the /X/Y directory? 是否可以从/X内部调用诸如git pull类的命令,但以/X/Y目录为目标?

EDIT: I guess I was wondering specifically: is it possible to do this using the a git command, but without having to change directories? 编辑:我想我特别想知道:是否可以使用git命令来执行此操作,而不必更改目录?

NOTE: I've accepted VonC's answer as it's much more elegant than previous options. 注意:我已经接受VonC的答案,因为它比以前的选项要优雅得多。 For people running Git older than 1.8.5, please see bstpierre's answer below . 对于运行Git 1.8.5之前的版本的人,请参见下面的bstpierre答案


#1楼

参考:https://stackoom.com/question/lknu/不在git目录中时进行git-pull


#2楼

For anyone like me that was trying to do this via a drush (Drupal shell) command on a remote server, you will not be able to use the solution that requires you to CD into the working directory: 对于像我这样试图通过远程服务器上的drush(Drupal shell)命令执行此操作的人,您将无法使用需要CD进入工作目录的解决方案:

Instead you need to use the solution that breaks up the pull into a fetch & merge: 相反,您需要使用将提取分解为获取和合并的解决方案:

drush @remote exec git --git-dir=/REPO/PATH --work-tree=/REPO/WORKDIR-PATH fetch origin
drush @remote exec git --git-dir=/REPO/PATH --work-tree=/REPO/WORKDIR-PATH merge origin/branch

#3楼

This post is a bit old so could be there was a bug andit was fixed, but I just did this: 这篇文章有点老了,所以可能有一个错误并且已经修复,但是我只是这样做了:

git --work-tree=/X/Y --git-dir=/X/Y/.git pull origin branch

And it worked. 而且有效。 Took me a minute to figure out that it wanted the dotfile and the parent directory (in a standard setup those are always parent/child but not in ALL setups, so they need to be specified explicitly. 我花了几分钟时间弄清楚它想要点文件和父目录(在标准设置中,它们始终是父/子目录,但在所有设置中却不是,因此需要明确指定它们。


#4楼

This might be a similar problem, but you can also simply chain you commands. 这可能是一个类似的问题,但是您也可以简单地将命令链接起来。 eg 例如

On one line 一行

cd ~/Sites/yourdir/web;git pull origin master

Or via SSH. 或通过SSH。

ssh username@atyourserver.com -t "cd ~/Sites/thedir/web;git pull origin master"

#5楼

Starting git 1.8.5 (Q4 2013) , you will be able to "use a Git command, but without having to change directories". git 1.8.5(2013年第四季度)开始 ,您将能够“使用Git命令,而不必更改目录”。

Just like " make -C <directory> ", " git -C <directory> ... " tells Git to go there before doing anything else . 就像“ make -C <directory> ”, git -C <directory> ... ”告诉Git在执行其他操作之前先去那里

See commit 44e1e4 by Nazri Ramliy : 参见Nazri Ramliy的 commit 44e1e4

It takes more keypresses to invoke Git command in a different directory without leaving the current directory: 在不离开当前目录的情况下,需要更多的按键才能调用其他目录中的Git命令:

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

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; do git -C $d svn rebase; done

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 2.3.4(2015年3月)起,并通过Karthik Nayak( KarthikNayak 提交6a536e2<path>为空时 git将把“ git -C '<path>' ”视为no-op。

' git -C "" ' unhelpfully dies with error " Cannot change to '' ", whereas the shell treats cd ""' as a no-op. ' git -C "" '无故死于错误“ Cannot change to '' “,而shell将cd”“”视为无操作。
Taking the shell's behavior as a precedent, teach git to treat -C ""' as a no-op, as well. 以shell的行为为先例,教git将-C“”'也视为无操作。


4 years later, Git 2.23 (Q3 2019) documents that ' git -C "" ' works and doesn't change directory 4年后,Git 2.23(Q3 2019)证明' git -C "" '有效并且不会更改目录

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). 自6a536e2起它一直在运行( git :当<path>为空时将“ git -C '<path>' ”视为no-op,2015-03-06,Git v2.3.4)。

That means the documentation now (finally) includes: 这意味着现在(最终) 的文档包括:

If ' <path> ' is present but empty, eg -C "" , then the current working directory is left unchanged. 如果存在' <path> '但为空,例如-C "" ,则当前工作目录保持不变。


#6楼

As some of my servers are on an old Ubuntu LTS versions, I can't easily upgrade git to the latest version (which supports the -C option as described in some answers). 由于我的某些服务器在旧的Ubuntu LTS版本上,因此我无法轻松将git升级到最新版本(如某些答案中所述,它支持-C选项)。

This trick works well for me, especially because it does not have the side effect of some other answers that leave you in a different directory from where you started. 这个技巧对我来说效果很好,特别是因为它没有其他一些答案的副作用,这些其他答案使您与开始时所在的目录不同。

pushd /X/Y
git pull
popd

Or, doing it as a one-liner: 或者,将其作为单线执行:

pushd /X/Y; git pull; popd

Both Linux and Windows have pushd and popd commands. Linux和Windows都具有push和popd命令。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐