a helpful primer for users sick of git's poorly-named commands
I've used Git since 2011, and this is the stuff that I've always had to Google to remember. I hope it helps you not hate Git so much.
Show me all commits on my branch, starting now and going back until the very first commit.
git log
Show me all commits on my branch, but don't waste my screen space this time.
git log --oneline
Show me commits on all branches with ASCII art to show how the branches work.
git log --graph
The holy grail: Show me all commits on all branches in a compact, colorized way.
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
Show me what's going on with the current state of my branch.
git status
I don't like reading. Give it to me in a shorter way.
git status -s
Stage files, but only some chunks of some files, and ask me which ones.
git add -p
(This launches the patch staging tool. -p
is for patch. Type ? for help.)
I don't like the interface of git add -p
. Give me a menu-driven tool for staging stuff.
git add -i
(This launches the interactive staging tool. -i
is for interactive.)
Commit all files I have staged with git add
.
git commit -m 'stuff: i did some stuff here'
Commit all files, including the ones I haven't staged. I want them all. (This still won't commit untracked files. That is probably for the best.)
git commit -am 'stuff: i did some stuff here'
(-a
: all, -m
: message)
I have to do something else with this repo quick. Save my progress without making a commit.
git stash
OK, I'm back to where I started from. Restore my progress.
git stash pop
I staged a file I don't actually want to commit. Unstage it.
git reset somefile.py
I have an unstaged file that's modified and I want to restore it to the last commit.
git checkout --
I really mucked up my repo and I just want to go back to the same state I was at before.
git reset --hard
I just ran git reset --hard
which fixed all the tracked files. Now I want to nuke all the untracked files that are still around.
git clean -df
(-d
: directories, -f
: files)
I made a commit by mistake. I want to go back to the last commit, but I don't want to lose any of the changes I made in the most recent commit.
git reset head~1
I want to delete a local branch.
git branch -d my-branch
Git is yelling at me because the branch has unmerged changes. Yes, I REALLY want to delete this local branch.
git branch -D my-branch
I want to delete a remote branch.
git push origin :my-branch
This is my first time pushing this branch to the remote.
git push -u origin head
I want to know what's changed on the remote, but I don't actually want to change any of the contents of my local branches. (This means origin/my-branch
and my-branch
on your local machine might be different.)
git fetch
I want to catch up with the remote and delete all of my local branches that have been deleted on the remote.
git fetch -p
I want to update my local branch with changes from the remote.
git pull
I want to update all my local branches with all their corresponding changes from the remote.
git pull --all
Git is yelling at me because I changed branches but my submodules didn't change too. Fix my submodules.
git submodule update
Git Flow is a great way to manage a large software repository that lots of developers are hacking on. It's a system for structuring and naming branches so that you can manage what's happening and still push releases on time.
Check out the cheatsheet for Git Flow.
SCM Breeze is a really nice set of shortcuts for bash and zsh. It's seriously a life-changer.
If you are somewhat familiar with Git and usually use the command line, install SCM Breeze.
Hub lets you do GitHub stuff from the command line. I mostly use it for creating repositories and opening pull requests.
Check out the Hub project here.
An awesome site with lots of tips on how to get yourself out of trouble in Git land.