gitcontinuous-integrationtravis-citravis-ci-clitravis-ci-api

Not able to do git reset to previous commit in travis


While building my code in travis, in the case of failure I want reset HEAD to the previous commit and perform some action. The problem is when in travis.yml I do:

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

It always points to the latest commit.

If i do git reset --hard HEAD 04d24f1 I am getting fatal: Cannot do hard reset with paths. and printing hashes for some reason shows letter m next to the commit that i tried to reset to

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

If i print history of commits by git reflog -4, then I get only 2 commits and they both have the same hash

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

If i print history of commits by git log --oneline --graph --decorate, then it displays the history like this:

* 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

Additional info - At the beginning this is what travis doing before running commands:

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

Do you know why i'm not able to get reset while building on Travis?


Solution

  • As already pointed out by Sajib Khan, you want

    git reset --hard HEAD~1
    

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


    There is some useful information here, that explains the difference. *In short, HEAD, in your case never pointed anywhere other than (HEAD, origin/feature/RXM-73-create-aws-rollback-script, feature/RXM-73-create-aws-rollback-script), and @ notation is for where head was *.

    You can investiagte the list of possible @{#} values by looking at

    git reflog

    in order to verify that HEAD did not point elsewhere.