Rewriting History
- Consider the following history
- For the third commit i.e. your latest commit the commit there is typo in commit message
- consider the following history
- To rewrite history we need to back by 2 commits and rewrite history
git rebase -i HEAD~2
. This is called as interactive rebase.
- Lets change all the commit messages and add my i.e my commit 1
- combine 3 and 4 commits into one => squash
- Now lets try deleting the commit 3 & 4 which was squashed
- Before delete
- Command
git rebase -i HEAD~3
- Impact of rewriting history when working with remote repositories
- Ensure you do the rewriting history either with teams approval
- Git maintains one more log which is referred as reflog
-
Exercise:
- Create a new local repostory
- create 3 commits (commit 1, commit 2, commit 3)
- note commit id of commit 2
- do interactive rebase to delete commit 2
- now check git log
- Use reflog to recover the deleted commit
- Ignoring some files and folders to be tracked: