git reset affects only the local repository. It does not automatically affect the remote repository. If you have pushed commits to a remote repository and then use git reset, you will need to force push (git push --force) to update the remote branch, which can rewrite history and potentially disrupt collaborators.
Example Code
-
Reset to a previous commit and keep changes staged:
git reset --soft HEAD~2
This will move the HEAD to two commits ago and keep changes staged.
-
Unstage all files and keep changes:
git reset
This will unstage all changes but leave them in the working directory.
-
Discard all local changes and reset to the latest commit:
git reset --hard HEAD
This will reset the working directory and the staging area to the latest commit, discarding all local changes.
-
Undo a commit but keep changes in the working directory:
git reset --mixed HEAD~1
This will undo the last commit and unstage the changes, leaving them in the working directory.