Git Pet Tricks

Git Pet Tricks

Inspired by Kendra Little’s recent post My Git CLI Cheat Sheet, I decided to do a little write-up about how I use git on my computers.

The Shell

First off, I don’t use git without some serious shell customizations. I like to be able to the local git status just by pressing enter in the shell when I’m being super lazy.

On macOS and Linux I use the ZSH shell with the powerlevel10k theme. It’s both customizable and fast.

On Windows things are still quite simple. I use posh-git to add helpful git tools to PowerShell. In addition, I use oh-my-posh for some theming excitement and then I add on the paradox theme to give myself a delightful prompt.

Here’s the PowerShell configuration:

Import-Module posh-git
Import-Module oh-my-posh
Set-Theme Paradox

The shell in action

But you’re not here for a pretty shell, you’re here for helpful git ideas!

The gitconfig file

Everything in git can be controlled from a central .gitconfig file. On my
Windows computer, my .gitconfig looks like this:

[credential]
    helper = wincred
[include]
    path = c:\\Users\\jeremiah\\src\\peschkaj\\dotfiles\\git\\gitconfig
[include]
    path = c:\\Users\\jeremiah\\src\\peschkaj\\dotfiles\\git\\gitconfig-windows

This sets up the Windows credential helper for my credentials and then instructs git to find the rest of my configuration in one of two files. The important one is the gitconfig file, this is where I’ve set up my aliases. You see, git helpfully lets you set up aliases for long and annoying commands that you just never want to type again. Git’s aliases open up a lot of power, even if you don’t want to create any yourself. One upside of using a centralized gitconfig file is you’re able to keep your configuration under version control and just tell git where that configuration is located.

I won’t go through all of the changes line by line, but I’ll cover my favorites.

Checking Out and Commiting Code

I’m incredibly laz. git checkout is too much typing, so I’ve aliased it to git co. As a helpful mnemonic, I’ve aliased git commit to git ci (ci means check-in, har har).

History

git hist shows us a lovely graph log of changes in a repository over time. You
can see how branches were merged and how work progressed on each branch. This is
much better than the uglier view you’d get from git log.

Git history on a multi-user project

Standup

git standup lets you know what changed since the start of the last working day. I haven’t been working on any source code in the last day, so I made a silly commit just to demonstrate how this works:

I stood up and did something stupid!

Undo

git undo: it undoes your most recent commit! Here you can see that I’ve undone the commit that I used to demonstrate git standup, the changes are now shown as unstaged:

Ctrl-Z!

Sync

git sync will sync changes from the remote repository to your local master. Make sure you’ve staged or stashed your local commits before doing this! I didn’t take a screenshot because it’s not really exciting. Well, you might think it is, but it’s just stuff changing.

Want to Learn More?

Pretty Powershell: Scott Hanselman has an excellent blog post How to make a pretty prompt in Windows Terminal with Powerline, Nerd Fonts, Cascadia Code, WSL, and oh-my-posh.
gitconfig: The gitconfig documentation is the best place I’ve found to learn. Or you could search github for files named gitconfig and see what other people are doing! That’s how I’ve built most of my gitconfig over the years.
Steal my configuration: You can just use my configuration as it exists today. Head over to my dotfiles repository and take a look.