简体   繁体   English

硬复位后推 git 分支

[英]Pushing git branch after hard reset

I am a total beginner with git, so plz pardon my silly words if any.我是 git 的初学者,所以请原谅我的愚蠢言论。

Okay, so I have a local branch where I've done hard reset to a certain commit.好的,所以我有一个本地分支,在那里我对某个提交进行了硬重置。 And now when I do git status it shows that -现在,当我执行git status时,它显示 -

On branch product
Your branch is behind 'origin/product' by 3 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

nothing to commit, working tree clean

Which is why I can't push my branch, as I first have to pull the changes.这就是为什么我不能推送我的分支,因为我首先必须拉取更改。

Now, I do understand why this is happening, (because hard reset removes all the git history after that certain commit, and the remote branch does have those commits, therefore its telling me that origin/product branch is ahead by 3 commits) (plz correct me if I'm wrong)现在,我确实明白为什么会发生这种情况,(因为硬重置会在该特定提交之后删除所有 git 历史记录,并且远程分支确实有这些提交,因此它告诉我原始/产品分支领先 3 个提交)(请如我错了请纠正我)

So how can I push the changes made by hard reset?那么如何推送硬重置所做的更改呢?

Git tries to preserve the history wherever applicable, so with default settings it will not "revert" branches on push. Git 尝试在适用的情况下保留历史记录,因此在默认设置下,它不会在推送时“恢复”分支。

To force overwrite a remote branch with the tip (head) of your local branch, the traditional way was to use a "force push":要使用本地分支的尖端(头部)强制覆盖远程分支,传统的方法是使用“强制推送”:

git push -f
git push --force  # Equivalent

However, note that the above command pushes all your branches in "force mode".但是,请注意,上述命令会将所有分支推送到“强制模式”。 If you want to do it safer by pushing only one branch, use this syntax:如果您想通过只推送一个分支来更安全地执行此操作,请使用以下语法:

git push origin +master

The plus sign means "overwrite", and as you've specified the branch name, only master will be force-pushed.加号表示“覆盖”,并且当您指定分支名称时,只有master将被强制推送。

Both methods cover two cases of both "reverting" and "overwriting with diverged histories".这两种方法都涵盖了“还原”和“用不同的历史覆盖”两种情况。


By the way, this understanding is (partially) wrong:顺便说一句,这种理解(部分)是错误的:

hard reset removes all the git history after that certain commit硬重置会删除该特定提交后的所有 git 历史记录

Hard resets does not remove any commit from Git's local storage (that .git directory), but only points your current branch (head) at the target commit, and checks out any files changed.硬重置不会从 Git 的本地存储(即.git目录)中删除任何提交,而只会将您当前的分支(头)指向目标提交,并检查所有更改的文件。

Your commits stays in Git storage until GC'd.在 GC 之前,您的提交将保留在 Git 存储中。 The easiest way to verify that they still exists (and intact!) is to examine the output of git reflog .验证它们是否仍然存在(并且完好无损!)的最简单方法是检查git reflog的 output 。 You can also check them back using commands similar to git checkout/reset 'HEAD@{1}' should you want.如果需要,您还可以使用类似于git checkout/reset 'HEAD@{1}'命令来检查它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM