The main benefit of using a version control system like git is you can undo or redo your last or specific local or remote commits if already pushed.
Git provides you with four levels of stages where you can undo or redo changes.
- Working tree – This is your local git directory (before changes are staged)
- Staging area – This is the in-between state of working tree and local repository (after changes are staged but before commit)
- Local repo – This is your local git repository (after changes are committed)
- Remote repo – This is your central remote repository (after changes are pushed)
Note: Git also has tagging feature which keeps a version of your changes.
Warning: The command given below are for demo only and do not execute them blindly particularly if you are working with a remote repository.
Working tree:
Remove changes in git working tree:
1 2 3 4 5 6 7 8 |
## overwrite local changes git checkout -- ## save local changes so you can re-use them later git stash ## discard local changes to all files, permanently git reset --hard |
Add changes from git working tree to the staging area:
1 2 |
## stage the changes git add |
Staging area:
Undo changes from git staging area (keeping your changes in the working tree):
1 2 3 4 5 |
## unstage the file but keep your changes: git restore --staged ## unstage everything but keep your changes: git reset |
Undo changes from git staging area (discarding all changes):
1 2 3 4 5 |
## discard all local changes, but save them for later: git stash ## discard everything permanently: git reset --hard |
Redo changes from git staging area to the local repository:
1 2 3 |
## add and commit the changes from staging area to local repsoitory git add git commit -m " |
Local repository:
Undo git commits from a local repository (keeping your changes in staging area):
1 2 3 4 5 |
## form last git commit and all changes git reset --soft HEAD~1 ## from any git commit and specific or all changes git checkout |
Undo git commits from a local repository (keeping your changes in the working tree):
1 2 3 4 5 |
## form last git commit and all changes git reset HEAD~1 ## from any git commit and specific or all changes git reset |
Undo commits from a local repository (discarding your changes):
1 2 3 4 5 |
## form last git commit and all changes git reset --hard HEAD~1 ## from any git commit and all changes git revert |
Redo commits from local repository to remote repository:
1 2 3 |
git add git commit -m " git push |
Remote repository:
Undo last git commit in git remote repository:
1 2 3 |
## undo last commit in remote repository git reset --hard HEAD~1 git push |
Undo last git commit message:
Undo last git commit message in local repository:
1 |
git commit --amend -m "updated commit message" |
Undo last git commit message in a remote repository:
1 2 |
git commit --amend -m "updated commit message" git push |
Undo a git tag in remote and local repository
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Delete existing tag in local repository git tag -d ## Delete existing tag in remote repository git push --delete #make the changes ## Create a new tag in local repository git tag ## Create a new tag in remote repository git push |