Skip to content
abby-ql edited this page Mar 19, 2026 · 1 revision

Git Cheat Sheet

1. Initial Setup

git clone <repo-url>
cd <repo-name>

2. Create a New Feature Branch

Always branch off the latest main:

git checkout main
git pull upstream main   # or origin main
git checkout -b feature/your-feature-name

3. Make Changes & Commit

Check changes:

git status

Stage files:

git add .
# or
git add <file>

Commit:

git commit -m "feat: add xyz"

4. Push Your Branch

git push origin feature/your-feature-name

5. Create a Pull Request (PR)

  1. After pushing, go to GitHub

  2. Click Compare & pull request

  3. Add:

    • Clear title
    • Description
    • Testing notes
  4. Request reviewers

6. Keeping Your Branch Up to Date

Option A: Merge (easier)

git checkout main
git pull upstream main
git checkout feature/your-feature-name
git merge main

Option B: Rebase

git checkout feature/your-feature-name
git fetch upstream
git rebase upstream/main

7. Resolving Merge Conflicts

When Git says there are conflicts:

  1. Open the conflicted files
  2. Look for markers:
<<<<<<< HEAD
Your changes
=======
Incoming changes
>>>>>>> branch-name
  1. Edit to keep correct logic

Then:

git add .

If merging:

git commit

If rebasing:

git rebase --continue

8. Handling Rebase Issues

Abort if things go wrong:

git rebase --abort

After successful rebase, force push:

git push --force-with-lease

** Never use force push on shared branches like main

9. Checking Status

git status
git log --oneline --graph
git diff

10. Common Mistakes & Fixes

a. Forgot to pull latest main

Fix:

git pull upstream main

b. Committed to wrong branch

Fix:

git checkout correct-branch

11. Best Practices

  • Use clear branch names
  • Keep PRs small and focused & link PR to the specific issue
  • Pull latest main frequently
  • Write meaningful commit messages
  • Test before pushing
  • If someone merged a PR before expect conflicts
  • Always re test after resolving conflicts

12. Quick Workflow Summary

git checkout main
git pull upstream main
git checkout -b feature/x

# work
git add .
git commit -m "message"

# open PR
git push origin feature/x