美文网首页Git
Git fix accidently amended commi

Git fix accidently amended commi

作者: JaedenKil | 来源:发表于2021-11-29 19:03 被阅读0次

    Scenario:
    Need to update a public method "strToNum()".
    The key command:

    git reset --soft xxx
    

    Steps:

    1. Checkout master, pull the lastest changes:
    git switch master; git pull
    // Say the latest commit is "add support s support"
    
    1. Create a new branch and make changes:
    git swtich -c fixBugxxx
    // Make some chnages to "strToNum()" and add a new test case, say "strToNumTest02()"
    git add -u
    git commit -m "Update strToNum() to fix bug xxx"
    

    The issue is: when I add a new test case, I accidently wrote a wrong test case, which will absolutely fail.
    And we have a git hook to run test cases every time when we add a new commit:

    $ cat .git/hooks/pre-commit
    #!/bin/sh
    ./gradlew check
    

    So the git commit command fails.

    1. I fixed the test case strToNumTest02(), and try to amend the changes to the earlier commit "Update strToNum() to fix bug xxx".
      Which leads to the issue, I thought the changes will be amended to Update strToNum() to fix bug xxx, but actually they are amended to the earlier commit add support s support.
    // Update strToNumTest02()
    git add -u
    git commit --amend --no-edit
    
    1. So I need to restore my branch fixBugxxx to align with the master, and save the changes to a new commit
    git status // Check if it's clean
    git reset --soft origin/master
    git diff --cached // Double check the changes to be committed
    git commit -m "Update strToNum() to fix bug xxx"
    git push -u origin fixBugxxx
    
    1. Make a MR in the remote repository.

    And it's fixed.
    The key command:

    git reset --soft xxx
    

    相关文章

      网友评论

        本文标题:Git fix accidently amended commi

        本文链接:https://www.haomeiwen.com/subject/gpmlxrtx.html