git push 不提交本地更改到远程存储库

时间:2021-07-26 07:39:31

标签: git

我已按照以下步骤操作。

$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 推送到远程仓库。我想要类似于我的本地状态的远程存储库状态。

提前致谢。

2 个答案:

答案 0 :(得分:1)

<块引用>
$ 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 处的远程存储库的 origin 设置为相同的状态。远程存储库有权拒绝放弃提交 4 和 5,尤其是在您不拥有远程存储库的情况下。如果您确实拥有远程存储库,则可以进行设置,以便可以放弃那里的提交——但您没有告诉我们是否是这种情况。 >

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

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

答案 1 :(得分:0)

如果您对几个文件进行了更改,那么您首先要做的就是不要

<块引用>

$git commit -m "commit6"

但是要使用 git add file_name 将文件添加到 stage 然后你可以创建一个提交并将其推送到远程仓库 确保您首先使用 git remote

将离线存储库与遥控器相关联
相关问题