签出旧的提交并创建一个新的主分支?

时间:2021-11-28 22:56:04

As it seems, the master branch of one of my project's went into a completely wrong direction. So what I want to do is:

看起来,我的一个项目的主分支进入了一个完全错误的方向。所以我想做的是:

  • Checkout an older commit
  • 签出较旧的提交
  • Develop things into a different direction using a new branch
  • 使用新分支将事物发展到不同的方向
  • Call this new branch master and discontinue the old master branch
  • 调用此新分支主服务器并停止旧主分支

How do I do this?

我该怎么做呢?

I have seen that I could create a new branch branch1 and then use

我已经看到我可以创建一个新的分支branch1然后使用

$ git branch -m master old_master
$ git branch -m branch1 master

to backup the old master and then rename the new branch to master, but I wonder whether there are any drawbacks in this, and whether there is a better way to achieve my goal (whatever "better" means in this context).

备份旧的master然后将新的分支重命名为master,但我想知道这是否有任何缺点,以及是否有更好的方法来实现我的目标(在这种情况下无论“更好”意味着什么)。

Any thoughts or hints?

有什么想法或提示吗?

PS: The changes I want to "revert" have already been pushed, so I can not just delete them.

PS:我想要“恢复”的更改已被推送,所以我不能删除它们。

2 个解决方案

#1


1  

The approach you tell is really the good one. Using hard reset is ok only if you are open to loosing the development history till that point.

你说的方法真的很好。只有在您放弃开发历史记录之前,才能使用硬重置。

Basically, do the following

基本上,请执行以下操作

git branch -m master old_master
git branch -m branch1 master

After you've done it, you will need to force push the master (because your local branch history would be completely different from that on the remote)

完成后,您将需要强制推送主(因为您的本地分支历史将与远程上的完全不同)

git push -f origin master

Also note that in case there are multiple developers working on the master branch, their local master branch might be corrupted if they do a pull, so its best they re-create master branch on their machines.

另请注意,如果有多个开发人员在主分支上工作,如果他们执行拉取操作,他们的本地主分支可能会损坏,因此最好在他们的计算机上重新创建主分支。

git branch -D master
git branch master --track origin/master

Both the forced push and (possibly) recreating the master branch will be needed with with hard reset as well.

强制推送和(可能)重建主分支都需要具有硬重置功能。

#2


1  

1.You have to reset hard on your master branch to the sha-id you want to rollback to

1.您必须在主分支上重置为要回滚的sha-id

git reset --hard <commit_id>
  1. While doing the push, you would have to use the -f option
  2. 在执行推送时,您必须使用-f选项

You can also try to do a git revert instead of git reset

你也可以尝试做一个git revert而不是git reset

PS: if you want to keep the history around , you can tag master branch with a tag name or even create a branch named master_old

PS:如果要保留历史记录,可以使用标记名称标记master分支,甚至可以创建名为master_old的分支

#1


1  

The approach you tell is really the good one. Using hard reset is ok only if you are open to loosing the development history till that point.

你说的方法真的很好。只有在您放弃开发历史记录之前,才能使用硬重置。

Basically, do the following

基本上,请执行以下操作

git branch -m master old_master
git branch -m branch1 master

After you've done it, you will need to force push the master (because your local branch history would be completely different from that on the remote)

完成后,您将需要强制推送主(因为您的本地分支历史将与远程上的完全不同)

git push -f origin master

Also note that in case there are multiple developers working on the master branch, their local master branch might be corrupted if they do a pull, so its best they re-create master branch on their machines.

另请注意,如果有多个开发人员在主分支上工作,如果他们执行拉取操作,他们的本地主分支可能会损坏,因此最好在他们的计算机上重新创建主分支。

git branch -D master
git branch master --track origin/master

Both the forced push and (possibly) recreating the master branch will be needed with with hard reset as well.

强制推送和(可能)重建主分支都需要具有硬重置功能。

#2


1  

1.You have to reset hard on your master branch to the sha-id you want to rollback to

1.您必须在主分支上重置为要回滚的sha-id

git reset --hard <commit_id>
  1. While doing the push, you would have to use the -f option
  2. 在执行推送时,您必须使用-f选项

You can also try to do a git revert instead of git reset

你也可以尝试做一个git revert而不是git reset

PS: if you want to keep the history around , you can tag master branch with a tag name or even create a branch named master_old

PS:如果要保留历史记录,可以使用标记名称标记master分支,甚至可以创建名为master_old的分支