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.
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 truecd /path/to/repository
git initgit 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>.
git remote add <name> <address>
#Example: git remote add origin https://github.com/rileyjshaw/git-cheatsheet.gitgit remote set-url <name> <NEWURL>git remote -vgit remote rm <name>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 filesgit 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 .gitignoregit commit -m "<commit message>"
git commit -am "<commit message>" #useful shorthand to automatically stage all tracked, modified files before the commitNote: Commit messages should be brief, and written in the present tense.
"Add PWM block for motor 2"
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.
git pull [<remote repository name> <local branch name>]
#Example: git pull origin masterAs 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 idealUsing 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.
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 filegit branch <name>git branch [-a]-a will list both local and remote branches
git checkout <name>
#Or if you're feeling fancy...
git checkout -b <name> #Creates and checks out a branchgit checkout <branch to merge to>
git merge <branch to merge from>git branch -d <branch name>git push origin –delete <branch name>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 stashesThere'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 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
Warning: be very careful when amending or resetting. Generally, you should not use these commands if the commit has already been pushed.
Git reset HEAD <file>git checkout -- <filename_or_dir> # "dot" '.' works for current dirgit commit --amend -m "<New commit message>"git reset --soft HEAD^git reset --hard HEAD^git reset --hard HEAD^^- 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