我有一个远程gitosis服务器和一个本地git存储库,每次我在代码中做大的更改时,我也会将更改推到该服务器。

但是今天我发现,即使我有一些本地更改并提交到本地存储库,当运行git push origin master时,它说“一切都是最新的”,但当我使用git clone在远程服务器上签出文件时,它不包含最新的更改。我只有一个名为“master”的分支机构和一个名为“origin”的远程服务器。

PS: 这是git在运行ls-remote时显示的内容,我不确定它是否有帮助

$ git ls-remote origin
df80d0c64b8e2c160d3d9b106b30aee9540b6ece        HEAD
df80d0c64b8e2c160d3d9b106b30aee9540b6ece        refs/heads/master
$ git ls-remote .
49c2cb46b9e798247898afdb079e76e40c9f77ea        HEAD
df80d0c64b8e2c160d3d9b106b30aee9540b6ece        refs/heads/master
df80d0c64b8e2c160d3d9b106b30aee9540b6ece        refs/remotes/origin/master
3a04c3ea9b81252b0626b760f0a7766b81652c0c        refs/tags/stage3

当前回答

我遇到过这样的情况,我在一个特征分支上,而我的同事也创建了他自己的特征分支。我运行git fetch -a,然后git push origin <coworkers_branch>。它一直告诉我一切都是最新的。

我通过检出到<coworkers_branch>,然后从我的特征分支中提取,然后提交并推回到<coworkers_branch>来修复它。

我真诚地希望这能帮助到一些人,因为我花了太多的时间在这上面。

其他回答

我的错误不同于上面提到的所有错误。如果你不知道为什么你会有一个分离的头,那么你可能不会。我正在使用git commit和git push进行自动驾驶,还没有读取git commit的输出。结果,这是一条错误消息,因为我忘记了-am。

[colin] ~/github/rentap.js [master] M % git commit 'figured out some more stuff with the forms in views and started figuring out row and mode in models so also made matching routes and controllers'
error: pathspec 'figured out some more stuff with the forms in views and started figuring out row and mode in models so also made matching routes and controllers' did not match any file(s) known to git.
[colin] ~/github/rentap.js [master] M % git push
Enter passphrase for key '/home/colin/.ssh/id_ecdsa': 
Everything up-to-date

通过把-am放在我经常做的地方来修复它:

[colin] ~/github/rentap.js [master] M % git commit -am 'figured out some more stuff with the forms in views and started figuring out row and mode in models so also made matching routes and controllers'

另一个可能罕见的情况,但值得一提的是-当你在本地环境中配置了预提交钩子(扫描代码以查找错误,例如Python安全,flake8, black, isort, bandit等),如果其中一个失败了,你的提交也失败了。这在控制台中应该是显而易见的,但是由于来自钩子的错误可能会充斥控制台中,因此您可能没有意识到您的提交将无法工作,直到预提交钩子被配置为忽略问题或问题得到解决。

$ git push origin local_branch:remote_branch

解释

我也犯了同样的错误,花了几个小时试图解决这个问题。最后我找到了。 我不知道的是git push origin branch-x会尝试在本地搜索branch-x然后推送到远程branch-x。

在我的例子中,我有两个远程url。我做了一个从分支-x到分支-y的签出,当我试图从本地的y推到远程的x时,我有一条消息,一切都是最新的,这很正常,因为我正在推到第二个远程的x。

长话短说,为了不落入这种陷阱,你需要指定源ref和目标ref:

$ git push origin local_branch:remote_branch

更新:

如果你每次推送分支都要运行这个命令,你可能需要设置本地和远程分支之间的上游,如下所示:

$ git push --set-upstream origin local_branch:remote_branch

Or

$ git push -u origin local_branch:remote_branch

这里,我的解决方案与上面的不同。我还没有弄清楚这个问题是怎么发生的,但我解决了它。有点出乎意料。

现在来看:

$ git push origin use_local_cache_v1 Everything up-to-date $ git status On branch test Your branch is ahead of 'origin/use_local_cache_v1' by 4 commits. (use "git push" to publish your local commits) ...... $ git push fatal: The upstream branch of your current branch does not match the name of your current branch. To push to the upstream branch on the remote, use git push origin HEAD:use_local_cache_v1 To push to the branch of the same name on the remote, use git push origin test $ git push origin HEAD:use_local_cache_v1 Total 0 (delta 0), reused 0 (delta 0) remote:

对我有效的命令是

$git push origin HEAD:use_local_cache

(希望你们能尽快走出困境)

另一种可能是您在.gitignore文件中命名了一个被排除的目录。这样新的提交就不会被推送。我碰巧命名了一个目录来忽略“search”,但那也是我源代码树中的一个目录。