git mv'in: A Command I Wish I Knew Earlier
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.
- Make the
libs/
directory (assuming it doesn’t already exist). - Move the
not-dead/
directory inside thelibs/
directory. - Add the changes with
git
(not doing this makes it look like thenot-dead/
directory was deleted and a new, untrackedlibs/not-dead/
directory was created). - 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? 🙂