A Closer Look At Git Reset & Git Push

I have a pretty limited experience with git. I know the basics of push, pull, commit, checkout, branch... The bare-bones of what you need to make use of this tool. Not efficient use, though; I have made plenty of mistakes and backed myself into bizarre corners because I don't fully understand what a certain command does. In an attempt to familiarize myself more with the powerful commands at my disposal, and hopefully reduce the number of times I turn my repositories into some eldritch abomination, I decided to take a closer look at two of my favourite git commands: reset and rm.

This article assumes you have a basic understanding of Git, and know what HEAD and index refer to. If you don't, you'll definitely want to check out the docs before reading on.

Git Reset

What It Does

According to the official docs, git reset reverts the current HEAD to the specified state. It does this by following these three steps:

  1. Change the branch that HEAD points to
  2. Refactor the index to contain information about the files that HEAD now points to
  3. Modify the files on the working tree so it resembles index

Depending on the flags you use, you may stop after one or two steps. The default is to stop after step two. We'll cover how to go beyond that, or just stop after step one, in the modes section.

Modes

I don't feel it's necessary to go through every possible option for reset. Instead, I'll just cover the ones I think are important. If you want to check out all of them, take a look at the official documentation.

--SOFT

Changes what HEAD points to, and does not modify the index or working tree (stops after step one).

--MIXED

Changes what HEAD points to, and modifies the index, but not the working tree (stops after step two). --MIXED is the default mode, so if you fail to specify,

--HARD

Changes what HEAD points to, modifies the index and the working tree. If any changes have been made to the files in the working tree, they're discarded -- in other words, you lose those changes. This option makes reset very dangerous. I can speak from experience on this one -- before I understood reset, I would constantly use the --HARD option... And then get frustrated when my files were irrevocably changed.

--MERGE

This is a pretty interesting mode. It will reset he index, and then update the files in the working tree that are different from the specified commit. But, it will keep files that are different in the working tree than they are in the index.

--KEEP

Similar to --merge, this one resets the index and updates the files in the working tree. This one will only update files that are different between commit and HEAD, though.

Options

Oddly enough, there are a whopping 3 options for reset: -q, -quiet, and --no-quiet, which all do the same thing: perform the task quietly and only report errors.

Git RM

What It Does

Git rm will remove files from both the working tree and the index -- something to keep in mind when using! Goodness knows I've lost a fair few files this way (oops. My bad for not doing this research beforehand, eh?). With the right option, you can limit removal to just the index.

Options

FILE...

This one's pretty self-explanatory: these are the names of the files you want removed.

-F, --FORCE

In the event you run into issues removing a file, you can use this to force git to remove it anyway. Use this with caution.

-N, --DRY-RUN

This one won't remove files, but instead shows you which ones would be removed if you ran the rm command for real.

-R

Lets you do recursive removal for deletion of files in a directory.

--CACHED

This option will remove files from the index, and the index alone; it won't touch your working tree.

-Q, --QUIET

Stop git rm from outputting a line for each file that it removes. It will only report errors.

Comments