创建分支后恢复许多提交

发布于 2024-12-22 03:06:55 字数 369 浏览 6 评论 0原文

我正在使用 git 来管理我的项目,但我对此类程序没有太多经验,因此我经常不正确地使用它们(例如:我仅在一次提交中提交了许多(不相关的)更改)。我正在尝试更好地利用它们。

顺便说一句,我注意到我开始开发我的项目的扩展,它将更好地适合分支。因此,我从 git gui 创建了一个新分支,它是主分支的副本。 现在我想将主控恢复到我所做的最后一次提交,它与项目无关。我知道这个提交是什么,但我不知道如何将所有提交从 HEAD 恢复到这个提交,而且我不知道这是否会影响分支(失去所有新工作将是一种真正的痛苦..)

那么,我该怎么办?

PS:通常我使用git gui,但使用git bash没有问题。我使用 win7,我正在 eclipse 中管理一个 java android 项目

i'm using git to manage my project, but i'm not much experienced in this kind of programs, so often i use them improperly(eg: i commit many (unrelated) changes in only one commit). I'm trying to using them better.

Btw i noticed that i started to develop an extension of my project that would be better fitting into a branch. So from the git gui i created a new branch that's a copy of the master.
Now i want to revert the master to the last commit i made that it is not relevant to the project. I know what this commit is, but i don't know how to revert all the commits from HEAD to this one, and i don't know if this will influence the branch(it would be a real pain to lose all the new work..)

So, how can i do?

Ps: usually i use git gui, but i have no problem in using git bash. I use win7 and i'm managing an java android project in eclipse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

将军与妓 2024-12-29 03:06:55

如果您已创建分支,请更改回主分支并使用 git reset --hard commitid 将其重置为所需的提交。现在“master”已设置为此提交,并且分支仍然指向您之前配置的提交。

分支名称是特定提交的引用或备用名称。重置允许您更改参考指向的提交并强制工作树匹配(使用--hard)

If you have your branch created, change back to the master and reset it to the desired commit with git reset --hard commitid. Now 'master' is set to this commit and branch still points to the commit you had it configured for previously.

Branch names are references or alternate names for specific commits. reset allows you to change the commit that your reference points at and force the working tree to match (with --hard)

咋地 2024-12-29 03:06:55

master 分支的更改不会影响新分支的历史记录。

如果您想将 master 的 HEAD 更改为特定提交,请使用 重置

git checkout master
git reset --hard <commit id>

如果您的事件顺序是:

  1. 进行重大更改。
  2. 创建新分支。

如果您只想让 master 回到上一次提交之前,请使用 重置

git checkout master
git reset --hard HEAD^

如果您的事件顺序是:

  1. 进行重大更改。
  2. 提交其他更改(应保留在 master 中)。
  3. 创建新分支。

您只想从 master 删除 big Change 提交,同时保持所有其他提交完好无损,请使用 恢复

git checkout master
git revert <commit id>

如果您的事件顺序是:

  1. 提交重大更改。
  2. 提交其他更改(也应从 master 中删除)。
  3. 创建新分支。

并且您想要删除所有最近的提交,包括来自 masterbig Change,请使用 重置

git checkout master
git reset --hard <big change commit id>^

Changes to the master branch will not affect the history for your new branch.

If you want to change master's HEAD to be at a specific commit, use reset:

git checkout master
git reset --hard <commit id>

If your sequence of events were:

  1. Commit big change.
  2. Create new branch.

And you just want to get master back to before the previous commit, use reset:

git checkout master
git reset --hard HEAD^

If your sequence of events were:

  1. Commit big change.
  2. Commit other changes (which should stay in master).
  3. Create new branch.

And you just want to remove the big change commit from master while leaving all other commits intact, use revert:

git checkout master
git revert <commit id>

If your sequence of events were:

  1. Commit big change.
  2. Commit other changes (which should also be removed from master).
  3. Create new branch.

And you want to remove all recent commits up through and including big change from master, use reset:

git checkout master
git reset --hard <big change commit id>^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文