How can I pull updates from a force-pushed branch?

2.1k views Asked by At

I aim to minimise commits on each merge request. To achieve this I rebase locally before pushing to the MR and, if required, rebase against "old" MR commits and force-push the result. (I obviously don't rebase against commits already in master).

If I need to update on a testing environment I can't do this:

git pull

...because of the force-push. So instead I do this:

git fetch --all
git checkout master
git branch -D feature_branch
git checkout feature_branch  # now includes force-pushed changes

Obviously I could write my own script/hook to do this in one step. Is way to update a local branch (when I know the remote copy has been force-pushed) with fewer built-in steps?

1

There are 1 answers

0
eazy_g On

One liner:

git fetch origin +feature_branch:feature_branch

This updates branch feature_branch in the local repository by fetching from the feature_branch branch in the remote repository.

The + symbol is key in that it means the feature_branch will be updated even if it does not fast-forward (which is typically (always?) the case with a force-pushed branch)