Skip to content

Instantly share code, notes, and snippets.

@himanshuy
Forked from mplewis/how-do-i-into-git.md
Last active December 17, 2015 02:10
Show Gist options
  • Save himanshuy/c36fd07f19c0cb879d52 to your computer and use it in GitHub Desktop.
Save himanshuy/c36fd07f19c0cb879d52 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

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

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