在git中将单个文件恢复到以前的版本 [英] Reverting a single file to a previous version in git

查看:609
本文介绍了在git中将单个文件恢复到以前的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过文件上的不同提交。
说我修改了一个文件5次,我想回去修改2,在我已经提交并推送到一个仓库之后。



在我的理解中唯一的办法是保持很多分支,我有那个对吗?
如果我是对的,我会在几天内拥有数百家分支机构,所以我可能不会真正理解它。



任何人都可以清除我们先从定性描述我们想要做什么(其中大部分内容在Ben Straub的着作中说过)回答)。我们做了一些提交,其中五个提交了一个给定的文件,我们想把文件恢复到以前的版本。首先,git不保留单个文件的版本号。它只是跟踪内容 - 提交实质上是工作树的快照,以及一些元数据(例如,提交消息)。所以,我们必须知道哪个提交有我们想要的文件的版本。一旦我们知道了,我们需要做一个新的提交将文件恢复到该状态。 (我们不能仅仅关注历史,因为我们已经推出了这些内容,并且与其他人一起编辑历史记录。)

因此,让我们开始寻找正确提交。你可以很容易地看到提交的文件很容易修改:

  git log path / to / file 

如果您的提交信息不够好,并且您需要查看对文件做了什么每个提交都使用 -p / - patch 选项:

  git log -p path / to / file 

或者,如果您更喜欢gitk的图形视图

  gitk path / to / file 

您也可以通过查看菜单启动gitk来执行此操作;其中一个视图选项是要包含的路径列表。



无论哪种方式,您都可以找到提交的SHA1(哈希)你想要的文件版本。现在,你只需要这样做:

 #从给定提交中获取文件的版本
git结账< commit> path / to / file
#并提交此修改
git commit

( checkout命令首先将文件读入索引,然后将其复制到工作树中,因此不需要使用 git add 将其添加到索引中以准备提交。)



如果您的文件可能没有简单的历史记录(例如重命名和复制),请参阅VonC的出色评论。可以定向 git 来更加仔细地搜索这样的东西,但是会以速度为代价。如果你确信历史很简单,那么你就不用担心了。


Is there a way to go through different commits on a file. Say I modified a file 5 times and I want to go back to change 2, after I already committed and pushed to a repository.

In my understanding the only way is to keep many branches, have I got that right? If I'm right I'm gonna have hundreds of branches in a few days, so I'm probably not understanding it really.

Could anyone clear that up please?

解决方案

Let's start with a qualitative description of what we want to do (much of this is said in Ben Straub's answer). We've made some number of commits, five of which changed a given file, and we want to revert the file to one of the previous versions. First of all, git doesn't keep version numbers for individual files. It just tracks content - a commit is essentially a snapshot of the work tree, along with some metadata (e.g. commit message). So, we have to know which commit has the version of the file we want. Once we know that, we'll need to make a new commit reverting the file to that state. (We can't just muck around with history, because we've already pushed this content, and editing history messes with everyone else.)

So let's start with finding the right commit. You can see the commits which have made modifications to given file(s) very easily:

git log path/to/file

If your commit messages aren't good enough, and you need to see what was done to the file in each commit, use the -p/--patch option:

git log -p path/to/file

Or, if you prefer the graphical view of gitk

gitk path/to/file

You can also do this once you've started gitk through the view menu; one of the options for a view is a list of paths to include.

Either way, you'll be able to find the SHA1 (hash) of the commit with the version of the file you want. Now, all you have to do is this:

# get the version of the file from the given commit
git checkout <commit> path/to/file
# and commit this modification
git commit

(The checkout command first reads the file into the index, then copies it into the work tree, so there's no need to use git add to add it to the index in preparation for committing.)

If your file may not have a simple history (e.g. renames and copies), see VonC's excellent comment. git can be directed to search more carefully for such things, at the expense of speed. If you're confident the history's simple, you needn't bother.

这篇关于在git中将单个文件恢复到以前的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆