Git撤销commit中单个文件的修改

转自: https://blog.csdn.net/FIRE_TRAY/article/details/49762449

问题背景

笔者最近在实习发现了这么一个问题,push了一个提交但是这个提交中误删了两个文件,现在需要撤销错误的删除并重新提交

解决过程

  • 首先查询这个文件的log
1
$ git log <fileName>
  • 其次查找到这个文件的上次commit id xxx,并对其进行reset操作
1
$ git reset <commit-id> <fileName>
  • 再撤销对此文件的修改
1
$ git checkout <fileName>
  • 最后amend一下,再push上去
1
2
$ git commit --amend
$ git push origin <remoteBranch>

pop local commit

git reset –soft HEAD~1

HEAD~4^2 meaning

X~n means: The nth ancestor of X.

X^ means: The parent of X. This is equivalent to X~1.

If X has more than one parent, one needs to distinguish between them when using the ^ notation. So X^1 would be the first parent, X^2 would be the second parent, and so on. X^ is equivalent to X^1 (and also equivalent to X~1).

In your example, starting from commit 9ec05ca, which is HEAD:

db7e87a is HEAD~1 (or alternatively HEAD^). 796ddb0 is HEAD~2 (or alternatively HEAD^^). 1a56a81 is HEAD~4 (or alternatively HEAD^^^^, but nobody would use that). e014d91, being the first parent of 1a56a81, is HEAD~5, or HEAD~4^, or HEAD~4^1. f69811c, being the second parent of 1a56a81, is HEAD~4^2.

https://git-scm.com/docs/gitrevisions

Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计