GitHub Tips and Tricks
Published on by Paul Redmond
I’ve compiled a list of my favorite GitHub tips and tricks that I use in my workflow daily. You might be familiar with some or even all of them, but I find that developers new to GitHub and veterans alike might pick up a new tip or two by sharing!
I’d love to hear from you about your favorite GitHub tips that save you time and improve your code workflow. Here’s my short list of GitHub tips and tricks I’ve loved lately:
Ignore Whitespace in Diffs with the ?w=1 Trick
You can remove whitespace from diffs with the ?w=1
query param. The w=1
query param removes whitespace-only diffs and only shows you lines with code changes. When looking at a large diff with lots of code cleanup and CS fixes this is very helpful to get at the heart of any code changes going along with the whitespace fixes.
I would suggest making separate pull requests for mass whitespace fixes, but sometimes you need to do some cleanup along the way, and removing whitespace diffs is an excellent way to get the point of the pull request quickly.
Commits by Author
You can find the contributions made by an author with the ?author=<github_username>
query string param. I find this handy when I want to see my past contributions on a large project or single out a specific developer’s commits on a project. Here are my humble contributions to the Laravel framework, for example.
Linking to Files
This one comes from Derrick Reimer on Twitter. Press ‘y’ to transform the URL into a SHA-based reference:
???? When linking to a file on @github, press 'y' to transform the URL to a SHA-based reference. That way, the link will remain stable even if the underlying file changes in future commits. pic.twitter.com/cAsupEk7tV
— Derrick Reimer (@derrickreimer) May 30, 2018
Sharing the SHA-based reference is evergreen, meaning that the code won’t change in the same way compared to linking to a branch-based file URL like:
https://github.com/laravel/framework/blob/5.6/src/Illuminate/Http/Request.php # VS a hash-based URL https://github.com/laravel/framework/blob/adec8f3b55a7d03305fb8a36b6865206f9a81a8b/src/Illuminate/Http/Request.php
Line Highlighting
Line highlighting is evident from the UI when you click a line number, but you can also link to a range of lines too. For example, the hash #L23-L30
would select the line range of lines 23-30. In tandem with the ‘y’ trick, the SHA-based URL will ensure your line number hash is accurate in the future.
You can also select a range of lines from a file by first clicking a line number from the GitHub UI, holding down “shift” and clicking the ending line:
Cross-Linking Issues
You can cross-link issues and pull requests with the following format in comments, pull request descriptions, etc.:
<organization>/<repo>#<issue_number>
Based on that pattern, here’s an example:
Depends on laravelnews/my-package#123
I love using this tip between my private GitHub repositories and any internal libraries or packages that a pull request depends upon to merge. The other benefit is that GitHub automatically creates a reference in the issue so others can see the relationship.
Organizing Contribution Guidelines
Did you know that you can organize documents like issue templates and contribution guidelines in a docs/
folder or a .github
folder? I recently learned about this trick, which is a neat way to organize the files around creating new issues and sharing contribution guidelines.
For example, you can organize the contributing document in the following locations:
contributing.mddocs/contributing.md.github/contributing.md
Issue templates, pull request templates, and various other project resources work the same way:
.github/ISSUE_TEMPLATE.md.github/PULL_REQUEST_TEMPLATE.md.github/SUPPORT.md.github/CODE_OF_CONDUCT.md
Keyboard Shortcuts
If you’re getting into open-source and using GitHub in general, you can learn about all the keyboard shortcuts available on a given page by hitting “Shift + ?”
Make sure you click “show all” for additional shortcuts, including my favorite shortcut: the ‘t’ key:
Task Lists
You can create task lists in pull requests with markdown like the following:
- [ ] Tests for validation- [x] Tests for unauthorized access- [ ] Add indexes to migrations
Which will render as the following in your pull request:
You can check the boxes too once you save the description of a pull request, and you can edit the description to add more todos and manage complete vs. incomplete.
As a bonus, they show up as todo’s on the master list of PRs:
Todos might be something experienced GitHub users are familiar with, but I find that new GitHub users are blown away by this somewhat hidden feature. I use it to communicate that the pull request is still a work-in-progress, and what is still outstanding.
Hub CLI Tool
Hub is a command-line wrapper for git for working with GitHub. You can choose to alias git to hub, or use it as a stand-alone (I prefer the latter):
alias git=hub
With hub you can quickly clone and fork repositories:
hub clone laravel/framework-> git clone git://github.com/laravel/framework.github fork
In a GitHub project, you can browse the issues with the hub browse
command:
hub browse -- issues # or a specific projecthub browse laravel/framework issueshub browse laravel/framework wikihub browse laravel/framework pulls
And my favorite hub command is creating a pull request from the command line, which gives you a git commit
-like experience and finally outputs the PR link after creating the pull request:
hub pull-request # check for help infohub pull-request --help
Check out hub on hub.github.com for install instructions and more examples.
More Tips
Do you have some GitHub tips you love? Reply to Laravel News on Twitter and share your tips with @laravelnews.