Skip to main content

Command Palette

Search for a command to run...

Git & GitHub

Published
•7 min read•View as Markdown

Git & GitHub Cheat Sheet – What, Why, When, How


1. Git Basics

1.1 git init

  • What: Initializes a new local Git repository.

  • Why: To start version control in a project.

  • When: When creating a new project from scratch.

  • How:

git init

1.2 git clone

  • What: Creates a local copy of a remote repository.

  • Why: To work on an existing project hosted on GitHub.

  • When: When you want to collaborate or start with an existing codebase.

  • How:

git clone <repo_url>
git clone -b <branch_name> <repo_url>  # Specific branch

1.3 git status

  • What: Shows the current status of files in the repository.

  • Why: To check which files are modified, staged, or untracked.

  • When: Before staging or committing changes.

  • How:

git status

2. Staging & Committing

2.1 git add

  • What: Moves changes from working directory → staging area.

  • Why: Prepares files for committing.

  • When: After modifying/creating files.

  • How:

git add <file>
git add .  # All changes

2.2 git commit

  • What: Saves staged changes into repository history.

  • Why: To create a permanent record of your changes.

  • When: After staging meaningful updates.

  • How:

git commit -m "Meaningful message"

3. Branching & Merging

3.1 git branch

  • What: Creates, lists, or deletes branches.

  • Why: To work on features independently.

  • When: Before starting a new feature/bug fix.

  • How:

git branch                 # List branches
git branch <name>          # Create new branch
git branch -d <name>       # Delete branch

3.2 git checkout / git switch

  • What: Switches between branches or restores files.

  • Why: To change context or retrieve a file’s version.

  • When: Before making changes in another branch.

  • How:

git checkout <branch>
git checkout -b <new_branch>  # Create & switch

3.3 git merge

  • What: Combines another branch into the current branch.

  • Why: To integrate changes from different branches.

  • When: After completing a feature branch and wanting it in main.

  • How:

git checkout main
git merge feature-branch

4. Undoing Changes

4.1 git reset

  • What: Unstages or removes commits.

  • Why: To discard changes or redo commits.

  • When: Mistaken commits or staging.

  • How:

git reset <file>           # Unstage file
git reset --soft HEAD~1    # Undo commit, keep changes
git reset --hard HEAD~1    # Undo commit, discard changes

4.2 git revert

  • What: Creates a new commit to undo a previous commit.

  • Why: Safe way to undo in shared repositories.

  • When: When you need to “undo” without rewriting history.

  • How:

git revert <commit_hash>

5. Remote Repositories (GitHub)

5.1 git remote

  • What: Connects local repo to a remote (like GitHub).

  • Why: To push/pull changes between local and remote.

  • When: After initializing a project or cloning a new one.

  • How:

git remote add origin <repo_url>

5.2 git push

  • What: Sends local commits to remote repository.

  • Why: To share your work with team or backup online.

  • When: After committing changes.

  • How:

git push -u origin main
git push

5.3 git pull

  • What: Fetches and merges remote changes into local.

  • Why: To sync your local repository with remote updates.

  • When: Before pushing new code to avoid conflicts.

  • How:

git pull
git pull --rebase  # Cleaner history

6. Tags (Releases)

6.1 git tag

  • What: Marks specific commits as releases.

  • Why: To version your software.

  • When: Before deploying or releasing.

  • How:

git tag v1.0
git tag -a v1.0 -m "Release 1.0"
git push origin v1.0

7. Stash (Temporary Save)

7.1 git stash

  • What: Temporarily saves changes without committing.

  • Why: To switch branches without losing work.

  • When: Midway through a task but need to change branch.

  • How:

git stash
git stash pop
git stash list

8. Collaboration (GitHub Workflow)

  • What: Workflow for team collaboration.

  • Why: Ensures safe code integration.

  • When: Always in team projects.

  • How:

    1. Fork → Clone → Create Branch

    2. Make changes → Commit → Push

    3. Open Pull Request → Review → Merge


9. Common Problems

  • Merge Conflicts:

    • What: Conflicting changes between branches.

    • Why: Two people changed the same file lines.

    • When: During merge/pull.

    • How: Edit file manually → git add . → git commit

  • Detached HEAD:

    • What: Not on any branch, just a commit.

    • How to fix: git checkout main

Git & GitHub Cheat Sheet


1. Git Basics

Configuration

git config --global user.name "Your Name"         # Set username
git config --global user.email "you@example.com"  # Set email
git config --list                                # View all configurations
  • Why? Identifies you as the author of commits.

  • Tip: Use --global for all repositories, omit it to set per repository.


Repository Initialization

git init    # Initialize a new Git repository
  • Use: Start version control in a new project.

Cloning Repositories

git clone <repo_url>      # Clone a repository from GitHub
git clone -b <branch> <repo_url>  # Clone specific branch
  • Tip: Clones all history by default.

Check Status

git status   # Shows changes (staged, unstaged, untracked)
  • Use: Always run before commit to verify state.

2. Staging & Committing

Staging Changes

git add <file>        # Stage a specific file
git add .             # Stage all changes
  • Unstage:
git reset <file>

Committing Changes

git commit -m "Meaningful commit message"
  • Best Practice: Use concise, descriptive messages.

3. Branching

Create & Switch Branches

git branch                   # List all branches
git branch <branch_name>     # Create a new branch
git checkout <branch_name>   # Switch to branch
git checkout -b <branch_name> # Create & switch

Merge Branches

git checkout main
git merge <branch_name>   # Merge changes into main
  • Possible conflicts: Resolve manually, then:
git add .
git commit

Delete Branch

git branch -d <branch>   # Delete (safe)
git branch -D <branch>   # Force delete

4. Viewing History

git log                      # Full commit history
git log --oneline            # Compact history
git log --graph --oneline --decorate --all  # Visual
git diff                     # View unstaged changes
git diff --staged            # View staged changes

5. Undoing Changes

Undo last commit (keep changes)

git reset --soft HEAD~1

Undo last commit (discard changes)

git reset --hard HEAD~1

Remove from staging

git reset <file>

Revert a commit

git revert <commit_hash>

6. Remote Repositories (GitHub)

Add Remote

git remote add origin <repo_url>

Push Changes

git push -u origin main    # First push
git push                   # Subsequent pushes

Pull Changes

git pull                   # Fetch & merge
git pull --rebase          # Fetch & rebase (cleaner history)

Fetch without merging

git fetch

7. Tags (Releases)

git tag v1.0                  # Create a tag
git tag -a v1.0 -m "Release"  # Annotated tag
git push origin v1.0          # Push tag to GitHub

8. Stash (Temporary Save)

git stash           # Save changes temporarily
git stash pop       # Re-apply last stash
git stash list      # View stashes

9. Collaboration (GitHub Workflow)

  1. Fork → Copy repository to your account.

  2. Clone → git clone <forked_repo_url>

  3. Create Branch → git checkout -b feature-branch

  4. Make changes → Commit → Push

  5. Open Pull Request (PR) → Review & Merge.


10. Common .gitignore Patterns

# Ignore Python
*.pyc
__pycache__/

# Ignore Node modules
node_modules/

# Ignore environment files
.env
  • Add via: .gitignore file.

11. Advanced Commands

Rebase

git rebase main
  • Use: Clean history by moving commits on top of main.

Cherry-pick

git cherry-pick <commit_hash>
  • Use: Pick a single commit from another branch.

Squash Commits

git rebase -i HEAD~3   # Squash last 3 commits

Blame

git blame <file>
  • Use: Show who changed each line.

12. GitHub Specific

  • Create Repository: From GitHub → New → Add README.

  • Clone via HTTPS/SSH: Prefer SSH for secure access.

  • Issues & Pull Requests: Track bugs & manage contributions.

  • Actions: CI/CD workflows.

  • Branch Protection Rules: Prevent force pushes to main.


13. Reset vs Revert vs Checkout

  • Reset: Moves HEAD to previous commit (can discard changes).

  • Revert: Creates a new commit to undo changes (safe for shared repos).

  • Checkout: Switches between branches or restores file.


14. Best Practices

  • Commit often with meaningful messages.

  • Use branches for new features.

  • Never push directly to main/master (use Pull Requests).

  • Regularly pull/rebase to avoid conflicts.

  • Always .gitignore sensitive files (like .env, passwords).

More from this blog