如何将 git master 分支重置为分叉存储库中的上游分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42332769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I reset the git master branch to the upstream branch in a forked repository?
提问by fossfreedom
I've completely messed up the master branch of my forked git repo.
我已经完全搞砸了我的分叉 git 存储库的主分支。
I want to completely reset the master branch that was pushed to my fork with the contents of the upstream master repo. I have no interest in retaining any master branch changes or history.
我想用上游主存储库的内容完全重置推送到我的分支的主分支。我没有兴趣保留任何主分支更改或历史记录。
The simplest approach would have been to delete my forked repo and refork from the upstream project. However, I have work in other pushed branches that I don't want to lose.
最简单的方法是从上游项目中删除我的分叉 repo 和 refork。但是,我在其他我不想丢失的推送分支中工作。
Thus how to I reset my pushed master branch with the upstream master?
因此,如何使用上游 master 重置我推送的 master 分支?
git clone https://myrepo.git
cd myrepo
git remote add upstream https://upstream.git
git fetch upstream
Where do I go from here to reset my local and remote master branches with the upstream master?
我该从哪里开始使用上游 master 重置我的本地和远程 master 分支?
回答by Johannes Barop
You can reset your local master branch to the upstream version and push it to your your repository.
您可以将本地 master 分支重置为上游版本并将其推送到您的存储库。
Assuming that "upstream" is the original repository and "origin" is your fork:
假设“上游”是原始存储库,而“原点”是您的分支:
# ensures current branch is master
git checkout master
# pulls all new commits made to upstream/master
git pull upstream master
# this will delete all your local changes to master
git reset --hard upstream/master
# take care, this will delete all your changes on your forked master
git push origin master --force
(You can define the original repo as "upstream" with git remote add upstream /url/to/original/repo
.)
(您可以使用git remote add upstream /url/to/original/repo
.将原始存储库定义为“上游” 。)
回答by Bhavesh Gupta
This would reset your master branch with the upstream master and if the branch has been updated since your forked it would pull those changes as well.
这将使用上游 master 重置您的 master 分支,如果分支自您分叉后已更新,它也会拉取这些更改。
git checkout master
git reset upstream/master
git pull --rebase upstream master
git push origin master --force
PS: Assuming Upstream is the original repo while origin is your copy.
PS:假设上游是原始回购,而来源是您的副本。
回答by Chetabahana
I have tried the method like this:
我试过这样的方法:
$REPO=<repo>
$ORIGIN=<user>/$REPO
$UPSTREAM=<upstream>/$REPO
$ git clone [email protected]:$ORIGIN.git
$ cd $REPO
$ git checkout master
$ git remote add upstream [email protected]:$UPSTREAM.git
$ git reset --hard upstream/master
$ git pull --rebase upstream master
$ git push origin master --force
the output will show a warning:
输出将显示警告:
fatal: ambiguous argument 'upstream/master':
unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
So the correct way is put git pull
before git reset
:
所以正确的方法放在git pull
之前git reset
:
$ git clone [email protected]:$ORIGIN.git
$ cd $REPO
$ git checkout master
$ git remote add upstream [email protected]:$UPSTREAM.git
$ git pull --rebase upstream master
$ git reset --hard upstream/master
$ git push origin master --force
then the output will be like this:
那么输出将是这样的:
From github.com:<upstream>/<repo>
* branch master -> FETCH_HEAD
* [new branch] master -> upstream/master
HEAD is now at 7a94b1790 Merge pull request #4237 from <upstream>/...
Current branch master is up to date.
Everything up-to-date.