Git fix accidently amended commi
2021-11-29 本文已影响0人
JaedenKil
Scenario:
Need to update a public method "strToNum()".
The key command:
git reset --soft xxx
Steps:
- Checkout master, pull the lastest changes:
git switch master; git pull
// Say the latest commit is "add support s support"
- 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.
- 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 toUpdate strToNum() to fix bug xxx
, but actually they are amended to the earlier commitadd support s support
.
// Update strToNumTest02()
git add -u
git commit --amend --no-edit
- 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
- Make a MR in the remote repository.
And it's fixed.
The key command:
git reset --soft xxx