我有一个远程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重置——很难:如果你想保存当前修改的文件,请确保首先使用git stash。

$ git log -1
# note the SHA-1 of latest commit
$ git checkout master
# reset your branch head to your previously detached commit
$ git reset --hard <commit-id>

正如git签出手册页中提到的(重点是我的):

有时能够签出不在分支顶端的提交是有用的。 最明显的例子是在标记的官方发布点检出提交,就像这样:

$ git checkout v2.6.18

Earlier versions of git did not allow this and asked you to create a temporary branch using the -b option, but starting from version 1.5.0, the above command detaches your HEAD from the current branch and directly points at the commit named by the tag (v2.6.18 in the example above). You can use all git commands while in this state. You can use git reset --hard $othercommit to further move around, for example. You can make changes and create a new commit on top of a detached HEAD. You can even create a merge by using git merge $othercommit. The state you are in while your HEAD is detached is not recorded by any branch (which is natural --- you are not on any branch). What this means is that you can discard your temporary commits and merges by switching back to an existing branch (e.g. git checkout master), and a later git prune or git gc would garbage-collect them. If you did this by mistake, you can ask the reflog for HEAD where you were, e.g. $ git log -g -2 HEAD


虽然git push表示“所有内容都是最新的”,但从技术上讲,你仍然可以推送一个分离的HEAD,正如Jonathan Benn在评论中所指出的那样

 git push origin HEAD:main

您必须指定目标分支,因为源不是分支,也没有上游目标分支。

其他回答

呃. .如果你是一个git新手,你确定你在git push之前有git commit吗?我第一次就犯了这个错误!

见上面VonC的回答-我需要一个额外的步骤:

$ git log -1
- note the SHA-1 of latest commit
$ git checkout master
- reset your branch head to your previously detached commit
$ git reset --hard <commit-id>

我这么做了,但是当我尝试着去推remoterepo master时,它说 "错误:未能推动一些参考。为了防止您丢失历史记录,非快进更新被拒绝,合并远程更改(例如。'git pull'),然后再推。”

所以我做了'git拉remoterepo master',它发现了一个冲突。我再次做了git reset——hard <commit-id>,将冲突文件复制到备份文件夹,git再次拉出remoterepo master,将冲突文件复制回我的项目,git提交,然后git推送remoterepo master,这一次它成功了。

Git不再说“一切都是最新的”,也不再抱怨“快进”。

在我的例子中,我有2个远程回购。

git remote -v
originhttps https://asim_kt@...
originhttps https://asim_kt@...
origin  ssh:git@bitbucket.org:...
origin  ssh:git@bitbucket.org:...

两次回购都是一样的。一个是https,另一个是ssh。因此,删除不需要的,(在我的情况下,ssh。因为我使用https,因为ssh不能工作!)为我解决了这个问题。

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

现在来看:

$ 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”,但那也是我源代码树中的一个目录。