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

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

问题描述

假设我有一个目录, / X / Y ,它是一个git仓库。是否有可能以某种方式从 / X 中调用像 git pull 这样的命令,但是以 / X / Y 目录?



编辑:我想我特别想知道:是否可以做这是使用git命令,但不必更改目录?



注意:我接受VonC的答案,因为它比以前的选项更优雅。对于运行Git超过1.8.5的人,请参阅 bstpierre的答案在下面

解决方案

开始 git 1.8.5(2013年第四季度),您将能够使用Git命令,但而不必改变目录。


就像 make -C< directory> git -C< directory> ... 告诉Git在做任何事情之前去那里


请参阅提交44e1e4 Nazri Ramliy


它需要更多按键来在不同目录下调用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; (cd $ d&& git svn rebase);完成



  4. 上面显示的方法可用于脚本编写,但对于快速命令行调用来说太麻烦。 / p>

    使用这个新选项,可以用更少的击键来完成上述操作:


    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月)和提交6a536e2 Karthik Nayak( KarthikNayak git < path> 时,$ c>会将 git -C'< path>' code>为空。


' git -C'unhelpfully die with error不能更改为'',而shell会将cd'视为无操作。

以shell的行为为先例,教导 git 将-C'视为无操作符。



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?

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

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.

解决方案

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

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

See commit 44e1e4 by Nazri Ramliy:

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

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

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