簡體   English   中英

將git checkout中的子模塊包含在掛鈎的GIT_WORK_TREE中

[英]Including submodules in git checkout to GIT_WORK_TREE in hook

在更新后的掛鈎中使用以下代碼時是否可以包含子模塊?

GIT_WORK_TREE=/path/to/directory git checkout -f

我還有什么其他選擇來分發代碼,包括更新后掛鈎的子模塊?

謝謝。

這個問題:“ 使用git submodule update --init上后鈎 ”中提到的錯誤消息,如果你使用這個,你可以看到post-update鈎:

GIT_WORK_TREE=/path/to/directory git submodule update --init

這會給:

remote: You need to run this command from the toplevel of the working tree.

因此最好直接在目標倉庫中cd並從那里運行命令:

export GIT_DIR=$(pwd)

cd /path/to/target/workingtree

git checkout -f master
git submodule update --init --recursive

但是,如“ 在推送到裸工作目錄后如何在工作樹中初始化/更新git子模塊? ”中所示:

看起來當您運行“git submodule update”時無法設置GIT_WORK_TREE
它會嘗試使用它作為子模塊的工作樹,而不是超級項目

Aaron Adams的博客文章“ Git push with submodules:how-to guide ”描述了類似的錯誤信息,如OP iliveinapark評論中所示

可悲的是,這不起作用,我懷疑是因為我的回購是一個簡單的回購。
我按照這些命令得到的錯誤是:

fatal: This operation must be run in a work tree

如果,為了克服上述錯誤,我使用類似的東西:

git --git-dir=<my bare repo> --work-tree=<where I export to> submodule update --init --recursive 

我明白了:

fatal: working tree '<where I export to>' already exists. Clone of '<submodule repo>' into submodule path '<submodule path>' failed

上面提到的博客文章提出了一種基於非裸倉庫的方法(通常推薦用於推送,但在這種情況下是必要的):

使用Git管理帶有子模塊的網站:正確的方法

首先,讓我們創建一個通用的post-receive鈎子,我不需要在每個存儲庫的基礎上進行更改:

[aaron@aaronadams]$ cat > /usr/local/share/git-core/templates/hooks/post-receive.sample
#!/bin/sh
#
# An example hook script to update the working tree, including its
# submodules, after receiving a push.
#
# This hook requires core.worktree to be explicitly set, and
# receive.denyCurrentBranch to be set to false.
#
# To enable this hook, rename this file to "post-receive".

# Read standard input or hook will fail
while read oldrev newrev refname
do
:
done

# Unset GIT_DIR or the universe will implode
unset GIT_DIR

# Change directory to the working tree; exit on failure
cd `git config --get core.worktree` || exit

# Force checkout
git checkout --force

# Force update submodules
git submodule update --init --recursive --force
[aaron@aaronadams]$ chmod +x /usr/local/share/git-core/templates/hooks/post-receive.sample

現在讓我們繼續並打破所有規則。

我們要:

  • 在我們的網站目錄中初始化一個非裸Git存儲庫;
  • 確保它可以從git push接收;
  • 顯式將其工作樹設置為其父目錄;
  • 並啟用我們剛剛創建的鈎子。
[aaron@aaronadams]$ cd /var/www/vhosts/aaronadams.ca/sites/staging.aaronadams.ca
[aaron@aaronadams]$ git init && git config --bool receive.denyCurrentBranch false && git config --path core.worktree ../ && mv .git/hooks/post-receive.sample .git/hooks/post-receive
Initialized empty Git repository in /var/www/vhosts/aaronadams.ca/sites/staging.aaronadams.ca/.git/

最后,在我們的本地機器上,我們將更改我們的遙控器以反映新存儲庫的位置,並推送。

[aaron@aaronadams]$ git remote set-url staging aaron@aaronadams.ca:sites/staging.aaronadams.ca
[aaron@aaronadams]$ git push staging master
remote: Submodule 'codeigniter' (git://github.com/EllisLab/CodeIgniter.git) registered for path 'codeigniter'
remote: Cloning into 'codeigniter'...
remote: Submodule path 'codeigniter': checked out 'fd24adf31255822d6aa9a5d2dce9010ad2ee4cf0'
To aaron@aaronadams.ca:sites/staging.aaronadams.ca
 * [new branch]      master -> master

神聖的廢話,它的工作!

這種方法不僅與子模塊兼容,而且只需要一個命令來設置一個新的遠程存儲庫 (好的,它包含四個命令)。
它還將存儲庫和工作樹保持在同一個位置; 並且在我們的配置或鈎子文件中不需要絕對路徑,它現在也完全可移植


OP iliveinapark 提到

然而,這變得有點過於繁瑣,所以我選擇了簡單的強制結賬,並將手動更新我的子模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM