Git Cheat Sheet

17 Jan

 

I have started making some developer cheat sheets for my own personal use using EverNote. There is so much to remember and I am often reminded that the goal is to develop good software and not to remember thousands of commands (as big and superior as doing that makes some people feel). I need cheat sheets! I am working on my own cheat-sheets for git, zsh, rvm, aws and heroku as well as some language ones.  A few folks asked me to share them so here goes starting with my git cheat sheet . Given they are primarily for myself they won’t contain all commands you may want to use so feel free to copy and modify (this is all copied from others in the first place). For instance in this git cheat sheet there is no rebasing and very little about resetting your local repository when things go horribly wrong. I am sure I will update it in due course. You can subscribe to the shared Evernote file if you are an EverNote user here. I will try and keep this page updated but that EverNote will be my source of truth!
If you do find mistakes, have smarter ways of doing things or can’t figure out why something is missing do let me know. I would love to make it better for me and anyone who is using it.
Useful Resources
(see shell customization cheat sheet for adding a good git prompt in the shell)


Global Settings

git config [–global]

User Details
user.name $name i.e git config –global user.name Mark Curphey
user.email $email i.e git config –global user.email mark@curphey.com
Github
github.user $user
github.token $token
or just edit the ~/.gitconfig file !

 

Creating Repositories

Create Local Repository from an Existing Local Project

cd ~/project_dir
git init
git add .

Clone Remote Repository
git clone git://github.com/user/repo.git

Clone a Local Repository
git clone ~/existing/repo ~/new/repo
git clone you@host.org:dir/project.git

Local Repositories
List Changes in Working Directory
git status

Add Files to Repository
git add [filename1] [filename2]
git add .

Delete Files in Repository
git rm [filename1] [filename2]

List Changes to Tracked Files
git diff

Commit Changes
git commit -am “commit message”
(-a is all files that are tracked, NOT all files, so you still need to add filename or add .)
(-m is with a commit message)
Return to Last Committed State
git reset –hard HEAD


Remote Repositories (Github)


List Remote Repositories Aliased
git remote

Add Remote Repository
git remote add [alias] [location] i.e. git remote add origin git://github.com/curphey/repo.git

Remove Remote Repository
git remote rm [alias] i.e. git remote rm origin

Pull from Remote Repository and Merge into Current Branch
git pull [alias] [location] i.e. git pull origin master
(once you have pulled once the alias and remote branch are no longer needed)

git fetch from Remote Repository is same as pull but without auto-merging

Push Local Changes to Remote
git push [alias] [branch]

If the server rejects your push, always try a git pull and then retry as 99 times out of 100 you didn’t have the latest remote!


Branching and Merging

List Available Branches
git branch

Create a Branch
git branch [branch name] i.e git branch [experimental]

Switch to Work in a Branch
git checkout [branch name] i.e git checkout experimental
Create and Immediately Switch to New Branch (i.e both of last two steps)
git checkout -b [branch name]

Merge Branch
git merge [branch to merge] i.e. git merge experimental will merge experimental back into working branch

Track Original Repository of an Open Source Project on Github
Fork repository, create an upstream remote, fetch and merge (or pull) changes into your fork.

git remote add upstream https://github.com/rails/rails.git
git fetch upstream
git merge upstream/master

Show Log of Activity
git log

Tag a Commit i.e. v.0_beta1
git tag [note]

3 Responses to “Git Cheat Sheet”

  1. Anonymous January 24, 2012 at 10:02 am #

    2 more great Git resources
    http://think-like-a-git.net/epic.html 
    http://gitready.com/ 

    • Mark Curphey January 25, 2012 at 12:58 pm #

      Awesome!

    • Anonymous February 2, 2012 at 8:47 am #

      Thanks Howard. The video was superb!

Leave a comment