Ad

How To Use Git Rebase To Merge When Local Branch Behind Remote Branch?

- 1 answer

I'm working on a web project on the local branch, dev, and I currently complete my part. But here is the thing, my local branch is behind the remote branch main. Generally, I'd like to use git merge to merge remote branch. But this time, my manager ask me use rebase to merge, and I never rebase to merge remote branch. So, here I'm seeking help about the process that use rebase to merge when local branch behind remote branch.

Ad

Answer

  1. First fetch all remote branches: git fetch
  2. Then rebase your branch onto the remote main: git rebase main dev
  3. If there are conflicts, resolve them, git add, git rebase --continue
  4. (Force-) push your updated branch: git push origin dev or git push origin +dev

Alternatively, with git pull:

  1. Switch to branch git checkout dev
  2. git pull --rebase origin main
  3. Push

Personally, I always prefer fetch + local operations, because it allows me to verify the remote history, before creating new commits.

Ad
source: stackoverflow.com
Ad