Git - Part 4: Fixing and Amending commits
Soft reset - resets to the previous state, but leaves your changes
git reset --soft UniqueHash
Hard reset - resets everything to the previous state, deletes your changes
git reset --hard UniqueHash
Amend commit - saves changes to the same previous commit
git commit --amend
Exercise: fixing commit
- Create new Git repository
git init
- Create a new file index.html with content "Hello Wrold!"
touch index.html
vi index.html
- Add and Commit index.html file with message "Initial commit"
git add index.html
git commit -m "Initial commit"
- Check git status (see: On branch master nothing to commit, working directory clean)
git status
- Check git log (see:Initial commit)
git log --oneline
- Open index.html and change text to "Hello World!"
vi index.html
- Add and Commit index.html file with message "Fix typo"
git add index.html
git commit -m "Fix typo"
- Check git log (see: both commits)
git log --oneline
Exercise: amending commit
- Check git log and copy UniqueHash from Initial commit
git log --oneline
- Hard reset repository to the initial commit
git reset --hard UniqueHash
- Review index.html and check that the typo is there
cat index.html
- Open index.html and change text to "Hello World!"
vi index.html
- Add index.html to the staging area
git add index.html
- Check git status (see: Changes to be committed: modified: index.html)
git status
- Amend commit
git commit --amend
- Check git log (see: only initial commit)
git log --oneline
- Review index.html and check that the typo is fixed
cat index.html