GIT – Undoing Changes

Undo Uncommitted Changes

Before we start undoing things, let’s take a look at the status of our repository.

git status

We have a tracked file and an untracked file that need to be changed. First, we’ll take care of index.html:

git reset --hard

This changes all tracked files to match the most recent commit. Note that the --hard flag is what actually updates the file. Running git reset without any flags will simply unstage index.html, leaving its contents as is. In either case, git reset only operates on the working directory and the staging area, so our git log history remains unchanged.

Next, let’s remove the dummy.html file. Of course, we could manually delete it, but using Git to reset changes eliminates human errors when working with several files in large teams. Run the following command.

git clean -f

This will remove all untracked files. With dummy.html gone, git statusshould now tell us that we have a “clean” working directory, meaning our project matches the most recent commit.

Be careful with git reset and git clean. Both operate on the working directory, not on the committed snapshots. Unlike git revert, theypermanently undo changes, so make sure you really want to trash what you’re working on before you use them.

Revisions

No comments yet.

Leave a Reply