- Setup & Config
- Authentication
- Init & Clone
- Staging & Snapshots
- Moving, Removing, & Restoring
- Branches & Merging
- Inspect & Compare
- Temporary Storage
- Releases & Version Tags
- Share and Get Updates
- Rewrite History
See where Git is located:
which git
Get the version of Git:
git --version
Basic config for the git client:
git config --global user.name "Jane Doe"
git config --global user.email janedoe@example.com
git config --global core.editor "atom --wait"
git config --global color.ui auto
See existing git config:
git config --list
GitHub uses SSH keys to authenticate computers to GitHub user accounts. If you don't already have a SSH key made, create one by running:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Then upload your SSH Key to your GitHub account and test access to your account by running:
ssh -T git@github.com
If everything is properly configured you should get:
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.
Initialize Git:
git init
Clone to localhost:
git clone https://github.com/user/project.git /path/to/folder
Clone specific branch to localhost:
git clone -b branchname https://github.com/user/project.git
See the status of the working directory:
git status
Stage a file for commit:
git add [file]
Unstage a file but retain changes in the working directory:
git reset [file]
View changes:
git diff [--staged]
Commit staged changes as a new snapshot:
git commit [-m] ["Message"]
Remove files from Git:
git rm [--cached] [file]
Move or rename files:
git mv [file] [new/path/filename]
Restore file from last snapshot:
git checkout -- [file]
Restore file from specific snapshot (in current branch):
git checkout cdbfda8 -- [file]
Add a new commit that undoes a previous commit:
git revert cdbfda8
Show local branches:
git branch [-vv]
Create new branch at current commit:
git branch branchname
Switch working directory to another branch:
git checkout branchname
Merge specified branch history into current branch:
git merge branchname
Stop merge in case of conflicts:
git merge --abort
Bring a commit from another branch into current branch:
git cherry-pick [SHA]
Show commits with metadata:
git log
Show commits with metadata and patches:
git log -p
Show commits as decorated graph summary:
git log --oneline --graph --all --decorate
Show commits for a specified file:
git log --follow [file]
Compare two branches:
git log branchA..branchB
git diff branchA..branchB
git diff origin/master..master
Compare range of commits:
git diff cdbfda8..HEAD
git diff cdbfda8..537a09f
Show metadata and patch for a given commit:
git show [SHA]
Stash modified and staged changes:
git stash
Show stashed changes:
git stash list
Pop stashed commit from the top:
git stash pop
Mount stashed commit but not drop it:
git stash apply
Show all released versions:
git tag
Create a version for release:
git tag v1.0.0
Checkout a specific released version:
git checkout v1.0.0
Add remote:
git remote add [alias] [url]
Fetch changes from a remote:
git fetch [alias]
Merge fetched commits into current branch:
git merge [alias]/[branch]
Share a branch with a remote:
git push [alias] [branch]
Show remotes [with details]:
git remote [-v]
Show remote branches:
git branch -r
Add an upstream branch to current branch:
git branch --set-upstream-to=[alias]/[branch]
Fetch and merge from upstream:
git pull
Fetch, merge, and rebase from upstream:
git pull --rebase
Interactive rebasing of commits:
git rebase --interactive
Removes commits newer than specified commit and stages modified files:
git reset --soft cdbfda8
Removes commits newer than specified commit completely:
git reset --hard cdbfda8
Amend the most recent commit:
git commit --amend -m "Message"