简体   繁体   English

撤消已提交的更改

[英]Undo committed changes

I learn from a book, called Ryan Hodson - Ry's Git Tutorial, and it says: 我从一本名为Ryan Hodson的书中学习-Ry的Git教程,它说:

Before we undo the changes, we need to return to the master branch. 在撤消更改之前,我们需要返回到master分支。 If we didn't, all of our updates would be on some non-existent branch. 如果我们不这样做,我们所有的更新将位于某个不存在的分支上。 You should never make changes directly to a previous revision. 绝对不要直接对先前的版本进行更改。

What does it mean? 这是什么意思?

In my case, I'm at the tip of the master branch, and i have to revert to the previous commit 就我而言,我处于master分支的顶端,我必须还原到上一次提交

This isn't the most clearly written passage IMO; 这不是国际海事组织写得最清楚的段落。 but after a couple re-reads, I'm pretty sure I've inferred the correct context to understand what it means. 但是经过几遍重读之后,我很确定可以推断出正确的上下文以理解其含义。 (It's the only conceptually-accurate way I can find to interpret this passage, so...) (这是我可以找到的唯一在概念上准确的方法来解释这一段内容,因此...)

Presumably if you've been following the tutorial step by step, then you are currently in "detached HEAD state". 据推测,如果您一直按照本教程进行操作,那么您当前处于“分离的HEAD状态”。 This occurs when you directly check out a commit - usually because you've checked out a commit further back in your branch's history. 当您直接签出提交时就会发生这种情况-通常是因为您已经在分支机构的历史中更进一步签出了提交。 For example if you have 例如,如果您有

A -- B -- C <--(master)

and you checkout the B commit, by saying something like 你检出B犯,说像

git checkout master^

then this puts you in detached HEAD state. 那么这会使您进入分离的HEAD状态。 You could represent this as 您可以将其表示为

A -- B -- C <--(master)
     ^
    (HEAD)

It looks like the tutorial leads you to do this to examine a change, which it now determines you want to undo. 看来该教程指导您执行此操作以检查更改,现在它确定您要撤消更改。

So it's saying that if you don't first checkout master, and you make another commit, you'll end up with 这就是说,如果您先结帐母版,然后又进行一次提交,则最终会得到

A -- B -- C <--(master)
      \
       D 
       ^
       (HEAD)

You'd still be in detached HEAD state, and D would not be "reachable" from any ref. 您仍然处于分离的HEAD状态,并且D不能从任何引用中“到达”。 This isn't always wrong (as the tutorial suggests), but it isn't typically right. 这并不总是错误的(如本教程所建议),但是通常并不正确。 You'd have to take further steps to prevent D from just getting discarded. 您必须采取进一步的措施来防止D被丢弃。

The tutorial uses the phrase "make changes directly to a previous revision", but that's not a precisely accurate description of what you'd be doing. 本教程使用的短语是“直接对先前的版本进行更改”,但这并不是您所做操作的准确描述。 It would be more accurate to say that you typically would avoid staging new changes while in detached HEAD state (but probably the author finds that phrasing too technical/confusing for git novices). 准确地说,您通常会避免在处于分离的HEAD状态下进行新的更改(但作者可能会认为对git新手来说措辞过于技术性/令人困惑)。

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

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