Git子树推送总是失败



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

1) 添加子树,使我的项目的"lib"子目录来自lib-dk存储库。

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

2) 对"lib"中的文件进行更改

3) 提交对主要项目回购的更改

$ git commit -am "update project"

4) 向主要项目回购推送更新

$ git push origin master

5) 将"lib"中的更改推回到"lib-dk"回购

$ 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回购没有任何变化,我也会被拒绝。当我尝试拉取时,它的行为就像有东西一样,但我可以通过拉取进行更新。尽管如此,这种推动仍被拒绝。

当我在不使用--squash选项的情况下尝试git subtree add时,它会起作用。我认为,正如评论者所说,--squash在以一种无益的方式篡改历史。

您可能需要使用git add -A .,然后使用git commit,而不是git commit -am <message>

因为git-add中的-A可以:

   -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).

另一方面,git-commit中的-a可以(手册页中没有给出太多细节):

   -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 .

如果在添加子树时使用--squash,则在推送之前必须执行git subtree pull --prefix=prefix remote branch --squash

最新更新