Git 分支新手 - 如何反转?

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

Git Branch newbie - How to reverse?

gitgit-branchgit-revert

提问by MEM

Not sure if this is a proper way to work with branches.

不确定这是否是使用分支的正确方法。

  • I had this application on structure A.
  • I created a branch Z while I was on that structure A (but I kept my self on the master branch working).
  • While working (on the master branch), I changed that structure and I end up messing the all thing. :)
  • I've never commit nor added those new files to the rep.
  • 我在结构 A 上有这个应用程序。
  • 我在那个结构 A 上创建了一个分支 Z(但我让自己在主分支上工作)。
  • 在工作时(在 master 分支上),我改变了这个结构,最终搞砸了所有事情。:)
  • 我从未向代表提交或添加这些新文件。

Request:

要求:

  • I need to revert to the structure existing on the last commit. Revert All Uncommitted changes and also remove new files and folders that may have been added.
  • 我需要恢复到上次提交时存在的结构。还原所有未提交的更改并删除可能已添加的新文件和文件夹。

How can this be done?

如何才能做到这一点?

回答by Dan Breen

If you created a separate branch that got messed up, all you need to do is switch back to the one you were on before: git checkout A(assuming Ais the name of the branch you were on... or maybe it's master).

如果您创建了一个搞砸了的单独分支,您需要做的就是切换回之前所在的分支git checkout A:(假设A是您所在分支的名称......或者可能是master)。

You can delete the bad branch later, if you want, with git branch -d <branchname>and maybe throw in a -ftoo, if there are unmerged commits.

您可以稍后删除坏的分支,如果你想,有git branch -d <branchname>,也许扔在一个-f过,如果有未合并的提交。

Edit:If your repository contains lots of bad changes you want to remove, you can use git reset --hard HEADto undo them. If there are new files (not tracked by git), git clean -fdwill remove them all. Obviously, use with discretion as they will not be recoverable by git.

编辑:如果您的存储库包含许多要删除的错误更改,您可以使用git reset --hard HEAD来撤消它们。如果有新文件(没有被 git 跟踪),git clean -fd将把它们全部删除。显然,请谨慎使用,因为 git 无法恢复它们。

回答by Dan Ray

I need to reverse to structure A "snapshot" on branch Z

我需要反转到分支 Z 上的结构“快照”

$ git checkout A
$ git branch -d Z
$ git branch Z
$ git reset --hard HEAD~3
$ git checkout Z

In English:

用英语:

Delete your "Z" branch and re-make it from the current state of "A".

删除您的“Z”分支并从“A”的当前状态重新创建它。

Reset your A branch back three commits. That's an EXAMPLE--three is probably not the right depth back in your history to reset to. Change that number to be the right number (and use git logto figure that out the number of commits back that your branches diverged).

将您的 A 分支重置为三个提交。这是一个例子——在你的历史中,三可能不是正确的深度重置。将该数字更改为正确的数字(并用于计算git log分支发回的提交数量)。

Switch to your Z branch and confirm that your changes formerly in "A" are now in "Z".

切换到您的 Z 分支并确认您以前在“A”中的更改现在在“Z”中。

回答by ralphtheninja

I suspect that you only created the branch with:

我怀疑您只使用以下内容创建了分支:

git branch Z

when you in fact wanted to create it and also switch to it. You can create a branch and switch to it by using the -b flag to git checkout, like this:

当您实际上想要创建它并切换到它时。您可以创建一个分支并通过使用 -b 标志来 git checkout 切换到它,如下所示:

git checkout -b Z

What you need to do now is to undo the changes that you have committed to your master branch. This will undo the commit and make the index look just like before you made the commit:

您现在需要做的是撤消您已提交给 master 分支的更改。这将撤消提交并使索引看起来就像提交之前一样:

git checkout master
git reset --soft HEAD^

Then switch to Z and commit the changes from the index (already staged):

然后切换到 Z 并提交索引中的更改(已暂存):

git checkout Z
git commit -m "blah blah"

Hope this helps.

希望这可以帮助。

Oh and yes avoid working directly on master, unless it's a simple bug fix.

哦,是的,避免直接在 master 上工作,除非它是一个简单的错误修复。



Please keep your answer because it contains important notions that I must understand. That didn't work. What I need is to: grab branch Z and place it as a master - is this possible ?

请保留您的答案,因为它包含我必须理解的重要概念。那没有用。我需要的是:抓取分支 Z 并将其作为主节点放置 - 这可能吗?

Of course it is. You can do this in a lot of different ways. If there is only one commit you could merge the Z branch into master to get that commit into master. But I'm assuming that you dont want the commit on the Z branch at all. Then you can do the same but switch branches, e.g.:

当然是这样。你可以用很多不同的方式来做到这一点。如果只有一个提交,您可以将 Z 分支合并到 master 以将该提交提交到 master。但我假设您根本不希望在 Z 分支上提交。然后你可以做同样的事情,但切换分支,例如:

git checkout Z
git reset --soft HEAD^
git checkout master
git commit -m "blah blah"