首页 > 解决方案 > git push 不将本地更改提交到远程存储库

问题描述

我已遵循以下步骤。

$git log --oneline

commit5
commit4
commit3
commit2
commit1

$git checkout commit3

$git log --oneline
commit3
commit2
commit1

Now i made changes to few file.

$git add .
$git commit -m "commit6"
$git log --oneline
commit6
commit3
commit2
commit1

Now, i try to push these changes to remote repository.

$git push -f origin master


This command is not pushing my commit6 to the remote server.
I am getting the message everything up-to-date.

如何将 commit6 推送到远程存储库。我想要类似于本地状态的远程存储库状态。

提前致谢。

标签: git

解决方案


$ git checkout commit3

我在这里假设您使用原始哈希 ID 来签出,例如:

$ git checkout fe3fec53a6
Note: switching to 'fe3fec53a6'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at fe3fec53a6 Merge branch 'bc/rev-list-without-commit-line'

请阅读所有git checkout打印的文本。

现在我对几个文件进行了更改。

$ git add .
$ git commit -m "commit6"
$ git log --oneline
commit6
commit3
commit2
commit1

实际上,您现在已经创建了一个新的未命名分支。这个新分支在你的新分支结束commit6

您现有的master分支保持不变。它继续以commit5您在原始输出中看到的现有内容结束。

如何将 commit6 推送到远程存储库。我想要类似于本地状态的远程存储库状态。

根据所讨论的远程存储库,这可能非常困难或不可能。特别是,您当地的州使用分离的 HEAD。您可能无法将其他存储库置于 detached-HEAD 状态。

可以将新提交发送到远程存储库,但您可能希望使用分支名称来执行此操作。当您处于分离 HEAD 模式时,您不在任何分支上。您根本没有在任何分支上进行 commit6 。

您可以选择放弃提交 4 和 5,而仅在本地使用提交 6 作为(本地)master分支的提示提交。您也可以尝试将远程存储库的master, over at设置origin为相同的状态。远程存储库将有权拒绝放弃提交 4 和 5,尤其是在您不拥有远程存储库的情况下。如果您确实拥有远程存储库,则可以进行设置以便可以丢弃那里的提交——但您没有告诉我们是否是这种情况。

您可以选择在本地添加一个新的提交master,以便commit7(新的)紧随其后commit5,并将这个新的提交及其文件发送到远程存储库。这比让远程存储库丢弃提交 4 和 5的请求更有可能成功,因为 Git 是为添加新提交而不丢弃现有提交而构建的。

您需要决定哪些操作最合适(或者是否有比其中任何一个更好的替代方法)。如果您希望强制远程存储库放弃提交,通常需要某种“强制推送”。实际上存在数百个关于此的 StackOverflow 问题。


推荐阅读