Git fix accidently amended commit

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
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容