Skip to main content

Useful Git

Git rebase

git rebase flow is basically a cleaner alterrnative to merge-based workflows. instead of creating messy merge commits, you "replay" your work on top of the lastest branch.

🔁 The idea (simple)

You have:

main:     A --- B --- C
feature: D --- E

After rebbase:

main:     A --- B --- C
feature: D' --- E'

Your commits (D,E) are rewritten on top of the lastest main.

🚀 Basic Rebase Flow

1. Create a feature branch

git checkout main
git pull origin main
git checkout -b feature/login

2. Work and commit

git add .
git commit -m "add login form"

3. Rebase with latest main (IMPORTANT STEP)

Before pushing or opening PR:

git checkout main
git pull origin main

git checkout feature/login
git rebase main

4. Fix conflicts (if any)

If Git stops:

# fix files manually

git add .
git rebase --continue

5. Push (force required ⚠️)

Because history changed:

git push origin feature/login --force

👉 Use --force-with-lease (safer):

git push --force-with-lease

🔀 Rebase vs Merge (real difference)

Merge:

git merge main
  • Keeps history
  • Adds merge commit ❌ (messy)

Rebase:

git rebase main
  • Clean history
  • Looks like linear timeline

🧠 Team Rebase Flow (clean workflow)

  1. Always branch from main
  2. Before PR → rebase on latest main
  3. Keep commits clean (optional: squash)
  4. Push with --force-with-lease
  5. Merge to main (usually fast-forward or squash)