Skip to content

Instantly share code, notes, and snippets.

@chaoyangnz
Forked from mplewis/how-do-i-into-git.md
Created January 23, 2018 23:55
Show Gist options
  • Save chaoyangnz/337edd5cda99dc544259589eb184d0a4 to your computer and use it in GitHub Desktop.
Save chaoyangnz/337edd5cda99dc544259589eb184d0a4 to your computer and use it in GitHub Desktop.

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 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

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

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.

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