Confused by the various options for cleaning up or resetting your branch, working area, or staging area?
Use this overview to help get things sorted:
Command | Description | Affects Working Area | Affects Staging Area | Example Call |
---|---|---|---|---|
git reset (no options) | Resets the current branch to a specific commit and removes changes from the Staging Area, but keeps changes in the Working Area. | No | Yes | git reset <commit-hash> |
git reset --soft | Resets the current branch to a specific commit, but keeps both the Staging Area and the Working Area unchanged. | No | No | git reset --soft HEAD~1 |
git reset --hard | Resets the current branch to a specific commit and discards all changes in the Working Area and the Staging Area. | Yes | Yes | git reset --hard <commit-hash> |
git reset (last commit) | Resets the current branch to the last commit. By default, it affects the Staging Area but not the Working Area. | No | Yes | git reset HEAD~1 |
git reset HEAD | Removes changes from the Staging Area but keeps them in the Working Area. | No | Yes | git reset HEAD <filename> |
git checkout -- | Resets changes in files in the Working Area to the last state of the HEAD commit. Does not affect the Staging Area. | Yes | No | git checkout -- <filename> |
git restore | A newer alternative to git checkout -- for resetting changes in files in the Working Area. Can also be used to remove changes from the Staging Area. | Yes, if without --staged | Optional, with --staged | git restore <filename> |
git clean | Removes untracked files and directories from the working directory. | Yes (only untracked files/directories) | No | git clean -df |
Each of these commands has specific use cases in managing the state of files in a Git repository, affecting either the Working Area, the Staging Area, or both.
Also take a look at our GIT training in Vienna or Remote: GIT Training – ciit-training.com
For further reading we recommend the free Pro Git Book: Pro Git free book