无法 git 重置为之前在 travis 中的提交

Not able to do git reset to previous commit in travis

在 travis 中构建我的代码时,如果失败,我想将 HEAD 重置为之前的提交并执行一些操作。问题是在 travis.yml 我做的时候:

after_failure:
- git reset --hard HEAD@{1}

它总是指向最新的提交。

如果我这样做 git reset --hard HEAD 04d24f1 我得到 fatal: Cannot do hard reset with paths. 并且出于某种原因打印哈希在我尝试重置为

的提交旁边显示字母 m
1b8bc73 (HEAD, origin/feature/RXM-73-create-aws-rollback-script, feature/RXM-73-create-aws-rollback-script) Fail 7
* 04d24f1m Pass 7  << letter m added
* 8c6a51e Fail 6
* 37e3e38 Pass 6

如果我按 git reflog -4 打印提交历史,那么我只会得到 2 次提交,并且它们都具有相同的哈希值

1b8bc73 (HEAD, origin/feature/RXM-73-create-aws-rollback-script, feature/RXM-73-create-aws-rollback-script) HEAD@{0}: checkout: moving from feature/RXM-73-create-aws-rollback-script to 1b8bc736d2297be68e18d13de74dde3f75694072
1b8bc73 (HEAD, origin/feature/RXM-73-create-aws-rollback-script, feature/RXM-73-create-aws-rollback-script) HEAD@{1}: clone: from https://github.com/MaxRepo/terraform-rx-manager-service.git

如果我打印 git log --oneline --graph --decorate 的提交历史,那么它会像这样显示历史:

* 1b8bc73 (HEAD, origin/feature/RXM-73-create-aws-rollback-script, feature/RXM-73-create-aws-rollback-script) Fail 7
* 04d24f1m Pass 7
* 8c6a51e Fail 6
* 37e3e38 Pass 6
* 4ddfd89 Pass 5

附加信息 - 一开始,这是 travis 在 运行 命令之前所做的:

0.74s$ git clone --depth=50 --branch=feature/RXM-73-create-aws-rollback-script https://github.com/MaxRepo/terraform-rx-manager-service.git MaxRepo/terraform-rx-manager-service
Cloning into 'MaxRepo/terraform-rx-manager-service'...
remote: Enumerating objects: 118, done.
remote: Counting objects: 100% (118/118), done.
remote: Compressing objects: 100% (91/91), done.
remote: Total 175 (delta 52), reused 61 (delta 22), pack-reused 57
Receiving objects: 100% (175/175), 32.98 KiB | 8.24 MiB/s, done.
Resolving deltas: 100% (66/66), done.
$ cd MaxRepo/terraform-rx-manager-service
$ git checkout -qf 1b8bc736d2297be68e18d13de74dde3f75694072

你知道为什么我在 Travis 上构建时无法 get reset 吗?

As already pointed out by Sajib Khan, you want

git reset --hard HEAD~1

and not git reset --hard HEAD@{1}.


有一些有用的信息 here 解释了差异。 *简而言之,HEAD,在你的例子中,除了 (HEAD, origin/feature/RXM-73-create-aws-rollback-script, feature/RXM-73-create-aws-rollback-script) 之外,从未指向任何地方,而 @ 符号表示 head was *.

您可以通过查看

来调查可能的 @{#} 值列表

git reflog

为了验证HEAD没有指向别处。