如何在恢复提交和保留单元测试的同时检查测试?

How to check the test while reverting a commit and preserving the unit test?

我正在使用一个开源项目。实际上我有一个 master 的克隆,我正在编写单元测试。现在在编写单元测试后说 abc.cxx 我想通过还原提交来检查我的测试是否失败(我有实际修复错误的补丁的提交 ID)。

那么通过 git 命令检查这个的最佳方法是什么。我已经尝试 git 还原,但我的测试也被删除了(在 abc.cxx 中),我无法测试该功能。

我是 git 的初学者,任何类型的错误都可能导致我重建 master(6-7 小时)。

我建议:

  1. 在专用的临时分支中提交单元测试,
  2. 还原此分支中的错误修复,
  3. 测试你的单元测试,
  4. 如果一切正常,rebase/merge 你在 master 上的单元测试

演示:

# INIT
$ git init test
Initialized empty Git repository in /tmp/test/.git/
$ cd test/
$ echo bug > file
$ git add file
$ git commit -m"Commit with bugs in it"
[master (root-commit) 498680f] Commit with bugs in it
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 file
$ echo feature > file
$ git commit -am"Fix bug #1"
[master 05540d3] Fix bug #1
 1 files changed, 1 insertions(+), 1 deletions(-)

# (1)
$ echo 42 > unit-test
$ git add unit-test
$ git checkout -b unit-test
A   unit-test
Switched to a new branch 'unit-test'
$ git commit -m"Add unit test for bug #1"
[unit-test 240bc6c] Add unit test for bug #1
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 unit-test
$ git l
* 240bc6c (HEAD, unit-test) Add unit test for bug #1
* 05540d3 (master) Fix bug #1
* 498680f Commit with bugs in it

# (2)
$ git revert 05540d3
Finished one revert.
[unit-test 5f5eb62] Revert "Fix bug #1"
 1 files changed, 1 insertions(+), 1 deletions(-)
$ git l
* 5f5eb62 (HEAD, unit-test) Revert "Fix bug #1"
* 240bc6c Add unit test for bug #1
* 05540d3 (master) Fix bug #1
* 498680f Commit with bugs in it

# (3)
$ # check for bug

# (4)
$ git checkout master
Switched to branch 'master'
$ git rebase 240bc6c
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 240bc6c.
$ git branch -D unit-test
Deleted branch unit-test (was 5f5eb62).
$ git l
* 240bc6c (HEAD, master) Add unit test for bug #1
* 05540d3 Fix bug #1
* 498680f Commit with bugs in it