Git Undoing Changes

git checkout git revert git reset git clean

Whereas reverting is designed to safely undo a public commit, git reset is designed to undo local changes.
Because of their distinct goals, the two commands are implemented differently:
resetting completely removes a changeset,
whereas reverting maintains the original changeset and uses a new commit to apply the undo.

git revert – Example

The following example is a simple demonstration of git revert. It commits a snapshot, then immediately undoes it with a revert.

# Edit some tracked files
# Commit a snapshot
$ git commit -m "Make some changes that will be undone"
# Revert the commit we just created
$ git revert HEAD

git reset & git clean Example

The following example obliterates all changes in the working directory, including new files that have been added.
It assumes you’ve already committed a few snapshots and are experimenting with some new developments.

# Edit some existing files
# Add some new files
# Realize you have no idea what you're doing
# Undo changes in tracked files
$ git reset --hard

# Remove untracked files
$ git clean -df

After running this reset/clean sequence, the working directory and the staging area will look exactly like the most recent commit, and git status will report a clean working directory. You’re now ready to begin again.

The git reset --hard and git clean -f commands are your best friends after you’ve made some embarrassing developments in your local repository and want to burn the evidence. Running both of them will make your working directory match the most recent commit, giving you a clean slate to work with.

Leave a Reply