Following yesterday’s tip where you learned how not to panic when presented with a vi editor, today’s tip will cover the scenario of what if you wanted to amend your most recent commit message.
Suppose you had a git commit message “updating readmeee” which you’ve caught immediately. The most straightforward way is git commit -m “new message” –amend
But let’s say you forgot the message flag and now you’re now face-to-face with the vi editor.
DO NOT START IMMEDIATELY TYPING! IT’S A TRAP!
When vi first opens, your cursor will be in command mode, despite seeing the cursor in the editor. (pssst… it’s a trap)
You’ll want to press i The status bar at the bottom will change to show —Insert–
Once you are finished with your edits, hit ESC to enter Command Mode, and then press :wq for write and quit.
and voila, you’ll see in your log that your last commit message has been modified, but the timestamp (and rest of commit history) is still the same.
There’s a joke, “How do you generate a random string? Put a Windows user in front of vi editor and tell them to exit.” Credit to Aaron Jorbin.
Suppose you’re doing research on the git commit –amend -m “some message” command and you forgot the -m “some message” part.
Everything spins around and suddenly I’m elsewhere…*
I’m in a sandy beach on a tropical isle.
Actually, you’re in the vi editor. Before you start typing anything, stop. Get up, stretch your legs. Odds are you’ve been at your desk for way too long anyways 🙂 Get some coffee and come back and search google what to do before you start typing.
If you are like “Say Yoho! SAY YOHO!!!*” to get out of here without making any changes, you’ll want to hit ESC and then type :q! Don’t think about it. just do it.
You can verify in the git log that the amend had no impact.
*The reason I got into computer programming was because of Scott Adam’s Pirates Adventure text-based adventure game for the TI-99 4A Home Computer. As a 6 year old, it fascinated me how a computer could understand some English commands (get safety sneakers) but not others (open refrigerator – you are in a kitchen after all!) “Say Yoho!” was the command to jump to a different part of the island. Say it too many times in a row and you’d die (I think). Never beat the game as a kid, but later in college, I downloaded an emulator and finally got past those crocodiles!