Amending/Undoing
There are multiple ways you can amend/undo previous actions in Git:
- Modifying the most recent commit using git commit --amend
- Modifying commits previous to the most recent, requiring an interactive rebase (see Interactive Rebasing for more)
- Unstaging a staged file using git reset
- Unmodifying a modified file using git reset
- Unstaging a staged file using git restore
- Unmodifying a modified file using git restore
These tools can help you fix mistakes, leading to a cleaner commit history. Be aware that, unlike most Git commands, some of these methods can cause irreversible changes to the affected files, and there is the risk that work could be lost. Proceed with caution.
- Modifying the most recent commit using git commit --amend
- Modifying commits previous to the most recent, requiring an interactive rebase (see Interactive Rebasing for more)
- Unstaging a staged file using git reset
- Unmodifying a modified file using git reset
- Unstaging a staged file using git restore
- Unmodifying a modified file using git restore
These tools can help you fix mistakes, leading to a cleaner commit history. Be aware that, unlike most Git commands, some of these methods can cause irreversible changes to the affected files, and there is the risk that work could be lost. Proceed with caution.
Use Cases:
- Creating a new feature or fixing a bug
- Starting work on a new project
- Collaborating with others
- List all the use cases here...
Examples:
Undoing Changes
1. Initialize a repository:
git init
2. Create a file (app.txt) with:
Initial content
3. Stage and commit the file:
git add .
git commit -m "Add app.txt"
git commit -m "Add app.txt"
4. Edit app.txt and add changes you don’t want (imagine you've been coding for awhile and it gets insanely
broken)
Initial content with an unimaginable amount of mistakes here
5. Restore the file back to the last committed version:
git restore app.txt
6. Create a file that you don’t want to commit (unwantedFile.txt):
This file should not be committed
7. Stage everything (including the file that you don't want):
git add .
8. Unstage ONLY the file you didn’t mean to add:
git restore --staged unwantedFile.txt
9. Edit app.txt again and add a small change:
Initial content
Added a new line
Added a new line
10. Commit the changes before you’re ready:
git add .
git commit -m "Add new line"
git commit -m "Add new line"
11. Undo the commit, but keep the changes so you can fix it:
git reset --soft HEAD~1
Amending Commits
12. Create a file you meant to include earlier (notes.txt):
Some notes go here
13. Stage + commit the file:
git add .
git commit -m "Add a file"
git commit -m "Add a file"
14. Fix the commit message to make it clearer:
git commit --amend -m "Add notes.txt"
15. Add another small change to App.txt:
Initial content
Added a new line
Another update
Added a new line
Another update
16. Add those changes to the most recent commit instead of creating a new one:
git add app.txt
git commit --amend
git commit --amend
17. Undo a commit safely without rewriting history (This is good for when you're working with other
people!):
git revert HEAD
(This creates a new commit that reverses the previous commit, while still keeping that previous commit in
the history).