Reset and Revert¶
Sometimes we commit changes that we don't intend to commit. If that is the case, we have several options to undo the action.
Revert¶
Info
If you want to undo changes and keep them in your Git history, you can use the
git revert
command
The git revert
command will create a new commit, which reverses all the changes made on the specified commit. Let's say that we want to revert the commit where we create the test
file.
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"
commit id: "Revert docs: create test"
- In the "docs: create test" commit,
test
file has created. The reverted commit contains all the opposite changes. It will then deletes thetest
file.
Reset¶
The another way is to use the git reset
command. There are three ways to reset the commit to undo it.
- soft
- hard
- mixed
soft¶
With the --soft
flag, we still have all the changes that we have made, and these changes will move to staged changes. You can use git status
to check all the staged changes.
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"
commit id: "docs: create hello"
Let's reset the "docs: create hello" commit with a soft flag. Reset commands also receive the number of commits that we want to reset, in this case we want to reset one commit.
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"
hard¶
With the --hard
flag, we will lose all the changes that we have made on that commit.
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"
commit id: "docs: create hello"
Let's reset the "docs: create hello" commit with a hard flag.
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"
mixed¶
With the --mixed
flag, we still have all the changes that we have made, and these changes will move to unstaged changes. You can use git status
to check all the unstaged changes.
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"
commit id: "docs: create hello"
Let's reset the "docs: create hello" commit with a mixed flag.
---
config:
logLevel: 'debug'
theme: 'default'
themeVariables:
'git0': '#ff0000'
'git1': '#ff00ff'
'git2': '#00ffff'
'git3': '#ffff00'
'git4': '#ff00ff'
'git5': '#00ffff'
---
gitGraph
commit id: "docs: create first story"
commit id: "docs: create second story"
commit id: "docs: create programming"
commit id: "docs: create test"