Skip to content

Instantly share code, notes, and snippets.

@ertborTek
Last active October 13, 2015 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ertborTek/4146089 to your computer and use it in GitHub Desktop.
Save ertborTek/4146089 to your computer and use it in GitHub Desktop.
Git Reference
Git Reference (from Code School's _Git Real_)
# Git Reference (from Code School's _Git Real_)
## Setup
Set up your identity for commits.
$ git config --global user.name "Name"
$ git config --global user.email "mail@example.com"
Colorize terminal output.
$ git config --global color.ui true
Start a local repository (from within the project's path).
$ git init
## History and Status
Get history
$ git log
See what's changed since the last commit.
$ git status
## Basics
Add a file to the staging area:
$ git add file
Add multiple files to the staging area.
$ git add <file> <file> ...
Add all new and modified files to the staging area.
$ git add --all
Add all files in a directory.
$ git add path/
Add all .extension files within the project.
$ git add "*.extension"
Add all .extension files within current directory:
$ git add *.extension
Commit staged files.
$ git commit -m "Message"
## Staging & Remotes
Show unstaged differences since the last commit.
$ git diff
View staged differences.
$ git diff --staged
Unstage a file:
$ git reset HEAD <file>
Revert to last commit:
$ git checkout -- <file>
Add tracked files to stage and then commit them.
$ git commit -a -m "Message"
Undo a commit (puts changes into staging):
$ git reset --soft HEAD^ # HEAD^ = before HEAD
Amend commit:
$ git commit --amend -m "Revise message if necessary."
Undo a commit (discarding last commit and all changes).
$ git reset --hard HEAD^
Undo last two commits.
$ git reset --hard HEAD^^
Add a remote repo:
$ git remote add <name of remote repository> <address>
$ git remote add origin https://github.com/Account/project.git
Show remote repositories:
$ git remote -v
Push repository:
$ git push -u <name of remote repository> <local branch to push>
$ git push -u origin master
Pull changes from remote repository:
$ git pull
Remove remote repository:
git remote rm <name of remote repository>
Push to remote repositories (-u tells git to remember the name and branch):
$ git push -u <name> <branch>
## LEVEL 3 - Cloning and Branching
Clone a repository
$ git clone <repository address> <local directory name>
Cloning does the following:
1. downloads repository
2. adds the 'origin' remote, pointing it to the clone URL
3. checks out initial branch, e.g., master, and sets the HEAD
Create a branch:
$ git branch <branch name>
Reveal current branch:
$ git branch
Switch branch (HEAD moves to active branch):
$ git checkout <branch name>
Merge branches:
$ git merge <branch to merge into current branch>
Delete a branch:
$ git branch -d <branch name>
Create and switch to a branch:
$ git checkout -b <branch name>
## LEVEL 4 - Collaboration Basics
The `git pull` command:
* fetches (syncs) local repository with remote repository but pulls it into the origin/master branch. The `git fetch` command does the same.
* merges origin/master with master, same as running `git merge origin/master`
* does a merge commit, prompting for a message
After the merge commit, run `git push` to bring the remotes origin/master into sync with the local one.
pul
Mark a previously conflicted file as fixed:
$ git add <file>
## LEVEL 5 - Remote Branches & Tags
Create a remote branch:
$ git checkout -b <branch>
$ git push origin <branch>
List remote branches:
$ git branch -r
Work on a remote branch:
$ git checkout <branch>
Show remote and local branches and their status:
$ git remote show <repository name>
Delete a remote branch:
git push <repository name> :<branch>
Delete a local branch:
git branch -d <branch>
Force delete a local branch:
git branch -D <branch>
Clean up stale branches, i.e., branches that may have been deleted to which there are outstanding tracking local branches:
git remote prune <repository name>
Link a local non-master branch to a remote master branch:
git push <repository name> <branch>:master
List last tag:
git tag
Revert to tag:
git checkout <tag name>
Add a tag:
git tag -a <tag name> -m "Tag Description"
Push tags:
git push --tags
## LEVEL 6 - Rebase Belong to Us
Rebase (`git rebase`) does the following:
* moves all changes, which differ from what's in origin/master, to a temporary area
* runs all origin/master commits, one at a time, on the master branch
* runs all commits, one at a time, in the temporary area on master
Rebase a local branch:
$ git checkout <branch>
$ git rebase master
$ git checkout master
$ git merge <branch>
Rebase:
$ git fetch
$ git rebase
## LEVEL 7 - History & Configuration
Colorize:
$ git config --global color.ui true
Display commit on a single line:
$ git log --pretty=oneline
Customize format:
$git log --pretty=format:"%h %ad- %s [%an]"
Format options
| Placeholder | Replacement |
|-------------|-------------|
| `%ad` | author date |
| `%an` | author name |
| `%h` | SHA hash |
| `%s` | subject |
| `%d` | ref names |
See what each commit changed (patch):
$ git log --oneline -p
See how many insertion/deletions for each file in each commit
$ git log --oneline stat
$ git log --oneline graph
Specify ranges:
$ git log --until=1.minute.ago
$ git log --since=1.day.ago
$ git log --since=1.hour.ago
$ git log --since=1.month.ago --until=2.weeks.ago
$ git log --since-2000-01-01 --until=2012-12-21
What has changed since last commit:
$ git diff
$ git diff HEAD
What has changed since _n_ commits ago:
$ git diff HEAD^
$ git diff HEAD^^
$ git diff HEAD~3
$ git diff HEAD^..HEAD
Diff by SHA:
$ git diff <SHA>..<SHA>
Diff by branches:
$ gif diff <branch> <branch>
Diff by time:
$ git diff --since=1.week.ago --until=1.minute.ago
See all changes and who made them:
$ git blame <file> --date short
Exclude files in `.git/info/exclude`, such as system by-products generated by local applications.
Exclude files from all copies using `.gitignore`.
Remove a file:
$ git rm <file>
$ git commit -m "Message"
Stop tracking a file:
$ git rm --cached <file>
Configure:
$ git config --global user.name "First Last"
$ git config --global user.email "name@example.com"
$ git config --global core.editor <editor>
$ git config --global merge.tool <diff tool>
$ git config --list
$ git config user.email
Aliases:
git config --global alias.mylog "log --pretty=format: '%h %s [%an]' --graph"
git config --global alias.lol "log --graph --decorate --pretty=oneline --abbrev-commit --all"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment