Skip to content

Instantly share code, notes, and snippets.

@macton macton/git_cheatsheet.txt
Last active Dec 7, 2016

Embed
What would you like to do?
Git cheatsheets
---------------
http://www.cheat-sheets.org/saved-copy/git-cheat-sheet.pdf
http://alvinalexander.com/git/git-cheat-sheet-git-reference-commands
Git concepts
------------
Repository - Collection of commit history of files being managed (not directories)
Working directory - Files in local file system (may or may not be tracked)
.git folder - Actual repository (full history)
Commit - Snapshot of change(s)
Branch - Timeline of commits
Staging Area - Pending commits
States - Working directory -> Staging Area -> Repository (-> Remote)
Remote - Conceptually additional State where commits are made to remote repository (Having its own three stages)
Local repositories
------------------
Initialize name
git config --global user.name "Your Name"
git config --global user.email "your.email@your-place.com"
Set editor
git config core.editor "notepad++ -multiInst -nosession"
Test editor
git config --global -e
Configure diff tool (p4merge)
git config --global diff.tool p4merge
git config --global difftool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
git config --global difftool.prompt false
Configure merge tool (p4merge)
git config --global merge.tool p4merge
git config --global mergetool.p4merge.path "C:/Program Files/Perforce/p4merge.exe"
git config --global mergetool.prompt false
Create project
git init project_name
Status
git status
Add file(s) to staging area
git add <files>
Commit files to repo
git commit -m "commit message"
Show list of commits
git log
Show list of commits, one line each + asterix graph of branching hierarchy + which commits part of which branches + history of all branches
git log --oneline --graph --decorate --all
Show list of commits and diffs
git show
List tracked files
git ls-files
Add modified files and commit
git commit -am "message"
Unstage file
git reset HEAD <file>...
Discard changes in working director
git checkout -- <file>...
Add git command alias globally
git config --global alias.hist "log --oneline --graph --decorate --all"
List global values
git config --global --list
Git log (based on alias above)
git hist
Git log of single file
git hist -- README.md
Rename file
git mv example.txt demo.txt
Remove file
git rm demo.txt
Stage changed files
git add -u
Stage changed files (all types of changes)
git add -A
Exclude files from repository
edit .gitignore
one file expression per line to ignore
List branches
git branch
List branch (both remote and local branches)
git branch -a
Create and switch to new branch (stage open changes in new branch)
git checkout -b updates
Compare changes between branches
git diff updates master
Switch branch
git checkout master
Merge changes from another branch
git merge updates
Delete branch
git branch -d updates
Run mergetool
git mergetool
// For vimdiff see: https://gist.github.com/mattratleph/4026987
Tag (lightweight) current commit
git tag mytag
List all tags
git tag --list
Delete tag
git tag -d mytag
Tag (annotated) tag
git tag -a v1.0 -m "Release 1.0"
Show list of commits and diffs (for given annotated tag)
git show v1.0
Stash current tracked changes
git stash
List currently stashed changes
git stash list
Reapply last stashed changes and remove stash
git stash pop
Reapply previous commit (change where HEAD is)
git reset dadad3a --soft
Git resets are --soft, --mixed, --hard (--mixed is default)
What are the differences? See: http://stackoverflow.com/a/3528483
--soft Moves changes to staging
--mixed Moves changes to working directory
--hard Removes changes destructively
Show list of commits, one line each (up to HEAD)
git log --oneline
Show list of all actions taken in repo
git reflog
Remote repositories
-------------------
BitBucket
http://bitbucket.org
Show all connected remote repositories associated with current repository
git remote -v
Connect repository to remote repository with name "origin"
git remote add origin ssh://git@bitbucket.org/macton/demo.git
Push all commits from local branch "master" to remote repository "origin" with tracking relationship -u (create equivalent "master" branch on remote), include tags --tags
git push -u origin master --tags
Remove connection with remote repository named "origin"
git remote rm origin
GitHub
http://github.org
BitBucket vs GitHub
| BitBucket | GitHub |
| ------------------------ | ------------------------ |
| Unlimited public repos | Unlimited public repos |
| Unlimited private repos | Charge by private repos |
| Charge by team size | Unlimited team size |
| Git repos | Git repos |
| Pull requests | Pull requests |
| Snippets | Gist |
| Teams | Organizations |
| Internal | Issues |
| JIRA integration | Third party |
| Atlassian Integration | Repo initialization |
| Mercurial | Social features |
Clone remote repository
git clone git@github.com:macton/start-remote.git
Clone remote repository into specified folder "demo-github"
git clone git@github.com:macton/start-remote.git demo-github
Sync references between local ("master") and remote repository ("origin") without merging commits
git fetch origin master
git fetch vs git pull
fetch adds remote history to repository, but does not merge
pull does fetch then merge
See: http://stackoverflow.com/a/292359
Fetch and merge changes from remote repo "origin" into local branch "master"
git pull origin master
Synchronize and update remote repo "origin" from local branch "master"
git push origin master
Other tips
----------
Is it possible to exclude specific files when doing a git merge?
[0] http://stackoverflow.com/questions/332528/is-it-possible-to-exclude-specific-commits-when-doing-a-git-merge/3970442#3970442
[1] http://stackoverflow.com/questions/14093540/tell-git-to-use-ours-merge-strategy-on-specific-files/14099431#14099431
$ git config merge.ours.driver true
e.g Create a file named .gitattributes in the same dir, with this line: config.php merge=ours
Git brancing and merging
[0] http://git-scm.com/book/en/Git-Branching-Basic-Branching-and-Merging
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.