GIT Reset/restore/revert changes
On git-scm you can find a nice description of the differences between the commands:
git-revert is about making a new commit that reverts the changes made by other commits. git-restore is about restoring files in the working tree from either the index or another commit. This command does not update your branch. The command can also be used to restore files in the index from another commit. git-reset is about updating your branch, moving the tip in order to add or remove commits from the branch. This operation changes the commit history.
Git revert is clear. It is not changing history but just adds a new commit. It should be used when the commit that you are addressing already went to others (e.g. has been pushed).
The git restore function restores working tree with content from the source. git restore .
will restore the current directory. It has switches --staged
to restore file in the index and --worktree
to restore from worktree. git restore --source=HEAD --staged --worktree hello.c
Reference
Git reset has different modes. Most popular are the three: soft, hard and mixed. Mixed is the default one so you can omit it. It resets the index but not the working tree. git reset
See undo add example here.
Soft reset leaves index and working tree as they are. All your files are seen as changed and to be committed. git reset --soft
See edit last commit example here.
Hard reset removes files from the index and stage. git reset --hard HEAD~
See move commit to another branch example here. All modes are nicely depicted in a table from git-scm:
working index HEAD target working index HEAD
----------------------------------------------------
A B C D --soft A B D
--mixed A D D
--hard D D D
To remove all current changes execute: git reset --hard HEAD && git clean -dfx
or git restore . git clean -dfx
- Previous: Yarn Unauthorised exception
- Next: PowerShell Runas different user