GitHub is a pivotal platform for collaboration in software development. With the right commands, automation scripts, and workflows, developers can dramatically improve their productivity and code quality. Here are 50 essential GitHub snippets every developer should know today.
I. Basic Git Commands
1. Initialize a new git repository
git init
2. Clone an existing repo
git clone https://github.com/user/repo.git
3. Check the status of your repo
git status
4. Stage all changes for commit
git add .
5. Commit with a meaningful message
git commit -m "Add feature X with proper validation"
6. Push commits to remote branch
git push origin main
7. Pull latest changes from remote
git pull origin main
8. Create a new branch and switch to it
git checkout -b feature/new-ui
9. Switch branch
git checkout develop
10. Delete branch locally
git branch -d feature/old-branch
II. GitHub-Specific Commands & Tips
11. Create a new repository using GitHub CLI
gh repo create my-new-project --public --description "A new repository"
12. Open current repo on GitHub page
gh repo view --web
13. List all pull requests for repo
gh pr list
14. Check out a pull request locally
gh pr checkout 42
15. Create a pull request via CLI
gh pr create --title "Add new login flow" --body "Implemented OAuth2 login" --base main --head feature/login
III. Branching & Merging
16. Merge a branch into current branch
git merge feature/new-ui
17. Rebase current branch onto main
git rebase main
18. Abort a merge or rebase if conflict occurs
git merge --abort
IV. Undoing and Resetting
19. Unstage files before commit
git reset HEAD <file>
20. Discard unstaged changes in a file
git checkout -- <file>
21. Revert a commit by creating a new commit
git revert <commit_hash>
22. Reset commits and changes (Dangerous: use with caution)
git reset --hard HEAD~1
V. Useful Git Alias Examples
23. Speedy status command
git config --global alias.s 'status -s'
24. Log oneline with graph and colors
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
VI. Managing Remote Repositories
25. Add remote origin
git remote add origin https://github.com/user/repo.git
26. Change URL for remote origin
git remote set-url origin git@github.com:user/repo.git
27. Remove a remote
git remote remove origin
VII. Tagging Releases
28. Create an annotated tag
git tag -a v1.0 -m "Version 1.0 release"
29. Push tag to remote
git push origin v1.0
30. Delete a remote tag
git push --delete origin v1.0
VIII. GitHub Actions Snippets
31. Basic workflow for Node.js project
name: Node CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '16'
- run: npm install
- run: npm test
32. Run tests on multiple Node versions
strategy:
matrix:
node-version: [12, 14, 16]
steps:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
33. Cache dependencies in GitHub Actions
- name: Cache NPM modules
uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
34. Upload artifacts
- uses: actions/upload-artifact@v3
with:
name: my-artifact
path: path/to/file
35. Notify on Slack in GitHub Actions
- name: Slack Notification
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,message,commit,author
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
IX. GitHub CLI Snippets
36. List repositories for current user
gh repo list --limit 10
37. Create a new issue
gh issue create --title "Bug report" --body "There is an issue with login"
38. View pull request details
gh pr view 123 --web
39. Close an issue
gh issue close 42
40. Assign yourself to an issue
gh issue edit 42 --add-assignee @me
X. GitHub Markdown Snippets
41. Task List Syntax
- [x] Item 1 done
- [ ] Item 2 pending
42. Table Example
| Feature | Supported |
| -------- | --------- |
| Login | ✅ |
| Signup | ❌ |
43. Embedding Images

XI. Collaboration and Review
44. Merge pull request via CLI
gh pr merge 123 --squash --delete-branch
45. Request review from team member
gh pr review 123 --request "username"
46. View changes in PR
gh pr diff 123
XII. Automation with GitHub Actions
47. Auto Label on PRs
name: Label PRs
on:
pull_request:
types: [opened, synchronize]
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v3
48. Auto Close Stale Issues
name: Close stale
on:
schedule:
- cron: '0 0 * * 0'
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v3
XIII. Git Tips & Tricks
49. Show last commit for each file in directory
git ls-files | xargs -I{} git log -1 --format="%h %ad %an" -- {}
50. Squash last N commits interactively
git rebase -i HEAD~3
Top comments (0)