从不同的目录执行 git pull
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7622616/
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
executing a git pull from a different directory
提问by opensas
I'm configuring calimoucho(a little play continuos integration server), and for it to work I need to run a command to pull a cloned git hub repository from outside it.
我正在配置calimoucho(一个小游戏持续集成服务器),为了让它工作,我需要运行一个命令来从它外部提取一个克隆的 git hub 存储库。
to be more precise, I'll explain it with an example.
更准确地说,我会用一个例子来解释它。
I have the following repository
我有以下存储库
cd /home/sas
mkdir apps
cd apps
mkdir myApp
cd myApp
git init
echo "my file" > file
git add .
git commit -m "initial commit"
Just a silly test repository where my app is supossed to be
只是一个愚蠢的测试存储库,我的应用程序应该在其中
Now I need to clone that repository to a checkout folder.
现在我需要将该存储库克隆到结帐文件夹。
cd /home/sas
mkdir calimoucho
cd calimoucho
mkdir checkout
cd checkout
git clone /home/sas/apps/myApp/
so I have the following directory structure
所以我有以下目录结构
~/apps
myapp
.git
file
~/calimoucho
checkout
myapp
.git
file
The continuos integration server will have to pull new changes from ~/apps/myapp to ~/calimoucho/checkout/myapp, running a command line sentence from ~/calimoucho
持续集成服务器必须将新的更改从 ~/apps/myapp 拉到 ~/calimoucho/checkout/myapp,从 ~/calimoucho 运行命令行语句
I try with the following command
我尝试使用以下命令
~/calimoucho$ git --git-dir=/home/sas/apps/myApp/.git --work-tree=/home/sas/calimoucho/checkout/myApp/ pull
and I get the following error
我收到以下错误
fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.
if I don't specify the --work-tree option, the pull is issued, but changes are applied to ~/calimoucho folder instead of ~/calimoucho/checkout/myApp
如果我不指定 --work-tree 选项,则发出拉取,但更改应用于 ~/calimoucho 文件夹而不是 ~/calimoucho/checkout/myApp
any idea how to update the cloned repo from the ~/calimoucho folder?
知道如何从 ~/calimoucho 文件夹更新克隆的 repo 吗?
thanks a lot
多谢
回答by Danny
I had this question, too. I found the answer in the git documentation (https://git-scm.com/docs/git).
我也有这个疑问。我在 git 文档 ( https://git-scm.com/docs/git) 中找到了答案。
Run the command
运行命令
git -C <git-working-directory> pull <git remote>
The specific command to answer this question is
回答这个问题的具体命令是
git -C checkout/myApp/ pull
Note that it is important -C <git-working-directory>
comes before the pull
command and any additional pull options can be specified at the end of the command. In the example above, the git clone
command would have setup the default remote repository ~/apps/myapp so it is not required to specify the remote repository.
请注意,-C <git-working-directory>
在pull
命令之前很重要,并且可以在命令末尾指定任何其他拉取选项。在上面的示例中,该git clone
命令将设置默认远程存储库 ~/apps/myapp,因此不需要指定远程存储库。
回答by Lance
This one worked for me:
这个对我有用:
git --git-dir=/home/myuser/awesomeproject/.git --work-tree=/home/myuser/awesomeproject pull
I'm using it inside a post-update hook to automatically pull and restart pm2 on my test server.
我在更新后挂钩中使用它来自动拉取并重新启动我的测试服务器上的 pm2。
回答by Noam Manos
In case your git version doesn't like the git-working-directoryoption:
如果您的 git 版本不喜欢git-working-directory选项:
Unknown option: -C
未知选项:-C
Then use the push-directorycommand beforehand.
然后事先使用push-directory命令。
For example, this will pull your git project from parent directory (or clone if it doesn't exists):
例如,这将从父目录中拉取您的 git 项目(如果它不存在,则克隆):
pushd ./project-name && git pull && popd || git clone https://repo-url/project-name
回答by ryantm
You should not set the work-tree to a different repository than the git-dir variable. I think they are meant to be used when you don't want the .git folder to be in the same directory as your working tree. Instead try this:
您不应将工作树设置为与 git-dir 变量不同的存储库。我认为它们应该在您不希望 .git 文件夹与您的工作树位于同一目录中时使用。而是试试这个:
~/calimoucho/$ git pull --work-tree=checkout/myApp/ ../../apps/myapp