DEV Community

Cover image for GitHub Contributions CheatSheat
Harsh Bhardwaj
Harsh Bhardwaj

Posted on

GitHub Contributions CheatSheat

GitHub Cheat Sheet for Contributions

This cheat sheet focuses on the most used Git commands and those critical for open source contributions, such as cloning, branching, committing, pushing, pulling, merging, and handling conflicts. It’s designed for efficient collaboration on GitHub, incorporating key definitions and commands for contribution workflows.

Key Definitions

  • Local repository: A directory on your machine containing project code and files.
  • Remote repository: An online version hosted on platforms like GitHub.
  • Cloning: Copying a repository to a new directory.
  • Commit: A snapshot of project changes you can revert to.
  • Branch: An isolated copy of the project for working without affecting the main project.
  • Git merge: Combining two branches.
  • .gitignore file: Lists files Git should not track (e.g., large data, private info).
  • Staging area: A cache for changes to be committed next.
  • Git stash: A cache for saving changes to revisit later.
  • Commit ID/hash: A unique identifier for a commit.
  • HEAD: Refers to the latest commit; HEAD~n refers to the nth previous commit (e.g., HEAD~2 is the second-to-last commit).

Cloning Repositories

  • Clone a repository (HTTPS recommended):
  $ git clone https://github.com/your_username/repo_name.git
Enter fullscreen mode Exit fullscreen mode
  • Clone a specific branch:
  $ git clone -branch <branch_name> <repo_url>
Enter fullscreen mode Exit fullscreen mode
  • Clone into a specific directory:
  $ git clone <repo_url> <dir_name>
Enter fullscreen mode Exit fullscreen mode

Managing Remote Repositories

  • List remote repositories:
  $ git remote
Enter fullscreen mode Exit fullscreen mode
  • Add a remote connection:
  $ git remote add <remote> <url_to_remote>
Enter fullscreen mode Exit fullscreen mode
  • Remove a remote connection:
  $ git remote rm <remote>
Enter fullscreen mode Exit fullscreen mode
  • Rename a remote connection:
  $ git remote rename <old_name> <new_name>
Enter fullscreen mode Exit fullscreen mode

Working with Files

  • Add a file/directory for tracking:
  $ git add <filename_or_dir>
Enter fullscreen mode Exit fullscreen mode
  • Add all files in current directory:
  $ git add .
Enter fullscreen mode Exit fullscreen mode
  • Remove a file from working directory/staging:
  $ git rm <filename_or_dir>
Enter fullscreen mode Exit fullscreen mode
  • Check repository status:
  $ git status
Enter fullscreen mode Exit fullscreen mode
  • Commit staged changes:
  $ git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode
  • Stage and commit all tracked files:
  $ git add -am "Commit message"
Enter fullscreen mode Exit fullscreen mode
  • Amend the latest commit message:
  $ git commit --amend -m "New commit message"
Enter fullscreen mode Exit fullscreen mode

Stashing Changes

  • Stash staged/unstaged changes:
  $ git stash
Enter fullscreen mode Exit fullscreen mode
  • Reapply and remove stash:
  $ git stash pop
Enter fullscreen mode Exit fullscreen mode
  • Reapply and keep stash:
  $ git stash apply
Enter fullscreen mode Exit fullscreen mode
  • Drop stash:
  $ git stash drop
Enter fullscreen mode Exit fullscreen mode

Viewing Differences

  • Show uncommitted changes:
  $ git diff
Enter fullscreen mode Exit fullscreen mode
  • Show differences between two commits:
  $ git diff <id_1> <id_2>
Enter fullscreen mode Exit fullscreen mode

Working with Branches

  • List all branches (local and remote with -a):
  $ git branch
  $ git branch -a
Enter fullscreen mode Exit fullscreen mode
  • Create a new branch (without switching):
  $ git branch <new_branch>
Enter fullscreen mode Exit fullscreen mode
  • Switch to an existing branch:
  $ git checkout <branch>
Enter fullscreen mode Exit fullscreen mode
  • Create and switch to a new branch:
  $ git checkout -b <new_branch>
Enter fullscreen mode Exit fullscreen mode
  • Rename current branch:
  $ git branch -m <new_name>
Enter fullscreen mode Exit fullscreen mode
  • Safe delete a local branch (prevents deleting unmerged changes):
  $ git branch -d <branch>
Enter fullscreen mode Exit fullscreen mode
  • Force delete a local branch (merged or unmerged):
  $ git branch -D <branch>
Enter fullscreen mode Exit fullscreen mode
  • Push a local branch to remote:
  $ git push <remote_repo> <branch>
Enter fullscreen mode Exit fullscreen mode
  • Delete a remote branch:
  $ git push <remote_repo> --delete <branch>
Enter fullscreen mode Exit fullscreen mode

Merging Branches

  • Merge a branch into main:
  $ git checkout main
  $ git merge <other_branch>
Enter fullscreen mode Exit fullscreen mode
  • Merge with a commit message (no fast-forward):
  $ git merge --no-ff <other_branch>
Enter fullscreen mode Exit fullscreen mode
  • Compare differences between branches:
  $ git diff <branch_1> <branch_2>
Enter fullscreen mode Exit fullscreen mode
  • Compare a single file between branches:
  $ git diff <branch_1> <branch_2> <file>
Enter fullscreen mode Exit fullscreen mode

Fetching and Pulling

  • Fetch all commits/branches without merging:
  $ git fetch <remote>
Enter fullscreen mode Exit fullscreen mode
  • Fetch a specific branch:
  $ git fetch <remote> <branch>
Enter fullscreen mode Exit fullscreen mode
  • Merge fetched changes:
  $ git merge <remote>/<branch>
Enter fullscreen mode Exit fullscreen mode
  • Pull (fetch and merge simultaneously):
  $ git pull <remote>
Enter fullscreen mode Exit fullscreen mode

Logging and Reviewing

  • List all commits (with author, ID, date, message):
  $ git log
Enter fullscreen mode Exit fullscreen mode
  • List commits in one line (limit with -n, e.g., -5):
  $ git log --oneline [-n]
Enter fullscreen mode Exit fullscreen mode
  • Log with diff information:
  $ git log --stat
Enter fullscreen mode Exit fullscreen mode

Reversing Changes

  • Checkout older commits:
  $ git checkout <commit_id>
Enter fullscreen mode Exit fullscreen mode
  • Undo latest commit (keep changes):
  $ git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Discard latest commit changes (no recovery):
  $ git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Revert a specific commit (safe, may cause conflicts):
  $ git revert <commit_id>
Enter fullscreen mode Exit fullscreen mode

Contribution Workflow Tips

  • Fork the repository and clone your fork (git clone <forked_repo_url>).
  • Create a branch for your changes (git checkout -b fix-issue-123).
  • Commit frequently with clear messages (git commit -m "Fix bug in X").
  • Pull upstream changes to avoid conflicts (git pull origin main).
  • Push to your fork (git push origin <branch>).
  • Open a PR with a clear description, referencing the issue (e.g., “Fixes #123”).
  • Resolve merge conflicts by editing conflicted files, staging, and committing.
  • Respond to feedback promptly, pushing updates to the same branch.
  • Follow up politely if no response after a week (e.g., “Any updates on this PR?”).
  • Be ethical: Respect the code of conduct, avoid spamming PRs, and prioritize quality.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.