Git & GitHub Cheat Sheet
This cheat sheet is designed to keep you productive with Git and GitHub commands at your fingertips.
Core Git Commands
git init
: Initializes a new Git repository in the current directory.git clone <repo_url>
: Creates a local copy of a remote repository.git status
: Displays the status of the working directory and staging area.git add <file_or_folder>
: Adds changes to the staging area.git add .
: Stages all changes in the current directory.git commit -m "message"
: Saves staged changes with a descriptive message.git push origin <branch>
: Uploads local commits to the remote branch.git pull origin <branch>
: Downloads changes from the remote branch and merges them into the local branch.git fetch origin
: Downloads changes from the remote repository without merging.git merge <branch>
: Combines changes from another branch into the current branch.git branch <branch_name>
: Creates a new branch locally.git branch
: Lists all local branches.git checkout <branch_name>
: Switches to the specified branch (deprecated; usegit switch
).git switch <branch_name>
: Switches branches (recommended).git log
: Displays the commit history of the repository.git diff
: Shows differences between the working directory and the staging area.git stash
: Temporarily saves uncommitted changes for later use.git stash apply
: Re-applies stashed changes without removing them.git reset <file>
: Unstages changes and moves them back to the working directory.git reset --hard <commit>
: Resets the repository to a specific commit, discarding all changes.git clean -f
: Deletes untracked files in the working directory.git tag <tag_name>
: Creates a tag for marking a specific commit.git show <tag_name>
: Displays information about a specific tag.
GitHub-Specific Commands
git remote -v
: Lists all remote repositories associated with the local repo.git remote add origin <repo_url>
: Links a local repository to a remote repository.git push --set-upstream origin <branch>
: Sets the remote branch for tracking and pushes the current branch.gh auth login
: Authenticates with your GitHub account (via GitHub CLI).gh repo create <name>
: Creates a new repository on GitHub from the command line.gh repo clone <repo>
: Clones a GitHub repository using GitHub CLI.gh pr create
: Creates a pull request for the current branch (GitHub CLI).gh pr list
: Lists all open pull requests for the repository (GitHub CLI).gh issue create
: Opens a new issue on GitHub from the terminal.git fork
: Creates a personal fork of a repository on GitHub (via GitHub CLI).git fetch upstream
: Syncs changes from the upstream repository (useful for forks).git pull upstream main
: Pulls updates from the upstream main branch into your local branch.
Collaboration Commands
git rebase <branch>
: Reapplies commits on top of another base branch (useful for maintaining a clean history).git cherry-pick <commit>
: Applies a specific commit from another branch.git revert <commit>
: Creates a new commit to reverse changes from a specific commit.git blame <file>
: Shows who last modified each line of a file.git log --oneline
: Displays a compact view of the commit history.git log --graph
: Visualizes the commit history as a graph.
Advanced and Maintenance Commands
git rm <file>
: Removes a file from the repository and stages the change.git mv <old_name> <new_name>
: Renames or moves a file and stages the change.git reflog
: Shows the history of references in the repository (e.g., branches, HEAD).git archive
: Creates an archive (e.g.,.zip
or.tar
) of the repository or a specific branch.git bisect
: Helps find the commit that introduced a bug by binary search.git submodule add <repo_url>
: Adds a submodule (another repository) to the project.git submodule update --init
: Initializes and updates all submodules.
Common Aliases (Optional)
git hist
: An alias forgit log --graph --oneline --all
(requires setup).git co
: An alias forgit checkout
(requires setup).
Usage Notes
Preview Changes: Use
git fetch
to check for updates on the remote repository without merging. This allows you to inspect changes before applying them locally.Meaningful Commit Messages: Write descriptive commit messages (e.g.,
git commit -m "Fix login button alignment issue"
) to make the history easy to understand.Branch Management: Use branches for feature development (e.g.,
git branch feature-login
) and keep themain
branch clean for production-ready code.Regular Syncing: Frequently sync with the remote repository using
git pull origin <branch>
to avoid merge conflicts.Stashing: Use
git stash
to save uncommitted work temporarily when switching branches or pulling updates.Code Review: Before merging branches, review changes using
git diff
orgh pr view
for pull requests on GitHub.Rebasing vs Merging: Use
git rebase
to keep a clean history, but prefergit merge
for shared branches to preserve collaboration context.Undoing Mistakes: Use
git reset
orgit revert
to undo changes safely.git revert
is ideal for public branches as it creates a new commit to reverse changes.Tracking Issues: Use GitHub issues (
gh issue create
) to keep track of bugs, feature requests, or tasks for the project.Tags for Releases: Use
git tag
to create versioned checkpoints (e.g.,v1.0.0
) and push tags withgit push origin <tag_name>
.Backups: Regularly push local changes to the remote repository (
git push
) to ensure work is not lost.Cleanup: Remove obsolete branches locally (
git branch -d <branch_name>
) and remotely (git push origin --delete <branch_name>
).