Git - 使本地 HEAD 成为新的主人

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

Git - Make local HEAD the new master

git

提问by Mehdiway

I decided to go back a few commits because the path I followed was wrong. So I checked out Added cordova to .gitignorecommit, and made some modifications. Like illustrated below :

我决定返回一些提交,因为我遵循的路径是错误的。所以我检查了Added cordova to .gitignore提交,并做了一些修改。如下图所示:

enter image description here

在此处输入图片说明

Now when I pushthe new modifications, an error message shows up :
error: src refspec (detached from aad6423) does not match any.

现在,当我推送新修改时,会显示一条错误消息:
error: src refspec (detached from aad6423) does not match any.

How can I tell git to discard the previous commits (in purple) and continue with my local HEAD as master ?

我怎样才能告诉 git 放弃以前的提交(紫色)并继续我的本地 HEAD 作为 master ?

回答by Tavian Barnes

Make HEAD your new local master:

使 HEAD 成为您的新本地master

$ git checkout -B master

Force-push your changes:

强制推送您的更改:

$ git push -f

回答by Max

Even though you don't want that old branch anymore, git really doesn't like rewriting history or discarding changes. Just revert and merge.

即使您不再需要那个旧分支,git 也确实不喜欢重写历史记录或丢弃更改。只需还原并合并。

git branch new_master              # name current detached HEAD
git checkout master                # switch back to master
git revert --no-edit HEAD~4..HEAD  # create commits reverting back to where the history split
git merge new_master               # merge
git branch -d new_master           # don't need it anymore

回答by pjz

So, I would do this in a couple steps:

所以,我会分几个步骤来做到这一点:

git checkout -b new_master

to get a nice ref to what you want the new master to be.

获得一个很好的参考,以了解您希望新主人成为的样子。

git checkout master ; git checkout -b old_master

to keep a ref to the old master in case you want to go back or something later ; you can always delete that branch later once you're sure.

保留对旧主人的引用,以防万一你想回去或以后什么的;一旦确定,您可以随时删除该分支。

git checkout master ; git reset --hard new_master

this will reset the HEAD of the branch you're on (master) to the specified reference (new_master).

这会将您所在分支的 HEAD (master) 重置为指定的引用 (new_master)。

git push -f origin

this will do a force-push of your new master branch to the remote. NOTE that this is bad practice if anyone else is using your remote repo as it will potentially break their fetches/pulls.

这会将您的新主分支强制推送到远程。请注意,如果其他人正在使用您的远程存储库,这是一种不好的做法,因为它可能会破坏他们的提取/拉取。

回答by isherwood

Because you have divergence, you'll need to destroy the remote master and push up the local version. Depending on the security in place, you may not be able to do so. This has other implications as well, depending on who else is doing work based on master. It should be done with extreme caution.

因为你有分歧,你需要销毁远程master并上推本地版本。根据适当的安全措施,您可能无法这样做。这还有其他含义,这取决于还有谁在基于 master 进行工作。应极其谨慎地进行操作

git push origin :master // deletes remote master
git push origin master  // pushes local master to remote

Another (probably better) approach would be to revert the commits to master and commit the reverts (which themselves are commits). Then cherry-pick the work you've done on your local. First, create a new topic branch locally to save your work.

另一种(可能更好)的方法是将提交还原为 master 并提交还原(它们本身就是提交)。然后挑选您在本地完成的工作。首先,在本地创建一个新的主题分支以保存您的工作。

git checkout -b <topic_branch_name>  // create new branch to save local work
git checkout master
git reset --hard HEAD // sync local master to remote HEAD

git revert <last commit to master>
git revert <second-to-last commit to master>
...
git revert <Added cordova to .gitignore commit>
git push

git cherry-pick <commit hash from topic branch commit(s)>

回答by dan

Since you pushed the changes upstream, the better approach is to revert them with another commit. A commit that will undo the changes. Removing commits or branches from upstream is badpractice. See this answerfor more details.

由于您将更改推送到上游,更好的方法是通过另一个提交还原它们。将撤消更改的提交。从上游删除提交或分支是不好的做法。有关更多详细信息,请参阅此答案