Git,如何重置提交的源/母版?

nmpmafwu  于 2022-12-10  发布在  Git
关注(0)|答案(6)|浏览(127)

我通过以下命令将本地主机重置为提交:

git reset --hard e3f1e37

当我输入$ git status命令时,终端显示:

# On branch master
# Your branch is behind 'origin/master' by 7 commits, and can be fast-forwarded.

#   (use "git pull" to update your local branch)
#
nothing to commit, working directory clean

由于我还想重置原点/页眉,因此我检出原点/主数据:

$ git checkout origin/master
Note: checking out 'origin/master'.

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 performing another checkout.

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

  git checkout -b new_branch_name

HEAD is now at 2aef1de... master problem fixed for master. its okay now.

并通过以下命令重置报头:

$ git reset --hard e3f1e37
HEAD is now at e3f1e37 development version code incremented for new build.

然后我尝试向origin/header添加提交,但没有成功。

$ git commit -m "Reverting to the state of the project at e3f1e37"
# HEAD detached from origin/master
nothing to commit, working directory clean

最后,我向我的本地主人结帐。

$ git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 7 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

因为,我重置了origin/master的头,我希望local和origin应该在同一个方向上,但正如你所看到的,git说我的local/master比origin/master落后7次提交。
我该如何解决这个问题?我正在寻找的东西是本地/主文件夹的头和源文件夹指向同一个提交。下图显示了我所做的。谢谢。

szqfcxe2

szqfcxe21#

origin/xxx分支总是指向一个remote的指针。你不能 checkout 它们,因为它们不是指向你本地仓库的指针(你只 checkout 提交。这就是为什么你看不到写在命令行界面分支标记中的名字,只看到提交哈希)。
要更新远程服务器,您需要做的是将本地更改强制推送到主服务器:

git checkout master
git reset --hard e3f1e37
git push --force origin master
# Then to prove it (it won't print any diff)
git diff master..origin/master
kqlmhetl

kqlmhetl2#

找到的解决方案here帮助我们将master更新到之前已经被推送的提交:

git checkout master
git reset --hard e3f1e37
git push --force origin e3f1e37:master

与接受的答案的关键区别在于push命令中master之前的提交哈希“e3f1e37:“。

sdnqo3pr

sdnqo3pr3#

假设您的分支在本地和远程都称为master,并且您的远程称为origin,您可以执行以下操作:

git reset --hard <commit-hash>
git push -f origin master

但是,如果有其他人正在使用您的远程仓库并拉取了您的修改,您应该避免这样做。在这种情况下,最好是还原您不想要的提交,然后像平常一样推送。

gstyhher

gstyhher4#

由于我也有类似的情况,我想我应该分享我的情况和这些答案如何帮助我(谢谢大家)。
所以我决定在本地工作,每次我想保存我在主分支上的进度时都修改我的最后一次提交(我知道,我应该分支,提交,继续推送,然后合并回master)。
一天深夜,我偏执地担心我的进度会因为硬件故障或其他什么东西而丢失,我决定把master推到origin。后来我一直在修改我的本地master分支,当我决定再次推的时候,我面对着不同的master分支,发现我不能像修改本地开发分支那样修改origin/upstream(duh!)。
所以我没有在本地 checkout master,因为我已经在提交了。Master没有改变。我甚至不需要重置--很难,我当前的提交是可以的。
我只是强制push到origin,甚至没有指定我想强制master提交什么,因为在这种情况下,它是HEAD所在的位置。检查了git diff master..origin/master,所以没有任何差异,就这样。全部修复。谢谢!(我知道,我是一个git新手,请原谅!)
因此,如果您已经可以在本地使用主分支,只需:

git push --force origin master
git diff master..origin/master
hkmswyz6

hkmswyz65#

以下是我的建议。不过,西蒙·布德里亚斯的上述回答也很棒。

查看分支

git checkout master

软重置,这意味着这些更改将变为未提交状态

git reset --soft HEAD~1

如果不是主分支,则从原始分支手动删除

修复您的代码,或将其堆栈。
然后,创建一个新的提交,并描述发生了什么,否则

git push -f origin master
j0pj023g

j0pj023g6#

步骤1.重置分支到特定的头部。步骤2.强制推送更改到您的远程分支。

git reset --hard e3f1e37 /   git reset --hard origin/master
 git push --force origin "Branch name"

完成。现在检查远程分支并重置为上一次提交

相关问题