git pull 而不是在 git 目录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5083224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git pull while not in a git directory
提问by Gavin Anderegg
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?
假设我有一个目录/X/Y
,它是一个 git 存储库。是否有可能以某种方式调用类似git pull
from inside的命令/X
,但针对/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 answeras it's much more elegant than previous options. For people running Git older than 1.8.5, please see bstpierre's answer below.
注意:我接受了VonC 的回答,因为它比以前的选项优雅得多。对于运行早于 1.8.5 的 Git 的人,请参阅下面 bstpierre 的回答。
回答by VonC
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 (Q4 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 44e1e4by Nazri Ramliy:
It takes more keypresses to invoke Git command in a different directory without leaving the current directory:
(cd ~/foo && git status)
git --git-dir=~/foo/.git --work-tree=~/foo status
GIT_DIR=~/foo/.git GIT_WORK_TREE=~/foo git status(cd ../..; git grep foo)
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:
git -C ~/foo status
git -C ../.. grep foo
for d in d1 d2 d3; do git -C $d svn rebase; done
在不离开当前目录的情况下,在不同目录中调用 Git 命令需要更多的按键:
(cd ~/foo && git status)
git --git-dir=~/foo/.git --work-tree=~/foo status
GIT_DIR=~/foo/.git GIT_WORK_TREE=~/foo git status(cd ../..; git grep foo)
for d in d1 d2 d3; do (cd $d && git svn rebase); done
上面显示的方法对于脚本编写是可以接受的,但是对于快速命令行调用来说太麻烦了。
使用这个新选项,可以通过更少的击键完成上述操作:
git -C ~/foo status
git -C ../.. grep foo
for d in d1 d2 d3; do git -C $d svn rebase; done
Since Git 2.3.4 (March 2015), and commit 6a536e2by Karthik Nayak (KarthikNayak
), git
will treat "git -C '<path>'
" as a no-op when <path>
is empty.
由于Git的2.3.4(2015年3月),并提交6a536e2通过KARTHIK纳亚克(KarthikNayak
),git
将把“ git -C '<path>'
”作为一个在无操作<path>
是空的。
'
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, teachgit
to treat -C ""' as a no-op, as well.
'
git -C ""
' 无益地死于错误“Cannot change to ''
”,而 shell 将 cd ""' 视为无操作。
以 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(2019 年第 3 季度)记录了“ 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
:将“git -C '<path>'
”视为<path>
空操作,2015 年 3 月 6日,Git v2.3.4)。
That means the documentationnow (finally) includes:
这意味着文档现在(最终)包括:
If '
<path>
' is present but empty, e.g.-C ""
, then the current working directory is left unchanged.
如果 '
<path>
' 存在但为空,例如-C ""
,则当前工作目录保持不变。
You can see git -C
used with Git 2.26 (Q1 2020), as an example.
例如,您可以看到git -C
与 Git 2.26(2020 年第一季度)一起使用。
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)
见提交b441717,提交9291e63,提交5236fce,提交10812c2,提交62d58cd,提交b87b02c,提交9b92070,提交3595d10,提交f511bc0,提交f6041ab,提交f46c243,提交99c049b,提交3738439,提交7717242,提交b8afb90(二零一九年十二月二十零日)由丹顿刘(Denton-L
)。
(由Junio C gitster
Hamano合并-- --在提交 381e8e9 中,2020 年 2 月 5 日)
t1507
: inlinefull_name()
Signed-off-by: Denton Liu
Before, we were running
test_must_fail full_name
. However,test_must_fail
should only be used on git commands.
Inlinefull_name()
so that we can usetest_must_fail
on thegit
command directly.When
full_name()
was introduced in 28fb84382b("Introduce<branch>@{upstream}
notation", 2009-09-10, Git v1.7.0-rc0 -- merge), thegit -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 -- mergelisted in batch #5)).
As a result, the helper function removed the need to manuallycd
each time. However, sincegit -C
is available now, we can just use that instead and inlinefull_name()
.
t1507
: 排队full_name()
签字人:Denton Liu
之前,我们正在运行
test_must_fail full_name
. 但是,test_must_fail
应该只用于 git 命令。
内联full_name()
以便我们可以直接test_must_fail
在git
命令上使用。当
full_name()
在28fb84382b(“Introduce<branch>@{upstream}
notation”,2009-09-10,Git v1.7.0-rc0 -- merge)git -C
中引入时,该选项尚不可用(因为它是在44e1e4d67d中引入的(“git
:在给定的目录中运行)带有 -C 选项”,2013-09-09,Git v1.8.5-rc0 --合并列在第 5 批中))。
因此,辅助函数消除了cd
每次手动的需要。但是,由于git -C
现在可用,我们可以使用它来代替 inlinefull_name()
。
回答by bstpierre
Edit:
编辑:
There's either a bug with git pull
, or you can't do what you're trying to do with that command. You canhowever, do it with fetch and merge:
要么存在错误git pull
,要么您无法使用该命令执行您想要执行的操作。但是,您可以使用 fetch 和 merge 来完成:
cd /X
git --git-dir=/X/Y/.git fetch
git --git-dir=/X/Y/.git --work-tree=/X/Y merge origin/master
Original answer:
原答案:
Assuming you're running bash or similar, you can do (cd /X/Y; git pull)
.
假设您正在运行 bash 或类似程序,您可以执行(cd /X/Y; git pull)
.
The git man pagespecifies some variables (see "The git Repository") that seem like they should help, but I can't make them work right (with my repository in /tmp/ggg2):
在git的手册页指定一些变量(请参阅“Git仓库”),似乎像他们应该帮助,但我不能让他们的工作权利(与我的仓库在/ tmp / ggg2):
GIT_WORK_TREE=/tmp/ggg2 GIT_DIR=/tmp/ggg2/.git git pull
fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.
Running the command below while my cwd is /tmp updates that repo, but the updated file appears in /tmp instead of the working tree /tmp/ggg2:
当我的 cwd 是 /tmp 时运行下面的命令更新该 repo,但更新的文件出现在 /tmp 而不是工作树 /tmp/ggg2:
GIT_DIR=/tmp/ggg2/.git git pull
See also this answer to a similar question, which demonstrates the --git-dir
and --work-tree
flags.
另请参阅对类似问题的回答,其中演示了--git-dir
和--work-tree
标志。
回答by takeshin
You may wrap it in a bash script or git alias:
您可以将其包装在 bash 脚本或 git 别名中:
cd /X/Y && git pull && cd -
回答by samtresler
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.
它奏效了。我花了一分钟才弄清楚它想要 dotfile 和父目录(在标准设置中,它们总是父/子,但不是在所有设置中,因此需要明确指定它们。
回答by IvanD
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 都有 pushd 和 popd 命令。
回答by Raman Sahasi
Using combination pushd
, git pull
and popd
, we can achieve this functionality:
使用组合pushd
,git pull
和popd
,我们可以实现这个功能:
pushd <path-to-git-repo> && git pull && popd
For example:
例如:
pushd "E:\Fake Directory\gitrepo" && git pull && popd
回答by jonescb
You can write a script like this:
您可以编写这样的脚本:
cd /X/Y
git pull
You can name it something like gitpull
.
If you'd rather have it do arbitrary directories instead of /X/Y
:
您可以将其命名为gitpull
.
如果你宁愿让它做任意目录而不是/X/Y
:
cd
git pull
Then you can call it with gitpull /X/Z
Lastly, you can try finding repositories. I have a ~/git
folder which contains repositories, and you can use this to do a pull on all of them.
然后你可以用gitpull /X/Z
最后调用它,你可以尝试查找存储库。我有一个~/git
包含存储库的文件夹,您可以使用它来拉取所有存储库。
g=`find /X -name .git`
for repo in ${g[@]}
do
cd ${repo}
cd ..
git pull
done
回答by dkinzer
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
回答by John Ballinger
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 [email protected] -t "cd ~/Sites/thedir/web;git pull origin master"