简体   繁体   English

如何将文件恢复到以前的版本

[英]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 .现在,如果我们在Ggit 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.

相关问题 如何在不覆盖当前更改的情况下将文件还原到以前的版本? - How to revert a file to a previous version without overwriting current changes? 如何将一次提交中的所有文件还原到以前的版本? - How to revert all files in a commit to a previous version? Git(TortoiseGit) - 如何将单个文件还原到以前的版本,然后撤消还原? - Git (TortoiseGit) - How to revert a single file to a previous revision and then undo the revert? 如何在上一次提交中将更改还原到1个文件 - How to revert changes to 1 file in previous commit 恢复到github上的先前本地版本 - Revert to previous local version on github 如何将我的项目还原到以前的版本? [简单解释。] - How to revert my project to a previous version? [Explained in simple terms.] 如何使用 git 简单地查看以前的版本而不恢复或重置? - How to simply view a previous version using git without revert or reset? 恢复为文件的旧版本 - Revert to an older version of a file 恢复到Github中的先前版本,仅在Github中进行编辑 - Revert To Previous Version In Github, only editing in Github 即使我在提交之前 git 添加了所有文件,我也可以恢复到只有一个文件的先前版本吗? - Can I revert to a previous version of just one file even if I git added all files before a commit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM