3

尝试使用 git subtree 在多个项目中共享公共库文件。这是我一直遇到的问题。

1) 添加子树,以便我的项目的“lib”子目录来自 lib-dk 存储库。

$ git subtree add --prefix=lib --squash git@bitbucket.org:dwknight/lib-dk.git master

2) 更改“lib”中的文件

3) 提交对主项目 repo 的更改

$ git commit -am "update project"

4) 将更新推送到主项目 repo

$ git push origin master

5) 将“lib”中的更改推送回“lib-dk”repo

$ git subtree push --prefix=lib git@bitbucket.org:dwknight/lib-dk.git master
git push using:  git@bitbucket.org:dwknight/lib-dk.git master
To git@bitbucket.org:dwknight/lib-dk.git
 ! [rejected]        f455c24a79447c6e3fe1690f5709357b7f96828a -> master (non-fast-forward)
error: failed to push some refs to 'git@bitbucket.org:dwknight/lib-dk.git'
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Merge the remote changes (e.g. 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

6) 即使 lib-dk 存储库中没有任何变化,我也会收到此拒绝。当我尝试拉动时,它的行为就像有东西一样,但我可以通过拉动进行更新。推动仍然被拒绝。

4

3 回答 3

2

当我在没有--squash选项的情况下尝试这个时git subtree add,它可以工作。我认为,正如评论者所建议的那样,这--squash是在以一种无益的方式摆弄历史。

于 2013-04-19T19:09:06.363 回答
0

If you used --squash when you added a subtree you have to do git subtree pull --prefix=prefix remote branch --squash before you push

于 2021-12-20T09:35:05.863 回答
0

你可能需要使用git add -A .thengit commit而不是git commit -am <message>

因为-Aingit-add会这样做:

   -A, --all, --no-ignore-removal
       Update the index not only where the working tree has a file matching <pathspec> but also where the
       index already has an entry. This adds, modifies, and removes index entries to match the working
       tree.

       If no <pathspec> is given when -A option is used, all files in the entire working tree are updated
       (old versions of Git used to limit the update to the current directory and its subdirectories).

另一方面,-aingit-commit会做(手册页没有给出太多细节):

   -a, --all
       Tell the command to automatically stage files that have been modified and deleted, but new files
       you have not told Git about are not affected.

可能会有所帮助。作者git add -A .明确调用。

于 2016-12-28T07:13:10.303 回答