简体   繁体   English

git 重置上游分支

[英]git reset upstream branch

So the master branch of my upstream repo is in bit of a mess.所以我的上游 repo 的 master 分支有点乱。

Commits on upstream branch looks like below.在上游分支上的提交如下所示。

A - Latest commit
|
B
|
C - Bad commit
|
D - Commit I want the upstream master branch to be at.

on my forked branch I can do a git reset --hard D .在我的分叉分支上,我可以执行git reset --hard D But how can I apply these changes back to the upstream master branch.但是如何将这些更改应用回上游主分支。 While creating a PR(Pull Request) from forked branch to upstream branch there are no changes shown.在创建从分叉分支到上游分支的 PR(拉取请求)时,没有显示任何更改。

NOTE : Direct commits on upstream branch is prohibited, it has to be done using a Pull Request from the Fork.注意:禁止在上游分支上直接提交,必须使用来自 Fork 的 Pull Request 来完成。

EDIT This is how i have cloned the remote repository.编辑这就是我克隆远程存储库的方式。

git clone http://myrepo.git
git remote add upstream http://main-repo.git

Now origin and upstream are like below.现在原点和上游如下所示。

origin - http://myrepo.git
upstream - http://main-repo.git

Forked repository is basically a mirror clone of the remote repository.分叉存储库基本上是远程存储库的镜像克隆。 On the bit bucket UI there is an option to create a fork and enable fork syncing between origin & upstream.在位桶 UI 上,有一个选项可以创建分叉并在源和上游之间启用分叉同步。

When I say forked branch it means that the branch which is on the forked repository.当我说分叉分支时,它意味着分叉存储库上的分支。

If changes need to be done by pull request, you cannot remove commits.如果需要通过拉取请求完成更改,则不能删除提交。

Instead, you will want to revert;相反,您会想要还原; this creates a new commit, which reverses the effects of the bad one.这会创建一个新的提交,从而逆转坏提交的影响。 Both changes will remain part of history.这两个变化都将成为历史的一部分。

Create a new branch as normal, then run the command: git revert hash_of_C (where you substitute the hash of commit C).正常创建一个新分支,然后运行命令: git revert hash_of_C (在其中替换提交 C 的哈希值)。 Review the change, fine-tune the commit message with git commit --amend if needed, then push for review as normal.审查更改,如果需要,使用git commit --amend微调提交消息,然后像往常一样推送审查。

If you absolutely need to remove the commit, you'll need to discuss that with the project lead.如果您绝对需要删除提交,则需要与项目负责人讨论。 Editing history that's already part of upstream master is a major disruption to the whole team, most likely you'll be told to revert instead.编辑已经是上游master一部分的历史记录对整个团队来说是一个重大的干扰,很可能你会被告知要恢复。 If the project lead agrees that commit C must not be part of history, they'll probably remove it themselves, most likely by temporarily giving themselves permission to push directly to master.如果项目负责人同意提交 C 不能成为历史的一部分,他们可能会自己删除它,最有可能的是暂时授予自己直接推送给 master 的权限。 The whole team will need to be alerted as well.整个团队也需要得到提醒。

The operation you want is a git push with --force, and since you do not have permission to do this.您想要的操作是带有 --force 的 git push,因为您无权执行此操作。
Then the alternative operation could a revert operation, and there is an elegant solution for you to revert multi commits.然后替代操作可以是还原操作,并且有一个优雅的解决方案可以让您还原多次提交。
How to revert multiple git commits? 如何恢复多个 git 提交?

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

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