Instantly share code, notes, and snippets.

Embed
What would you like to do?

How Do I Into Git?

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.

Learning About the Repo

Learning About History

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

Learning About Commits

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

Making Commits

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)

Saving Your Work

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

Undoing Stuff

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 made a bunch of changes that are all tangled up in each other. I want to keep some and get rid of the rest. They're so tangled that I want to just un-commit everything back to a certain point, but keep the changes so I can try and fix them.

git reset
git reset --soft

(--soft stages the stuff that was uncommitted; omitting it keeps the changes unstaged)

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 did something bad, and I lost work by overwriting a good commit. Help me find the work I lost!

git reflog

Deleting Stuff

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

Changing Stuff (beware!)

I want to rebase my branch onto another branch, but I also want to clean up, reorder, and combine some of my commits.

git rebase -i new-base

Pushing Stuff To Others

This is my first time pushing this branch to the remote.

git push -u origin head

Pulling Stuff From Others

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

Submodules

Git is yelling at me because I changed branches but my submodules didn't change too. Fix my submodules.

git submodule update

Helpful Stuff

Git Flow

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

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

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.

Oh, shit, git!

An awesome site with lots of tips on how to get yourself out of trouble in Git land.

@mauvm

This comment has been minimized.

mauvm commented Dec 17, 2015

What about Hub? You can manage GitHub repositories with it (git create repo-name).

Have you tried git status -sb?

Aliasing is another useful feature (git config --global alias.s "status -sb") . Also I've mapped git to g. Which allows me to run g s. Saves you a lot of typing.

Hope it helps.

@arrowrowe

This comment has been minimized.

arrowrowe commented Dec 17, 2015

OhMyZsh offers lots of great Git aliases. Try alias | grep git.

@ryanneufeld

This comment has been minimized.

ryanneufeld commented Dec 17, 2015

Don't forget about git add -p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment