1

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?

Update: Please don't vote against forced push at all.

Mot
  • 28,248
  • 23
  • 84
  • 121
  • I'd consider using a separate remote repository for this and then completely disallow forced pushes in the main repo. – ThiefMaster Jan 11 '12 at 19:16
  • It's still a little unclear as to what your workflow is. Can you elaborate and give some "User A, User B do this and that and I want this to happen" scenarios? – Adam Dymitruk Jan 11 '12 at 20:20
  • Duplicate of [Is there a way to configure git repository to reject 'git push --force'?](http://stackoverflow.com/questions/1754491/is-there-a-way-to-configure-git-repository-to-reject-git-push-force)? – nulltoken Jan 11 '12 at 21:05
  • I *want* to use forced push to be able to rebase my feature branch onto the latest master changes. But I want to prevent silently overwriting other changes. – Mot Jan 12 '12 at 05:45

1 Answers1

0

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.

somaen
  • 21
  • 2
  • 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. – Mot Feb 10 '12 at 05:37