简体   繁体   English

如何返回上一个主分支

[英]How to go back to the previous master branch

I have a serious problem with git. 我对git有严重的问题。 One of my team members decided to do a Git commit without before doing a git pull command so the master head was behind so I decided to do a git push --force. 我的一个团队成员决定在执行git pull命令之前不进行Git提交,因此主控头在后面,所以我决定进行git push --force。 Now the git branches are all messed up and I don't know how to change it. 现在,git分支都被弄乱了,我不知道如何更改它。 I have attached a screenshot to show what I am talking about. 我已附上屏幕截图,以显示我在说什么。

在此处输入图片说明

First off, don't panic! 首先,不要惊慌!

Next, ask that no one else push or pull until the repo you pushed to is neat and tidy again. 接下来,要求其他人不要推或拉,直到您推入的回购变得整洁。 If you go to that particular machine, you can use git reflog to see where it was before this happened. 如果转到该特定机器,则可以使用git reflog查看发生此情况之前的位置。 Other co-workers might also have in their repo a copy of this remote repo's master branch where it used to be, if they haven't pulled in the messy version. 如果其他同事没有拉入凌乱的版本,则他们的回购中可能还会有该远程回购的master分支的副本。 Use either method to find the hash of the last sensible commit. 使用任何一种方法来查找上一次明智提交的哈希。

If the remote server's a regular repo, not headless, then you can simply use git checkout master to make sure you're on the master branch, then git reset [last known good commit hash] --hard to set it back to how it was. 如果远程服务器是常规仓库,而不是无头仓库,那么您可以简单地使用git checkout master来确保您位于master分支上,然后git reset [last known good commit hash] --hard很难将其设置回原来的状态是。 This simply pretends that everything since that commit didn't happen. 这只是假装自该提交以来没有发生任何事情。 This kind of thing can be dangerous if you're not sure what you're doing, so make sure you make a backup first! 如果您不确定自己在做什么,这种事情可能很危险,因此请确保先进行备份!

Now that repo's clean again, everyone else can pull from it and push to it again. 现在该存储库再次干净,其他所有人都可以从中取出并再次推送到它。

Next up is the person who made the problematic commit. 接下来是做出有问题的承诺的人。 They can do the same thing: git checkout master to go to their local copy of the master branch, then git reset [last known good commit hash that they've already pulled in] --hard to move it back to where it originally was, then git pull [remote server name] master to get the subsequent changes. 他们可以做同样的事情: git checkout master转到其master分支的本地副本,然后git reset [last known good commit hash that they've already pulled in] --hard引入的git reset [last known good commit hash that they've already pulled in] --hard很难将其移回原来的位置,然后git pull [remote server name] master进行后续更改。

Then they'll have to merge in their changes again. 然后,他们将不得不再次合并他们的更改。

Key points to remember are: 要记住的关键点是:

  • When things go wrong, make a backup before doing anything drastic! 如果出现问题,请在做任何大动作之前先进行备份!
  • Don't panic, you can use Git to essentially rewrite history 别着急,您可以使用Git实质上重写历史记录
  • git reflog tells you what you've had checked out, which is useful for undoing mistakes git reflog告诉您已检查的内容,这对于消除错误很有用
  • git reset [hash] --hard pretends that everything since that hash didn't happen. git reset [hash] --hard假装没有发生任何事情。

In the future, if you need to force something, think if there's something you should check to do first. 将来,如果您需要强制执行某些操作,请考虑是否需要先执行一些操作。 If you're fighting against Git, it's usually a sign you've overlooked something. 如果您要与Git对抗,那通常就是您忽略了某些东西的迹象。

Since you're a team, there's a chance that one of your team members have the old master in their local system. 由于您是一个团队,因此您的团队成员中有一个在其本地系统中拥有旧master的机会。 Simply force push from that system to restore your remote Git repo. 只需从该系统强制推送即可恢复您的远程Git存储库。

Then go back to the team member who created the recent commits, pull the old master onto his system, resolve conflicts if any and push like normally done. 然后回到创建最近提交的团队成员,将旧的master拖入他的系统,解决冲突(如果有的话),然后像往常一样推送。

The easiest for you, is go back by one commit with your master branch, which would be something like: 对您而言,最简单的方法是使用master分支进行一次提交,该操作类似于:

git revert HEAD~1

Check also the answer given here for a detailed description of different revert commits. 还要检查此处给出的答案,以获取不同还原提交的详细说明。

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

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