簡體   English   中英

Git子樹導出和重新導入困境

[英]Git subtree export and re-import woes

我有一個更大的git存儲庫(A),它與我的另一個項目(B)共享一定數量的代碼。 為了使維護更容易,我決定使用公共代碼(C)建立第三個存儲庫,然后通過git subtree

我在A中編寫了所有內容(將公共代碼放在文件夾“sub”中)並使用Detach(move)子目錄中描述的過程進入單獨的Git存儲庫來創建C

現在我只有幾個提交C,我想把它放回A,文件夾子。 我使用http://www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html中描述的方法,因為子文件夾的所有提交現在都是重復的。 我不太介意這一點,但是現在我不知道如何繼續使用這個子目錄。

我在A / sub中進行了其他更改,我想將其推送到C.如git子樹中所述, 推送更改回我使用的子樹項目

git subtree split --prefix sub -b split-branch

只用子樹創建一個分支。 這需要一些時間,但成功完成。

git checkout split-branch
git push remote-c master

給我

failed to push some refs to "remote-c"
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and merge the remote changes
hint: (e.g. 'git pull') before pushing again.

但是一個git pull remote-c master說我已經是最新的了。

我該如何解決這種情況?

編輯1 :我試圖用一個小的測試腳本重現問題。 執行此腳本:

( cd testC; git init --bare )
( cd testA; git init )

cd testA
git remote add C ../testC

mkdir sub
echo subFile1 > sub/subFile1
echo subFile2 > sub/subFile2
git add sub
git commit -m "adding files"

echo FileA > fileA
echo FileB > fileB
git add fileA fileB
git commit -m "add root level files"

# extract subtree and push to C
git subtree split -P sub -b split-branch
git push C split-branch:master

# try to make an update in C
git checkout -b cmaster C/master
echo subFile2new > subFile2
git commit subFile2 -m "updated #2 in C"
git push

這導致了

To ../testC
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '../testC'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. If you did not intend to push that branch, you may want to
hint: specify branches to push or set the 'push.default' configuration
hint: variable to 'current' or 'upstream' to push only the current branch.

我嘗試了git push -f ,這似乎有用。 仍想知道究竟發生了什么以及為什么。

如果本地和遠程具有相同的命名分支,則git push的默認行為是嘗試將所有分支推送到當前分支遠程。 git push手冊:

特殊refspec :(或+:允許非快進更新)指示git推送“匹配”分支:對於本地端存在的每個分支,如果已經存在同名分支,則更新遠程端在偏遠的一邊。 如果沒有找到顯式的refspec(這既不在命令行上也不在相應的遠程文件的任何Push行中 - 見下文),並且沒有設置push.default配置變量,則這是默認的操作模式。

在這種情況下,由於您當前的遠程masterC並且您同時擁有本地master和遠程C/master因此它將被推送,並且由於樹根本不匹配,所以推送將失敗, master -> master (non-fast-forward)消息。

當你git pull到你的分支時,它會說up-to-date因為你正在拉你當前的分支,這是最新的。

要修改git push這種行為,您需要在git config中設置push.default值。 http://www.kernel.org/pub/software/scm/git/docs/git-config.html中查找“push.default”

我的解決方案:升級git。 :-)

假設testAtestC目錄已經存在, testC我的測試腳本會成功。 我正在使用最新版本的git(v1.8.2.1),所以也許他們修了一些東西,因為你發布了這個。

$ mkdir testA testC
$ ./test.sh 
Initialized empty Git repository in /tmp/git/testC/
Initialized empty Git repository in /tmp/git/testA/.git/
[master (root-commit) 3d5644d] adding files
 2 files changed, 2 insertions(+)
 create mode 100644 sub/subFile1
 create mode 100644 sub/subFile2
[master 398c203] add root level files
 2 files changed, 2 insertions(+)
 create mode 100644 fileA
 create mode 100644 fileB
Created branch 'split-branch'
57fe3e8fc226d854b623f11444d82dc77fd45682
Counting objects: 4, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 269 bytes, done.
Total 4 (delta 0), reused 0 (delta 0)
To ../testC
 * [new branch]      split-branch -> master
Branch cmaster set up to track remote branch master from C.
Switched to a new branch 'cmaster'
[cmaster 07c7c89] updated #2 in C
 1 file changed, 1 insertion(+), 1 deletion(-)
Counting objects: 5, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 285 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../testC
   57fe3e8..07c7c89  cmaster -> master

暫無
暫無

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

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