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
- Clone a specific branch:
$ git clone -branch <branch_name> <repo_url>
- Clone into a specific directory:
$ git clone <repo_url> <dir_name>
Managing Remote Repositories
- List remote repositories:
$ git remote
- Add a remote connection:
$ git remote add <remote> <url_to_remote>
- Remove a remote connection:
$ git remote rm <remote>
- Rename a remote connection:
$ git remote rename <old_name> <new_name>
Working with Files
- Add a file/directory for tracking:
$ git add <filename_or_dir>
- Add all files in current directory:
$ git add .
- Remove a file from working directory/staging:
$ git rm <filename_or_dir>
- Check repository status:
$ git status
- Commit staged changes:
$ git commit -m "Commit message"
- Stage and commit all tracked files:
$ git add -am "Commit message"
- Amend the latest commit message:
$ git commit --amend -m "New commit message"
Stashing Changes
- Stash staged/unstaged changes:
$ git stash
- Reapply and remove stash:
$ git stash pop
- Reapply and keep stash:
$ git stash apply
- Drop stash:
$ git stash drop
Viewing Differences
- Show uncommitted changes:
$ git diff
- Show differences between two commits:
$ git diff <id_1> <id_2>
Working with Branches
-
List all branches (local and remote with
-a
):
$ git branch
$ git branch -a
- Create a new branch (without switching):
$ git branch <new_branch>
- Switch to an existing branch:
$ git checkout <branch>
- Create and switch to a new branch:
$ git checkout -b <new_branch>
- Rename current branch:
$ git branch -m <new_name>
- Safe delete a local branch (prevents deleting unmerged changes):
$ git branch -d <branch>
- Force delete a local branch (merged or unmerged):
$ git branch -D <branch>
- Push a local branch to remote:
$ git push <remote_repo> <branch>
- Delete a remote branch:
$ git push <remote_repo> --delete <branch>
Merging Branches
- Merge a branch into main:
$ git checkout main
$ git merge <other_branch>
- Merge with a commit message (no fast-forward):
$ git merge --no-ff <other_branch>
- Compare differences between branches:
$ git diff <branch_1> <branch_2>
- Compare a single file between branches:
$ git diff <branch_1> <branch_2> <file>
Fetching and Pulling
- Fetch all commits/branches without merging:
$ git fetch <remote>
- Fetch a specific branch:
$ git fetch <remote> <branch>
- Merge fetched changes:
$ git merge <remote>/<branch>
- Pull (fetch and merge simultaneously):
$ git pull <remote>
Logging and Reviewing
- List all commits (with author, ID, date, message):
$ git log
-
List commits in one line (limit with
-n
, e.g.,-5
):
$ git log --oneline [-n]
- Log with diff information:
$ git log --stat
Reversing Changes
- Checkout older commits:
$ git checkout <commit_id>
- Undo latest commit (keep changes):
$ git reset HEAD~1
- Discard latest commit changes (no recovery):
$ git reset --hard HEAD~1
- Revert a specific commit (safe, may cause conflicts):
$ git revert <commit_id>
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.