In my last post, we took a look at the data model underpinning Git. Understanding that all of Git’s many commands (and there are many) are just an interface for manipulating that data model can help us to understand why Git works the way it does.
Remember, an ugly interface has to be memorized, but a beautiful design can be understood.
Git Incantations
Learning a handful of commands to use like magic incantations is a short-term solution at best, and a dangerous habit at worst.
But maybe you just need a quick solution, or maybe you like to live dangerously. Who am I to judge?
I’m still learning Git, myself. So if you’ve come here in search of the most esoteric, most arcane, most impressive Git one-liners, you may be disappointed with my current spellbook.
Basics
These are the cantrips, the small magics that all Git acolytes will learn.
git help <command>
: get help for a git commandgit init
: creates a new git repo, with data stored in the.git
directorygit status
: tells you what’s going ongit add <filename>
: adds files to staging area-
git commit
: creates a new commitPublic Service Announcement:
Write good commit messages!
Seriously, write good commit messages! git log
: shows a flattened log of historygit log --all --decorate --oneline --graph
: (a.k.a. log-a-dog) visualizes history as a DAGgit diff <filename>
: show changes you made relative to the staging areagit diff <revision> <filename>
: shows differences in a file between snapshotsgit checkout <revision>
: updates HEAD and current branch
Remotes
Managing your local file history is great, but the ability to distribute your version control remotely imparts extra power.
git remote
: list remotesgit remote add <name> <url>
: add a remotegit push <remote> <local branch>:<remote branch>
: send objects to remote, and update remote referencegit branch --set-upstream-to=<remote>/<remote branch>
: set up correspondence between local and remote branchgit fetch
: retrieve objects/references from a remotegit pull
: effectively:git fetch; git merge
git clone
: download repository from remote
Undo
Git documentation has this chicken and egg problem where you can’t search for how to get yourself out of a mess, unless you already know the name of the thing you need to know about in order to fix your problem.
– Katie Sylor-Miller
These are for simple fixes. If you’ve messed up harder, you’re in good company. Check out Oh Shit, Git!?! for some commands to recover from common Git mistakes.
git commit --amend
: edit a commit’s contents/messagegit reset HEAD <file>
: unstage a filegit checkout -- <file>
: discard changes
More Advanced Git
git config
: Git is highly customizablegit clone --depth=1
: shallow clone, without entire version historygit add -p
: interactive staginggit rebase -i
: interactive rebasinggit blame
: show who last edited which linegit stash
: temporarily remove modifications to working directorygit bisect
: binary search history (e.g. for regressions).gitignore
: specify files to ignore intentionally
Resources
Here’s my collection of extra reading and tangentially-related topic links.
Augmentations
GUIs: try one of the GUI Clients recommended by the Git community.
Integration: bring your Git into your editor with extensions! The VSCode Marketplace has many to choose from. Tim Pope’s fugitive.vim is the premier Git plugin for Vim.
Workflows: There are many different ways to use Git for your project. The general rule is to follow the flow of your team or project. If you’re trying to decide which model works best for your team, consider these options:
Links
- Pro Git - A Creative Commons licensed book by Scott Chacon and Ben Straub.
- Learn Git Branching - An interactive game to help learn git commands!