mirror of
https://github.com/git-learning-game/oh-my-git.git
synced 2024-12-20 20:33:11 +01:00
31 lines
970 B
Text
31 lines
970 B
Text
|
title = Undo a bad commit
|
||
|
cards = reset commit-a
|
||
|
|
||
|
[description]
|
||
|
|
||
|
Oh no, we made a bad commit! How can we undo making the commit, and go back to a point where we can try again?
|
||
|
|
||
|
The answer is using `git reset [commit]`, which does two things:
|
||
|
|
||
|
- It resets the current branch ref to the commit you specify.
|
||
|
- And it resets the index to that commit.
|
||
|
|
||
|
It does not change your working directory in any way, which means that after that, you can try making the commit you want again.
|
||
|
|
||
|
[setup]
|
||
|
|
||
|
echo "1 2 3 4" > numbers
|
||
|
git add .
|
||
|
git commit -m "Initial commit"
|
||
|
echo "1 2 3 4 5 6 7 8 9 11" > numbers
|
||
|
git commit -am "More numberrrrrs"
|
||
|
|
||
|
[win]
|
||
|
|
||
|
# In the last main commit, the numbers file contains the numbers from 1 to 10.
|
||
|
test "$(git show main:numbers)" = "1 2 3 4 5 6 7 8 9 10"
|
||
|
# The commit message of that commit is "More numbers".
|
||
|
git log -1 --oneline | grep "More numbers"
|
||
|
# The commit with the typo is not part of the main branch anymore.
|
||
|
git log --oneline | grep -v "rrrrr"
|