简体   繁体   English

如何删除或替换github中的master分支?

[英]How to delete or replace master branch in github?

I've got a problem while working with github.我在使用 github 时遇到了问题。

The problem is:问题是:

  1. I've created a repository.我已经创建了一个存储库。

  2. I've filled master branch but made a big mistake, so there's a big story what I don't need.我已经填满了 master 分支,但犯了一个大错误,所以有一个我不需要的大故事。

  3. Then I create a new branch (OhThisAmazingBranch), empty and began a normal project without problems.然后我创建一个新分支(OhThisAmazingBranch),清空并开始一个没有问题的正常项目。

  4. I need to delete master branch and make OhThisAmazingBranch as master.我需要删除 master 分支并将 OhThisAmazingBranch 设为 master。

How to do that?怎么做? Is that even possible?这甚至可能吗?

PS I am working using VS 2019 PS 我正在使用 VS 2019

UPD.更新。 I tried advises what I've found before asking in here, they didn't work, so here I am.我尝试建议我在这里询问之前发现的内容,但它们没有用,所以我来了。

UPD2. UPD2。 Thanks everybody for your answers, I'll try every one what's looks useful for me and let you know if it helped.感谢大家的回答,我会尝试每一个对我有用的东西,如果有帮助,请告诉你。

hard reset your master branch with "OhThisAmazingBranch"使用“OhThisAmazingBranch”硬重置你的主分支

git reset --hard OhThisAmazingBranch

Force push your new master branch强制推送你的新主分支

git push -f origin master

Voilà

While the other answers mention how to recover your master branch, in case you wish to do exactly what you mention in the question, use the following steps:虽然其他答案提到了如何恢复您的主分支,但如果您希望完全按照您在问题中提到的内容进行操作,请使用以下步骤:

  1. Change the default branch from master to OhThisAmazingBranch : This can be done in Settings > Branches and setting the default branch.将默认分支从master更改为OhThisAmazingBranch :这可以在Settings > Branches并设置默认分支。 More information here .更多信息在这里
  2. Delete the master branch: This can be done by clicking the branch icon and then the delete option in front of the branch name.删除master分支:这可以通过单击分支图标,然后单击分支名称前面的删除选项来完成。 More information here .更多信息在这里

You can then rename OhThisAmazingBranch if you wish to, which is described here .然后可以重命名OhThisAmazingBranch如果你想,这是描述在这里

Ideally you should not have to hack around your master branch in this way.理想情况下,您不应该以这种方式绕过您的master分支。 Rather, if you don't like the initial direction set by the master branch, then perhaps just create a feature branch and move in the direction you want.相反,如果您不喜欢 master 分支设置的初始方向,那么也许只需创建一个功能分支并朝着您想要的方向移动。 That being said, if you really wanted to do this, you could try resetting master to the first commit locally, then amending the first commit:话虽如此,如果你真的想这样做,你可以尝试将master重置为本地的第一次提交,然后修改第一次提交:

# from local master
git reset --hard <first SHA-1 commit hash>
# make the code changes you want here
git commit --amend -m 'new first commit'
git push --force origin master

Note that the amend step is necessary to introduce the changes you want at the beginning of the master branch.请注意,修改步骤是必要的,以便在master分支的开头引入您想要的更改。

If you're allowed and willing to make a destructive operation then you could try something like this:如果您被允许并愿意进行破坏性操作,那么您可以尝试以下操作:

  1. Checkout your local master branch.签出您的本地主分支。 eg git checkout master例如git checkout master
  2. Reset it to the last commit you want to keep.将其重置为您要保留的最后一次提交。 eg git reset --hard COMMIT_HASH例如git reset --hard COMMIT_HASH
  3. Then force-push it to your remote.然后将其强制推送到您的遥控器。 eg git push -f origin master例如git push -f origin master

Now both your local and remote master branches are the same.现在你的本地和远程 master 分支都是一样的。 You can branch from it and cherry-pick the commits from the other branch.您可以从中分支并从另一个分支中挑选提交。 (There may be conflicts though.) (虽然可能会有冲突。)

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

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