Skip to content

Git: Update local branch after rebase

Quite often we have to use the git rebase to realign the branch we are working on with the latest version of its parent branch.
After the rebase, your coworkers need to update their version of the branch.

The problem

You, and your coworkers are working on a new feature... Obviously, you created a feature branch to work on.
A couple of sprints further, the other teams have continued to fix bugs or implemented their features.

graph LR
  A[A] --> B[B];
  B[B] --> C[C];
  C[C] --> D[D];
  D[D] --> E[E];
  E[E] -.- F{{main}};
  C[C] --> GG[G];
  GG[G] --> HH[H];
  HH[H] --> II[I];
  II[I] -.- JJ{{feature/foo}};

You ar no longer aligned with the latest version. If your feature is done, a git merge might be an option... but quite often, one will use a git rebase to realign the branches.

git checkout feature/foo
git rebase main
git push -f

Done...
On your side.

But your coworkers also need the rebased version of the feature branch.
A git pull will not work as a git rebase rewrites the commits and therefor the hashes won't match.

This is the way

The long option

  1. checkout to another branch
  2. delete local branch
  3. fetch latest version of the branch from the repo
git checkout main
git branch -D feature/foo
git pull
git checkout feature/foo

But we developers are lazy...
This can go faster

In two lines

This is more efficient:

git fetch origin feature/foo
git reset --hard origin/feature/foo

Warning

The git reset will remove all uncommitted changes AND all the commits that were not pushed to the repository before the rebase will be lost.

Comments