[英]How to revert file to previous version
Let's say I'm working on branch A and I merged some changes from branch B. Branch B has a file named setup.sh and I work on branch A and modify the setup.sh file and commit it and later perform let's say 3 different commits on other files of branch A.假设我在分支 A 上工作并且我从分支 B 合并了一些更改。分支 B 有一个名为 setup.sh 的文件,我在分支 A 上工作并修改 setup.sh 文件并提交它,然后执行让我们说 3 个不同的提交分支 A 的其他文件。
Now I realise that I should not be making the changes in setup.sh file.现在我意识到我不应该在 setup.sh 文件中进行更改。 I want to revert back to the branch B version of setup.sh.我想恢复到 setup.sh 的分支 B 版本。 Obvious solution is git checkout and git commit, but it shows file modified by me in git blame.明显的解决方案是 git checkout 和 git commit,但它在 git blame 中显示我修改过的文件。 I do not want that.我不要那个。 Any idea how to proceed?知道如何进行吗?
To revert to a particular version of the file, you must make a new commit that contains that version of that file (along with its version of every other file, just like any other commit).要恢复到文件的特定版本,您必须进行包含该文件版本的新提交(以及其他每个文件的版本,就像任何其他提交一样)。
Obvious solution is git checkout and git commit, but it shows file modified by me in git blame.明显的解决方案是 git checkout 和 git commit,但它在 git blame 中显示我修改过的文件。
That will always be the case, because git blame
assigns the ownership of each line in a file based on the difference between each snapshot.情况总是如此,因为git blame
会根据每个快照之间的差异分配文件中每一行的所有权。 That is, we are given the fact that we have a series of commits:也就是说,我们得到了一系列提交的事实:
... <-F <-G <-H <--branch
where the branch name branch
tells Git that the last commit is commit H
, and commit H
tells Git that the previous commit is commit G
, with commit G
telling Git that the previous commit is commit F
, and so on.其中分支名称branch
告诉 Git最后一次提交是 commit H
, commit H
告诉 Git前一次提交是 commit G
, commit G
告诉 Git 前一次提交是 commit F
,依此类推。 What git blame
does is extract the file from commit H
, then extract the same file from commit G
. git blame
所做的是从提交H
提取文件,然后从提交G
提取相同的文件。 Where the lines match, the ownership moves from H
to G
, but we're not done yet.在行匹配的地方,所有权从H
移动到G
,但我们还没有完成。 Where the lines don't match, the ownership is at H
.如果行不匹配,则所有权位于H
。
Now, if we're at G
, git blame
extracts the file from commit G
and again from commit F
.现在,如果我们在G
, git blame
从提交G
和提交F
再次提取文件。 Where the lines match, the ownership moves back to F
, and where they don't, the ownership stays at G
.在线条匹配的地方,所有权移回F
,而在它们不匹配的地方,所有权保持在G
。
Once we're at F
, Git extracts the file from the parent of F
, and repeats this whole process.一旦我们到达F
,Git 从F
的父级中提取文件,并重复整个过程。
Eventually, each line has an assigned commit, and that's what you see to the left of the line.最终,每一行都有一个指定的提交,这就是您在该行左侧看到的内容。
This means that no matter how you restore the file to the old commit , it's you that restored the file, and git blame
will stop there.这意味着无论您如何将文件恢复到旧的 commit ,都是您恢复了文件,而git blame
将停在那里。 To keep going, you must run a second git blame
that says starting from before my commit, look backwards the way you always do and now git blame
will assign the lines to earlier commits.为了继续下去,你必须运行第二个git blame
,它说从我提交之前开始,向后看你一直做的事情,现在git blame
将把这些行分配给更早的提交。 Git starts at an end and works backwards and if you want it to start at an end that comes before your commit, you simply start it there, instead of at the current commit (or the current working tree copy, which is where we really have git blame
start, most of the time). Git 从一个结尾开始向后工作,如果你希望它在你提交之前的一个结尾开始,你只需从那里开始,而不是在当前提交(或当前工作树副本,这是我们真正拥有的地方)大多数情况下git blame
开始)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.