git 如何将本地分支恢复到 github 中的状态?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2147211/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:45:09  来源:igfitidea点击:

How do revert a local branch back to how it is in github?

gitbranchgithubrevert

提问by Jason Baker

I did a bit of development against the wrong branch in my local repository. I did a git branchwithout next doing a git checkout. The commands look something like this:

我对本地存储库中的错误分支做了一些开发。我做了一个git branch没有接下​​来做一个git checkout. 命令看起来像这样:

#On branch development
git branch release-v0.2.0b
# changes and several commits
git push origin release-v0.2.0b

And that's when I realized I was working on the wrong branch. My github repo is in the proper state, but my local repo isn't. I've merged the changes from development into release-v0.2.0b, but I'd like to reset development back to the way it is in my github repo. What's the best way to go about this?

那时我意识到我在错误的分支上工作。我的 github 存储库处于正确状态,但我的本地存储库不是。我已将开发中的更改合并到 release-v0.2.0b 中,但我想将开发重置为我的 github 存储库中的方式。解决这个问题的最佳方法是什么?

采纳答案by Jamey Hicks

Just to make sure I understand the state of things: you created the release branch but did not check it out, so your commits are on the development branch in your local repository. You said you merged the changes into the release-v0.2.0b branch.

只是为了确保我了解事情的状态:您创建了发布分支但没有检查它,因此您的提交位于本地存储库的开发分支上。您说您将更改合并到 release-v0.2.0b 分支中。

If that is the case, and there are no other commits on the development branch you need to preserve, just delete the local copy of the development branch and check it out again from origin.

如果是这种情况,并且开发分支上没有其他需要保留的提交,只需删除开发分支的本地副本并从源重新检出即可。

First, verify what branches you have and which one you are on:

首先,验证您拥有哪些分支以及您所在的分支:

git branch -av

Then switch away from the development branch so you can delete it:

然后切换离开开发分支,以便您可以删除它:

git checkout origin/development
git branch -D development

That actually leaves you on no branch, but you'll get back on a branch when you check it out again from the origin:

这实际上让你没有分支,但是当你从原点再次检出时,你会回到一个分支上:

git checkout origin/development -b development

I suggest checking out origin/development to avoid unnecessary churning of files in your snapshot.

我建议检查起源/开发以避免快照中不必要的文件搅动。

回答by Thanasis Pap

Even quicker you can just reset a local branch to a specific remote:

您可以更快地将本地分支重置为特定的远程:

git reset --hard remote/branch

Where "remote" the name of your remote
Where "branch" the name of the remote branch

其中“remote”远程
分支的名称“branch”远程分支的名称

回答by Lakshman Prasad

Go back to the local release branch you want the changes in, Pull there from github.

返回到您想要更改的本地发布分支,从 github 拉到那里。

git checkout local-branch-that-refers-public-v0.2.0b
git pull origin release-v0.2.0b

Then, reset the branch pointer of the local branch that you committed to the earlier commit you want to go back to.

然后,重置您提交到要返回的较早提交的本地分支的分支指针。

git checkout release-v0.2.0b
git reset HEAD^

回答by Eric Hogue