I was today years old when I learned how to use the command git mv. It’s not a challenging command to understand, but sometimes doing things the hard way makes the shortcuts more enjoyable. Case in point, my most recent blog post.

The “hard” way

At the end of Resurrection, just before Three years later…, there’s a code block where I move my newly merged not-dead/ subdirectory to a libs/ directory. Here’s how it was done.

# We're still inside the mono/ repo...
$ mkdir libs
$ mv not-dead libs/
$ git add . && git commit  # Don't forget to commit the directory movement!

It’s an easy one, two, three (four if you separate git add and git commit) steps.

  1. Make the libs/ directory (assuming it doesn’t already exist).
  2. Move the not-dead/ directory inside the libs/ directory.
  3. Add the changes with git (not doing this makes it look like the not-dead/ directory was deleted and a new, untracked libs/not-dead/ directory was created).
  4. Commit changes.

But what if we could cut it down to two?

The shortcut

Let’s pretend we didn’t run any of the commands in the previous section and start fresh.

# We're still inside the mono/ repo...
$ git mv not-dead/ libs/
$ git commit

That’s it! Everything we did the “hard” way can be achieved in those two lines. Even better, we could combine them into a single line:

# We're still inside the mono/ repo...
$ git mv not-dead/ libs/ && git commit

Isn’t that convenient? 🙂