创建 git 分支,并将原始状态恢复到上游状态

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

Create git branch, and revert original to upstream state

gitversion-controlgithub

提问by Andriy Drozdyuk

I recently screwed up my git repo and would like to find out if there is any remedy to it.

我最近搞砸了我的 git repo,想知道是否有任何补救措施。

My setup is this:

我的设置是这样的:

Central repo on github.
Personal repo on github (which is a fork of Central)
   +Central is setup as remote (upstream/master)
   +Master branch (origin/master)
   +Feature branch (origin/feature)

My workflow was like this:

我的工作流程是这样的:

Need to fix something in Central:
   1. checkout Master
   2. Make changes
   3. Pull from upstream/master and merge
   3. Commit, push to upstream/master

Need to work on a New Feature:
   1. Checkout/Create Feature branch
   2. Work work work
   3. Pull from upstream/master and merge
   4. Commit, push to upstream/master

This way I always had a pristine state of Central in my Master branch.

通过这种方式,我在我的 Master 分支中始终拥有原始的 Central 状态。

Now what I did was started working on Master branch instead. So I made changes to my master and can no longer branch from it to get a copy of Central. Whenever I need to make and push some fixes to Central, i have to clone the Central into another directory and work from there.

现在我所做的是开始在 Master 分支上工作。所以我对我的 master 进行了更改,不能再从它分支来获取 Central 的副本。每当我需要对 Central 进行修复和推送时,我必须将 Central 克隆到另一个目录并从那里开始工作。

My question: Is there a way to "revert" my master to be an identical copy of the Central, while moving all the changes I have made on my Master into another branch (say Feature)?

我的问题:有没有办法“恢复”我的 master 为 Central 的相同副本,同时将我在 master 上所做的所有更改移动到另一个分支(比如 Feature)?

I know it's confusing, and I would appreciate any help. I will clarify if anything is unclear.

我知道这很令人困惑,我将不胜感激。如果有什么不清楚的,我会澄清。

回答by Andriy Drozdyuk

Well the solution was pretty simple, hinted by Pat Notz and Bombe.

好吧,解决方案非常简单,由 Pat Notz 和 Bombe 暗示。

#Make sure we're on the master branch
$ git checkout master

# Make a new branch to hold the work I've done
$ git branch old_master

# Save this branch on my remote repo (for backup)
$ git checkout old_master
$ git push origin old_master

# Reset my local master back to match the commit just before I started 
# working on my new feature
$ git checkout master
$ git reset --hard 2aa93842342342

# Get it to be the same as my Central
$ git pull upstream master

# Now DELETE my master on my remote repo
$ git push origin :master

# And recreate it
$ git push origin master

# Branch created!
#* [new branch]      master -> master

#

Now I have two nice branches: master and old_master. With master being a copy of my Central, for fixes to production, and old_master holding all the work that I did previously!

现在我有两个不错的分支:master 和 old_master。master 是我的 Central 的副本,用于修复生产,而 old_master 保存了我之前所做的所有工作!

Thanks!

谢谢!

回答by Pat Notz

# Make sure we're on the master branch
$ git checkout master

# Make a new branch to hold the work I've done
$ git branch old_master

# Reset my local master back to match origin/master
$ git reset --hard origin/master

Now you can checkout old_masterand use it just like you did your featurebranch

现在您可以结帐old_master并使用它,就像在您的feature分支上一样

回答by Bombe

What exactly do you mean by

你到底是什么意思

I messed [up] my master and can no longer branch from it.

我搞砸了我的主人,不能再从它分支了。

You can always create a new branch from any commit in your repository, no matter how “messed up” it may be–which by the way is something Git has no notion of.

您始终可以从存储库中的任何提交创建一个新分支,无论它有多么“混乱”——顺便说一下,这是 Git 没有概念的。

Basically you can revert your repository to any state it previously had because Git will not explicitely delete any objects, it will only garbage collect unreferenced (dangling) objects every now and then. So you just need to find out what your repository looked like. gitkor git logcan help you there.

基本上,您可以将存储库恢复到以前的任何状态,因为 Git 不会明确删除任何对象,它只会时不时地垃圾收集未引用(悬空)的对象。所以你只需要找出你的存储库是什么样的。gitk或者git log可以在那里帮助您。

After you restored your local repository to a state you like you can simply push it back to your central public repository. If that results in a non-fast-forward push you might need to specify the --forceflag when pushing.

将本地存储库恢复到您喜欢的状态后,您只需将其推送回中央公共存储库即可。如果这导致非快进推送,您可能需要--force在推送时指定标志。