Home > Software engineering >  How to rebase feature branch off of master branch often, without force pushing to feature branch
How to rebase feature branch off of master branch often, without force pushing to feature branch

Time:04-24

We have a master branch, and feature branches where we do our work.
My organization follows the rebase workflow.

We branch off the master branch:

(master)git checkout -b feature

After a few commits when it's time to log off for the day, we simply push:

(feature)git push

Once the feature is ready, we rebase the feature branch off of the master branch:

(feature)git checkout master
(master)git pull
(master)git checkout feature
(feature)git rebase master

Solve any merge conflicts in case it's a Monday.
Then we push the local branch to origin, so that the manager can merge it with master using the gitlab interface.
Here, we are forced to force push feature to the remote:

(feature)git push -f

This is pretty much what my entire team does, everyone usually works on different branches so it's not an issue. But I still want to avoid forcing.

Safer alternative:

(feature)git push --force-with-lease

Using force-with-lease fails in case the push was about to override someone else's commits.

But is anyone aware of another way where we avoid any type of force at all.

Possibly something where the rebase shows up as an extra commit?

CodePudding user response:

Rebasing recreates your commits as new commits. That means that pushes to the same branch will no longer be fast-forward pushes. If you want to push your rebased commits to the same branch, the only way is to use force.

You could push to a new branch (different branch name) to avoid force.

CodePudding user response:

By definition, a rebase means you're rewriting the history of your branch.

If you want to update your feature branch with the changes from master/main without requiring a force-push afterwards, you need to use a merge.

Your two options would be a traditional merge-commit, or a squash-and-merge. Traditional merge commit would just be done like this:

git checkout feature
git merge master --no-ff

This will both update feature with the changes from master, and it will make master one of the parents of feature.

The commit-graph looks like this:

git merge master --no-ff

The way that you've described it, "something where the rebase shows up as an extra commit?", can be done using a "squash-merge":

git checkout feature
git merge --squash master
git commit

This will update feature with the changes from master by squashing all of the changes from master into a single commit onto feature. It does not change the parents of feature.

This commit-graph looks like this:

git merge --squash

  • Related