gpt4 book ai didi

Git Fetch 无法处理裸仓库,但 git pull 可以处理普通仓库

转载 作者:IT王子 更新时间:2023-10-29 00:57:16 26 4
gpt4 key购买 nike

首先,大局:我正在尝试为我正在运行的 Redmine/Gitolite 服务器编写一个 git post-receive 脚本。根据各种建议,我正在创建一个供 Redmine 读取的裸本地存储库,并且我正在 Gitolite 上设置一个接收后脚本以将更改推送到 Redmine 存储库。

但是,我对 Git 非常菜鸟,所以我什至不能在这里做一个简单的任务>_<。我想如果我弄清楚了这一点,我应该能够编写上面的脚本。设置我的测试存储库后,我创建了两个存储库作为测试。

(“Central Repo”是一个位于 git@localhost:testing 的 Gitolite 存储库)

cd /tmp
mkdir /tmp/test
$ git clone git@localhost:testing
$ git clone git@localhost:testing testing2
$ git clone git@localhost:testing --bare

现在当我运行 ls 时:

$ ls
testing testing2 testing.git

现在,我更改了 testing2 中的测试文件,然后将更改推送到中央仓库。

$ cd testing2
$ echo 'testline' >> test && git commit --allow-empty-message -a -m '' && git push

正如预期的那样,如果我在“testing”文件夹上运行“git pull”,一切都会按预期进行。

$ cd testing
$ git pull
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From localhost:testing
3242dba..a1ca5ba master -> origin/master
Updating 3242dba..a1ca5ba
Fast-forward
test | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
$ diff ./test ../testing2/test
$

如最后一个“diff”所示,“testing”目录和“testing2”目录完全按预期工作。 “git pull”命令同步两个目录。

但是,如果我进入 testing.git(又名:裸仓库),git fetch/git reset --soft 无法将裸仓库更新到最新版本。

$ ls
branches config description HEAD hooks info objects packed-refs refs
$ git fetch
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From localhost:testing
* branch HEAD -> FETCH_HEAD
$ git reset --soft
$ cd ..
$ git clone ./testing.git testing3
Cloning into testing3...
done.
$ cd testing3
$ diff test ../testing2/test
5a6
> testline

正如您从上一个示例中看到的,裸存储库无法更新,并且这两个文件之间存在某种差异。我做错了什么?

提前致谢

最佳答案

您的提取尚未更新 master 分支,仅更新 FETCH_HEAD(参见“What does FETCH_HEAD in Git mean?”)。

如“how do I pull to a bare repository?”中所述,您应该执行以下操作:

git fetch origin master:master

或者,对于 all the branches :

git fetch origin +refs/heads/*:refs/heads/*

Colin D Bennett添加:

If you want to fetch these on a regular basis, you should consider:

git config remote.origin.fetch +refs/heads/*:refs/heads/*

which will allow you to type git fetch to sync your branches with the remote.
Note that this make sense only in a bare repository where local branches are not supposed to be edited.

关于Git Fetch 无法处理裸仓库,但 git pull 可以处理普通仓库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10696718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com