Currently, after having rebased my feature branch onto the newest master commit, I have to use forced push to push the changes to the remote. Unfortunately, in this case it might happen, that I could lose commits which I have pushed from another machine.

Is there a way to tell Git to only allow the forced push if the remote branch in my local repository matches the corresponding branch in my remote repository?

1 Answer
1

You can either pull down those changes to your local branches, and solve any problems before pushing (which you might have to resolve on your other machine on the next pull otherwise), or you can push to specific branches with the :-notation.
git push origin [local-branch]:[remote-branch] Which also is the way to delete remote branches (i.e. git push origin :branch would push an empty branch to the specified remote-branch, thus deleting it).

This shouldn't touch any other branches at all. (if however there are external changes in the branch, take care to merge these in before force-pushing at all, by for instance branching onto a new branch for this).

Actually, now that I think about it, the better solution is to push the rebased branch to a new branch on the remote, instead of force-pushing to the current branch. (And then possibly deleting the old un-rebased branch if it's no longer needed), which would make for the least breakage.

Thanks for the 1st answer to my question. Technically, it might be possible that another developer has pushed changes after I pulled from the repository and before I force-pushed my changes. Technically, Git could check this, but unfortunately it doesn't. Using a different branch after each rebase is no option for me, because I may rebase multiple times each day.
–
MotFeb 10 '12 at 5:37