A collection of cool hidden and not so hidden features of Git and GitHub. This cheat sheet was inspired by Zach Holman's Git and GitHub Secrets talk at Aloha Ruby Conference 2012 (slides) and his More Git and GitHub Secrets talk at WDCNZ 2013 (slides).
- Ignore Whitespace
- Cloning a Repo
- Hub - Git Wrapper
- Decreasing Contributor Friction
- Previous Branch
- Git.io
- Gists
- Keyboard Shortcuts
- Closing Issues with Commits
- Checking out Pull Requests
- Cross-Link Issues
- Contributing Guidelines
- Syntax Highlighting in Markdown Files
- Commit History by Author
- Empty Commits
- Comparing Branches
- Line Highlighting in Repos
- Metadata and Plugin Support for GitHub Pages
- Diffs
- Emojis
- Images/GIFs
- Quick Quoting
- Styled Git Status
- Styled Git Log
- Git Query
- Merged Branches
- Quick Licensing
- Task Lists
- Relative Links
.gitconfig
Recommendations
Adding ?w=1
to any diff URL will remove any changes only in whitespace, enabling you to see only that code that has changed.
Read more about GitHub secrets.
When cloning a repo the .git
can be left off the end.
$ git clone https://github.com/tiimgreen/github-cheat-sheet
Read more about the Git clone
command.
Hub is a command line git wrapper that gives extra features and commands that make working with GitHub easier.
This allows you to do things like:
$ hub clone tiimgreen/toc
instead of:
$ git clone https://github.com/tiimgreen/toc.git
Check out some more cool commands Hub has to offer.
If you want people to use and contribute to your project, you need to start by answering their most basic questions. What does the project do? How do I use it? How am I allowed to use it? How do I contribute? How do I get up and running in development? How do I make sure my new features didn't break old functionality?
Friction is a command line script that will check your project for common answers to these questions. This is some example output:
To move to the previous directory in the command line:
$ cd -
Similarly, to move to the last branch in git:
$ git checkout -
# Switched to branch 'master'
$ git checkout -
# Switched to branch 'next'
$ git checkout -
# Switched to branch 'master'
Read more about Git branching.
Git.io is a simple URL shortener for GitHub. You can also use it via pure HTTP using Curl:
$ curl -i http://git.io -F "url=https://github.com/..."
HTTP/1.1 201 Created
Location: http://git.io/abc123
$ curl -i http://git.io/abc123
HTTP/1.1 302 Found
Location: https://github.com/...
Gists are an easy way to work with small bits of code without creating a fully fledged repo. Add .pibb
to the end of any Gist URL (like this) in order to get the HTML only version suitable for embedding in any other site.
Gists can be treated as a full repository so they can be cloned like any other:
$ git clone https://gist.github.com/tiimgreen/10545817
Read more about creating gists.
When on a repo page keyboard shortcuts allow you to navigate easily.
Pressing t
will bring up a file explorer.
Pressing w
will bring up the branch selector.
Pressing s
will select the Command Bar.
Pressing l
will edit labels on existing issues.
Pressing y
when looking at a file (e.g. https://github.com/tiimgreen/github-cheat-sheet/blob/master/README.md
) will change your URL to one which, in effect, freezes the page you are looking at. If this code changes, you will still be able to see what you saw at that current time.
To see all of the shortcuts for the current page press ?
.
Read more about using the Command Bar.
If a particular commit fixes an issue, any of the keywords fix/fixes/fixed
or close/closes/closed
, followed by the issue number, will close the issue once it is committed to the master branch.
$ git commit -m "Fix cock up, fixes #12"
This closes the issue and references the closing commit.
Read more about closing issues via commit messages.
If you want to check out pull request locally, you can fetch it using that command:
$ git fetch origin '+refs/pull/*/head:refs/pull/*'
then, checkout Pull Request (i.e. 42) using
$ git checkout refs/pull/42
Alternatively, you can fetch them as remote branches:
$ git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'
and checkout as:
$ git checkout origin/pr/42
and even fetch them automatically, if you add corresponding lines in your .git/config:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@github.com:tiimgreen/github-cheat-sheet.git
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git@github.com:tiimgreen/github-cheat-sheet.git
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
Read more about checking out pull requests locally.
If you want to link to another issue in the same repo, simple type hash #
then the issue number, it will be auto-linked.
To link to an issue in another repo, user_name/repo_name#ISSUE_NUMBER
e.g. tiimgreen/toc#12
.
Adding a CONTRIBUTING
file to the root of your repository will add a link to your file when a contributor creates an Issue or opens a Pull Request.
Read more about contributing guidelines.
For example, to syntax highlight Ruby code in your Markdown files write:
```ruby
require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', 'tiimgreen@gmail.com')
puts table.to_s
```
This will produce:
require 'tabbit'
table = Tabbit.new('Name', 'Email')
table.add_row('Tim Green', 'tiimgreen@gmail.com')
puts table.to_s
GitHub uses Linguist to perform language detection and syntax highlighting. You can find out which keywords are valid by perusing the languages YAML file.
Read more about GitHub Flavored Markdown.
To view all commits on a repo by author add ?author=username
to the URL.
https://github.com/rails/rails/commits/master?author=dhh
Read more about the differences between commits views.
Commits can be pushed with no code changes by adding --allow-empty
:
$ git commit -m "Big-ass commit" --allow-empty
To use GitHub to compare branches, change the URL to look like this:
https://github.com/user/repo/compare/{range}
Where {range} = master...4-1-stable
e.g.:
https://github.com/rails/rails/compare/master...4-1-stable
{range}
can be changed to things like:
https://github.com/rails/rails/compare/master@{1.day.ago}...master
https://github.com/rails/rails/compare/master@{2014-10-04}...master
which allows you to see the difference on the master branch up a set time ago or a specified date.
Read more about comparing commits across time.
To use GitHub to compare branches across forked repositories, change the URL to look like this:
https://github.com/user/repo/compare/{foreign-user}:{branch}...{own-branch}
eg.:
https://github.com/rails/rails/compare/byroot:idempotent-counter-caches...master
Either adding #L52
to the end of a code file URL or simply clicking the line number will highlight that line number.
It also works with ranges, e.g. #L53-L60
, to select ranges, hold shift
and click two lines:
https://github.com/rails/rails/blob/master/activemodel/lib/active_model.rb#L53-L60
Within Jekyll pages and posts, repository information is available within the site.github
namespace, and can be displayed, for example, using {{ site.github.project_title }}
.
The Jemoji and jekyll-mentions plugins enable emoji and @mentions in your Jekyll posts and pages to work just like you'd expect when interacting with a repository on GitHub.com.
[Read more about repository metadata and plugin support for GitHub Pages.](Repository metadata and plugin support for GitHub Pages)
Commits and pull requests including rendered documents supported by GitHub (e.g. Markdown) feature source and rendered views.
Click the "rendered" button to see the changes as they'll appear in the rendered document. Rendered prose view is handy when you're adding, removing, and editing text:
Read more about rendered prose diffs.
Any time you view a commit or pull request on GitHub that includes geodata, GitHub will render a visual representation of what was changed.
Read more about diffable maps.
Using the unfold button in the gutter of a diff, you can reveal additional lines of context with a click. You can keep clicking unfold until you've revealed the whole file, and the feature is available anywhere GitHub renders diffs.
Read more about expanding context in diffs.
Emojis can be generated on Pull Requests, Issues, Commit Messages, READMEs, etc. using :name_of_emoji:
:smile:
:poop:
:shipit:
:+1:
π π© π
The full list of supported Emojis on GitHub can be found here or here.
The top 5 used Ejmojis on GitHub are:
- -
:shipit:
- β¨ -
:sparkles:
- π -
:-1:
- π -
:+1:
- π -
:clap:
Images and GIFs can be added to comments, READMEs etc.:
![Alt Text](http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif)
All images are cached on GitHub, so if your host goes down, the image will remain available.
There are multiple ways of embedding images in Wiki pages. There's the standard Markdown syntax (shown above). But there's also a syntax that allows things like specifying the height or width of the image:
[[ http://www.sheawong.com/wp-content/uploads/2013/08/keephatin.gif | height = 100px ]]
Which produces:
When on a comment thread and you want to quote something someone previously said, highlight the text and press r
, this will copy it into your text box in the block-quote format.
Read more about quick quoting.
$ git status
Can be changed to:
$ git status -sb
Read more about the Git status
command.
$ git log --all --graph --decorate --oneline --abbrev-commit
NOTE: This can be added into an Alias (shorter command) using the instructions here
Read more about the Git log
command.
A git query allows you to search all your previous commit messages and finds the most recent one matching the query.
$ git show :/query
Where query
is the term you want to search, this then finds the last one and gives details on the lines that were changed.
$ git show :/typo
NOTE: Press q
to quit.
$ git branch --merged
Will give you a list of all branches that have been merged into your current branch.
Conversely:
$ git branch --no-merged
Will give you a list of branches that have not been merged into your current branch.
Read more about the Git branch
command.
When creating a repo GitHub gives you the options of adding in a pre-made license:
You can also add them to existing repos by creating a new file through the web interface. When the name LICENSE
is typed in you will get an option to use a template:
Also works for .gitignore
.
Read more about open source licensing.
In Issues and Pull requests check boxes can be added with the following syntax (notice the space):
- [ ] Be awesome
- [ ] Do stuff
- [ ] Sleep
When they are clicked, they will be updated in the pure Markdown:
- [x] Be awesome
- [x] Do stuff
- [ ] Sleep
Relative links are recommended in your Markdown files when linking to internal content.
[Link to a header](#awesome-section)
[Link to a file](docs/readme)
Absolute links have to be updated whenever the URL changes (e.g. repo renamed, username changed, project forked).
Using relative links makes your documentation easily stand on its own.
Read more about relative links.
Your .gitconfig
is the file that contains all your preferences.
Aliases are helpers that let you define your own git calls. For example you could set git a
to run git add --all
.
To add an alias, either navigate to ~/.gitconfig
and fill it out in the following format:
[alias]
co = checkout
cm = commit
p = push
# Show verbose output about tags, branches or remotes
tags = tag -l
branches = branch -a
remotes = remote -v
or type in the command line:
$ git config alias.new_alias git_function
e.g.
$ git config alias.cm commit
NOTE: for an alias with multiple functions use quotes:
$ git config alias.ac 'add -A . && commit'
Some recommendations include:
Alias | Current Command | What to Type |
---|---|---|
git cm |
git commit |
git config --global alias.cm commit |
git co |
git checkout |
git config --global alias.co checkout |
git ac |
git add . -A git commit |
git config --global alias.ac '!git add -A && git commit' |
git st |
git status -sb |
git config --global alias.st 'status -sb' |
git tags |
git tag -l |
git config --global alias.tags 'tag -l' |
git branches |
git branch -a |
git config --global alias.branches 'branch -a' |
git remotes |
git remote -v |
git config --global alias.remotes 'remote -v' |
Currently if you type git comit
you will get this result:
$ git comit -m "Message"
# git: 'comit' is not a git command. See 'git --help'.
# Did you mean this?
# commit
To call commit
when comit
is typed, just enable autocorrect:
$ git config --global help.autocorrect 1
So now you will get this result:
$ git comit -m "Message"
# WARNING: You called a Git command named 'comit', which does not exist.
# Continuing under the assumption that you meant 'commit'
# in 0.1 seconds automatically...
To add more colour to your git command line:
$ git config --global color.ui 1
Read more about the Git config
command.
Share on Twitter