Git & GitHub Cheat Sheet

This cheat sheet is designed to keep you productive with Git and GitHub commands at your fingertips.

Git & GitHub Cheat Sheet

Photo by Yancy Min on Unsplash

Core Git Commands

  1. git init: Initializes a new Git repository in the current directory.

  2. git clone <repo_url>: Creates a local copy of a remote repository.

  3. git status: Displays the status of the working directory and staging area.

  4. git add <file_or_folder>: Adds changes to the staging area.

  5. git add .: Stages all changes in the current directory.

  6. git commit -m "message": Saves staged changes with a descriptive message.

  7. git push origin <branch>: Uploads local commits to the remote branch.

  8. git pull origin <branch>: Downloads changes from the remote branch and merges them into the local branch.

  9. git fetch origin: Downloads changes from the remote repository without merging.

  10. git merge <branch>: Combines changes from another branch into the current branch.

  11. git branch <branch_name>: Creates a new branch locally.

  12. git branch: Lists all local branches.

  13. git checkout <branch_name>: Switches to the specified branch (deprecated; use git switch).

  14. git switch <branch_name>: Switches branches (recommended).

  15. git log: Displays the commit history of the repository.

  16. git diff: Shows differences between the working directory and the staging area.

  17. git stash: Temporarily saves uncommitted changes for later use.

  18. git stash apply: Re-applies stashed changes without removing them.

  19. git reset <file>: Unstages changes and moves them back to the working directory.

  20. git reset --hard <commit>: Resets the repository to a specific commit, discarding all changes.

  21. git clean -f: Deletes untracked files in the working directory.

  22. git tag <tag_name>: Creates a tag for marking a specific commit.

  23. git show <tag_name>: Displays information about a specific tag.


GitHub-Specific Commands

  1. git remote -v: Lists all remote repositories associated with the local repo.

  2. git remote add origin <repo_url>: Links a local repository to a remote repository.

  3. git push --set-upstream origin <branch>: Sets the remote branch for tracking and pushes the current branch.

  4. gh auth login: Authenticates with your GitHub account (via GitHub CLI).

  5. gh repo create <name>: Creates a new repository on GitHub from the command line.

  6. gh repo clone <repo>: Clones a GitHub repository using GitHub CLI.

  7. gh pr create: Creates a pull request for the current branch (GitHub CLI).

  8. gh pr list: Lists all open pull requests for the repository (GitHub CLI).

  9. gh issue create: Opens a new issue on GitHub from the terminal.

  10. git fork: Creates a personal fork of a repository on GitHub (via GitHub CLI).

  11. git fetch upstream: Syncs changes from the upstream repository (useful for forks).

  12. git pull upstream main: Pulls updates from the upstream main branch into your local branch.


Collaboration Commands

  1. git rebase <branch>: Reapplies commits on top of another base branch (useful for maintaining a clean history).

  2. git cherry-pick <commit>: Applies a specific commit from another branch.

  3. git revert <commit>: Creates a new commit to reverse changes from a specific commit.

  4. git blame <file>: Shows who last modified each line of a file.

  5. git log --oneline: Displays a compact view of the commit history.

  6. git log --graph: Visualizes the commit history as a graph.


Advanced and Maintenance Commands

  1. git rm <file>: Removes a file from the repository and stages the change.

  2. git mv <old_name> <new_name>: Renames or moves a file and stages the change.

  3. git reflog: Shows the history of references in the repository (e.g., branches, HEAD).

  4. git archive: Creates an archive (e.g., .zip or .tar) of the repository or a specific branch.

  5. git bisect: Helps find the commit that introduced a bug by binary search.

  6. git submodule add <repo_url>: Adds a submodule (another repository) to the project.

  7. git submodule update --init: Initializes and updates all submodules.


Common Aliases (Optional)

  1. git hist: An alias for git log --graph --oneline --all (requires setup).

  2. git co: An alias for git 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 the main 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 or gh pr view for pull requests on GitHub.

  • Rebasing vs Merging: Use git rebase to keep a clean history, but prefer git merge for shared branches to preserve collaboration context.

  • Undoing Mistakes: Use git reset or git 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 with git 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>).