Git has some amazing capabilities to help undo mistakes.
Below are 8 common git mistakes and steps to fix them via command line.
1. Undo everything
We made some changes but are not happy and want a clean working copy
git command
git restore .
We are working on uncommitted changes. Running git restore will have all changes removed.
2. Get an older copy of a file (or) get a deleted file back
We made a change to a file but now we want to restore it back to the previously committed version. git command
git restore <path_to_file>
example
git restore index.html
The same restore command can also be used to get a deleted file back (i.e. the last commit of the file)
If you want to get a version of the file at a specific commit
git command
- Run git log to see the commit hash you want the file of
git log --oneline
- Run git restore on that hash with source option
git restore --source <hash_id> <file_name>
example
git restore --source aee9226 index.html
3. Fix last commit message (or) add files to prior commit
- To fix a commit message git command
git commit --amend -m "<commit_message>"
example
git commit --amend -m "docs: correct spelling on README file "
amend command rewrites commit history. Do not use amend on changes that are already pushed.
- To add file to previous commit Add to staging area the file that was missed
example
git add .
git commit --amend
Use amend in your local branch before it is pushed to remote
4. Reverting a commit
This is useful when working on remote branch where a bad commit was found and we want to fix it (i.e. revert to a good state)
Revert does not remove the old commit but creates a new commit undoing the prior changes
To revert note the commit hash git command
git log --oneline
git revert <hash_id>
example
git revert 2e91ad2
5. Resetting branch to old state
Imagine we have a branch which has 5 commits C1, C2 to C5. Commits C4 and C4 are bad and we want to go back to commit C3
git command
- Find the commit hash of C3
git log --oneline
- Run reset hard command
git reset --hard <commit_hash>
hard option will move the branch HEAD to commit that was mentioned in reset. It will not keep any changes in staging area.
example
git reset --hard 2e91ad2
6. Recover a deleted commit (or) deleted branch
We ran git reset --hard but we did it on the wrong commit and need to get back deleted commit. Git reflog comes to the rescue
reflog shows all the changes that have been committed to a Git repository in chronological order (most recent action are on the top).
Recover a deleted commit git command
- Run the reflog command
git reflog
- Find the second commit after the reset command and create a branch
git branch <branch-name> <second_commit_after_reset>
example
git branch fixed-branch 2e91ad2
for recovering branch run the git reflog and checkout create branch as above with the commit hash prior to deletion
7. Committed to a main branch instead of feature branch
We accidentally committed to the main branch instead of feature-branch
git command We reset the head of the main branch to the stage prior to commit
git reset HEAD~
We then checkout a new branch and commit changes to it
git checkout -b <new_branch_name>
Followed by regular git process
example
git reset HEAD~
git checkout -b feature-branch
git add .
git commit -m "feat:new login change"
git push
8. Undo git merge
You ran git merge but you needed to make some more changes or want to undo the merge
example
git merge --abort