A quick reference for before your morning coffee. Beginners are strongly advised to check out some of the references below; memorizing syntax can only get you so far.
##Setting up a workstation Install Git by following these steps, then:
git config --global user.name "Your Name"
git config --global user.email youremail@domain.com
git config --global color.ui true
##Setting up a local repository
cd /path/to/repository
git init
##Using GitHub (or other remote repositories) ###Clone an existing repository
git clone <repo url> [<folder name>]
This downloads the entire repository into its own subfolder, adds the 'origin' remote which points to the clone URL, and checks out the initial branch. Subfolder name defaults to the repo name.
A remote URL points to wherever your code is stored. For the default remote, origin is often used for <name>.
###Set remote URL
git remote add <name> <address>
#Example: git remote add origin https://github.com/rileyjshaw/git-cheatsheet.git
###Update remote URL
git remote set-url <name> <NEWURL>
###Show remote repositories
git remote -v
###Remove remote
git remote rm <name>
#Basic actions ###Add file to staging area (preparing for a commit)
git add <list of files>
git add . #stage changed or new files in working tree (does not stage 'rm' actions)
git add -u #stage changed or removed files (does not stage untracked files)
git add [--all|-A] #shortcut for doing both git add . and git add -u
git add *.css #add all .css files in current directory to tracked files
git add "*.css" #add all css files in entire project to tracked files
###Remove a file from staging area
git rm <list of files> #untracks *and removes* files
git rm <list of files> -cached #untracks files but doesn't remove them. Usually want to add these to .gitignore
###Record a snapshot of the staging area via a commit
git commit -m "<commit message>"
git commit -am "<commit message>" #useful shorthand to automatically stage all tracked, modified files before the commit
Note: Commit messages should be brief, and written in the present tense.
"Add PWM block for motor 2"
###Push all unsynced commits to a remote repository
git push [-u] <remote repository name> <local branch name>
#Example: git push -u origin master
-u
sets the upstream repository to the specified remote. After this, you can run git push
, git fetch
etc without having to specify remote and branch names.
##Pulling
git pull [<remote repository name> <local branch name>]
#Example: git pull origin master
As long as you've set an upstream branch, you can type git pull
without the additional arguments.
git pull
actually does the following:
git fetch
git merge origin/master
#Use git log and you'll see why this isn't always ideal
Using git pull
can clutter your history with merges; for continuous changes where you just want your branch to reflect the upstream branch, add --rebase
to keep your logs sane.
git pull --rebase [<remote repository name> <local branch name>]
This way, merges in your history will represent points where divergent branches were intentionally merged.
##Basic references
git status #Check what has changed since the last commit
git diff #Show unstaged line differences since last commit
git diff --staged #Show staged line differences
git diff <file name> #Show changes in a specific file since last commit
git diff <commit id> <commit id> #Show changes between two commits
git log #Show repository history
git blame <file name> #Show what revision and author last modified each line of a file
#Intermediate actions ##Branching ###Create a new branch
git branch <name>
###List branches
git branch [-a]
-a
will list both local and remote branches
###Switch to a branch
git checkout <name>
#Or if you're feeling fancy...
git checkout -b <name> #Creates and checks out a branch
###Merge branches
git checkout <branch to merge to>
git merge <branch to merge from>
###Delete a branch locally
git branch -d <branch name>
###Delete a branch locally and remotely
git push origin –delete <branch name>
##Stashing
git stash #stashes uncommitted work away (modified tracked files & staged changes)
git stash pop #pops the latest stashed state and reapplys the changes
git stash list #shows list of stashes
There's more to stashing than this, but I reserve git stash
for quickly hiding the dirty parts of a branch (if I want to change branches without committing changes, for example). I prefer to treat the stash as a stack with ideal size 0 or 1.
##Tagging Tagging can be super useful if you're working on a repository with semantic versioning:
git tag [-a] <tag name> [-m <annotation>]
git push --tags
#Example: git tag -a v1.0.0 -m 'First major release build'
Adding the -a
and -m
options allows you to add annotations to the tag
#Advanced commands ##Staging and commits Warning: be very careful when amending or resetting. Generally, you should not use these commands if the commit has already been pushed.
###Unstage a staged file
Git reset HEAD <file>
git checkout -- <filename_or_dir> # "dot" '.' works for current dir
###Add staged files to the last commit
git commit --amend -m "<New commit message>"
###Undo last commit (changes into staging)
git reset --soft HEAD^
###Undo last commit and all changes
git reset --hard HEAD^
###Undo last 2 commits and all changes
git reset --hard HEAD^^
#Terminology
- master : default branch
- origin : conventional name for the default upstream repo
- HEAD : latest (or currently checked-out) commit
- More...
- From terminal:
git help <command>
(alternatively, check out the git(1) Manual Page) - Git Reference
- Everyday Git
- Pro Git
- Codeschool courses: 1, 2, and 3