DEV Community

Software Developer
Software Developer

Posted on

50 Most Useful GitHub Snippets for Developers

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
Enter fullscreen mode Exit fullscreen mode

2. Clone an existing repo

git clone https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

3. Check the status of your repo

git status
Enter fullscreen mode Exit fullscreen mode

4. Stage all changes for commit

git add .
Enter fullscreen mode Exit fullscreen mode

5. Commit with a meaningful message

git commit -m "Add feature X with proper validation"
Enter fullscreen mode Exit fullscreen mode

6. Push commits to remote branch

git push origin main
Enter fullscreen mode Exit fullscreen mode

7. Pull latest changes from remote

git pull origin main
Enter fullscreen mode Exit fullscreen mode

8. Create a new branch and switch to it

git checkout -b feature/new-ui
Enter fullscreen mode Exit fullscreen mode

9. Switch branch

git checkout develop
Enter fullscreen mode Exit fullscreen mode

10. Delete branch locally

git branch -d feature/old-branch
Enter fullscreen mode Exit fullscreen mode

II. GitHub-Specific Commands & Tips

11. Create a new repository using GitHub CLI

gh repo create my-new-project --public --description "A new repository"
Enter fullscreen mode Exit fullscreen mode

12. Open current repo on GitHub page

gh repo view --web
Enter fullscreen mode Exit fullscreen mode

13. List all pull requests for repo

gh pr list
Enter fullscreen mode Exit fullscreen mode

14. Check out a pull request locally

gh pr checkout 42
Enter fullscreen mode Exit fullscreen mode

15. Create a pull request via CLI

gh pr create --title "Add new login flow" --body "Implemented OAuth2 login" --base main --head feature/login
Enter fullscreen mode Exit fullscreen mode

III. Branching & Merging

16. Merge a branch into current branch

git merge feature/new-ui
Enter fullscreen mode Exit fullscreen mode

17. Rebase current branch onto main

git rebase main
Enter fullscreen mode Exit fullscreen mode

18. Abort a merge or rebase if conflict occurs

git merge --abort
Enter fullscreen mode Exit fullscreen mode

IV. Undoing and Resetting

19. Unstage files before commit

git reset HEAD <file>
Enter fullscreen mode Exit fullscreen mode

20. Discard unstaged changes in a file

git checkout -- <file>
Enter fullscreen mode Exit fullscreen mode

21. Revert a commit by creating a new commit

git revert <commit_hash>
Enter fullscreen mode Exit fullscreen mode

22. Reset commits and changes (Dangerous: use with caution)

git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode

V. Useful Git Alias Examples

23. Speedy status command

git config --global alias.s 'status -s'
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

VI. Managing Remote Repositories

25. Add remote origin

git remote add origin https://github.com/user/repo.git
Enter fullscreen mode Exit fullscreen mode

26. Change URL for remote origin

git remote set-url origin git@github.com:user/repo.git
Enter fullscreen mode Exit fullscreen mode

27. Remove a remote

git remote remove origin
Enter fullscreen mode Exit fullscreen mode

VII. Tagging Releases

28. Create an annotated tag

git tag -a v1.0 -m "Version 1.0 release"
Enter fullscreen mode Exit fullscreen mode

29. Push tag to remote

git push origin v1.0
Enter fullscreen mode Exit fullscreen mode

30. Delete a remote tag

git push --delete origin v1.0
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

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-
Enter fullscreen mode Exit fullscreen mode

34. Upload artifacts

- uses: actions/upload-artifact@v3
  with:
    name: my-artifact
    path: path/to/file
Enter fullscreen mode Exit fullscreen mode

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 }}
Enter fullscreen mode Exit fullscreen mode

IX. GitHub CLI Snippets

36. List repositories for current user

gh repo list --limit 10
Enter fullscreen mode Exit fullscreen mode

37. Create a new issue

gh issue create --title "Bug report" --body "There is an issue with login"
Enter fullscreen mode Exit fullscreen mode

38. View pull request details

gh pr view 123 --web
Enter fullscreen mode Exit fullscreen mode

39. Close an issue

gh issue close 42
Enter fullscreen mode Exit fullscreen mode

40. Assign yourself to an issue

gh issue edit 42 --add-assignee @me
Enter fullscreen mode Exit fullscreen mode

X. GitHub Markdown Snippets

41. Task List Syntax

- [x] Item 1 done
- [ ] Item 2 pending
Enter fullscreen mode Exit fullscreen mode

42. Table Example

| Feature  | Supported |
| -------- | --------- |
| Login    | ✅        |
| Signup   | ❌        |
Enter fullscreen mode Exit fullscreen mode

43. Embedding Images

![Alt text](https://via.placeholder.com/150)
Enter fullscreen mode Exit fullscreen mode

XI. Collaboration and Review

44. Merge pull request via CLI

gh pr merge 123 --squash --delete-branch
Enter fullscreen mode Exit fullscreen mode

45. Request review from team member

gh pr review 123 --request "username"
Enter fullscreen mode Exit fullscreen mode

46. View changes in PR

gh pr diff 123
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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" -- {}
Enter fullscreen mode Exit fullscreen mode

50. Squash last N commits interactively

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)