从git还原特定提交

我有一个git树有很多提交和很多文件。现在,我想还原触摸文件的特定提交。解释:

 psankar@linux-9dni:~/specific> git init
Initialized empty Git repository in /home/psankar/specific/.git/
psankar@linux-9dni:~/specific> echo "File a" > a
psankar@linux-9dni:~/specific> git add a ; git commit -m "File a"
[master (root-commit) 5267c21] File a
 1 file changed, 1 insertion(+)
 create mode 100644 a
psankar@linux-9dni:~/specific> echo "File b" > b
psankar@linux-9dni:~/specific> git add b; git commit -m "File b"
[master 7b560ae] File b
 1 file changed, 1 insertion(+)
 create mode 100644 b
psankar@linux-9dni:~/specific> echo "File c" > c
psankar@linux-9dni:~/specific> git add c; git commit -m "File c"
[master fd6c132] File c
 1 file changed, 1 insertion(+)
 create mode 100644 c
psankar@linux-9dni:~/specific> echo "b and c modified" > b ; cp b c
psankar@linux-9dni:~/specific> git commit -a -m "b and c modified"
[master 1d8b062] b and c modified
 2 files changed, 2 insertions(+), 2 deletions(-)
psankar@linux-9dni:~/specific> echo "a modified" > a
psankar@linux-9dni:~/specific> git commit -a -m "a modified"
[master 5b7e0cd] a modified
 1 file changed, 1 insertion(+), 1 deletion(-)
psankar@linux-9dni:~/specific> echo "c modified" > c
psankar@linux-9dni:~/specific> git commit -a -m "c modified"
[master b49eb8e] c modified
 1 file changed, 1 insertion(+), 1 deletion(-)
psankar@linux-9dni:~/specific> git log --pretty=oneline c
psankar@linux-9dni:~/specific> git log --pretty=oneline c | cat
b49eb8e03af331bddf90342af7d076f831282bc9 c modified
1d8b062748f23d5b75a77f120930af6610b8ff98 b and c modified
fd6c13282ae887598d39bcd894c050878c53ccf1 File c
psankar@linux-9dni:~/specific>

现在我想恢复只是两个提交b49eb8e03af331bddf90342af7d076f831282bc9和1d8b062748f23d5b75a77f120930af6610b8ff98,而不恢复更改为。 IOW只还原文件中的提交(不还原其他中间提交(可能有数千个)在不同的文件)这是怎么回事?

你可以使用git revert与–no-commit选项。在您的示例中:

$ git revert --no-commit b49eb8e 1d8b062
# Files that were modified in those 2 commits will be changed in your working directory
# If any of those 2 commits had changed 'a' then you could discard the revert for it:
$ git checkout a
$ git commit -a -m "Revert commits b49eb8e and 1d8b062"

如果不提供提交消息,则准备好的消息将在提交消息编辑器启动时可用。

如果您忽略了–no-commit选项,那么您指定的提交中的更改将被还原。这是通过在指定的提交中应用相反的更改并提交。这将导致一个新的提交,原始和恢复的提交将在您的存储库的历史。

http://stackoverflow.com/questions/20324638/reverting-specific-commits-from-git

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:从git还原特定提交