Subject: Re: Adding --force-if-newer flag to git push

From: Junio C Hamano <[email-masked]>
Date: Sat, 22 Oct 2022 10:48:33 -0700

Sergey <[email-masked]> writes:

> Would it be a good idea to add a new flag --force-if-newer to the
> "git push" command, so that force-pushing would succeed only if
> the last local commit's date is newer than that on the remote
> branch?

Probably not.  I do not see how we can force the clock on the
machine that created the commit (which may not be even yours)
sitting at the tip on the branch you are trying to overwrite to be
in sync with the clock on the local machine on which you created the
commit you are trying to use to replace it with.

> Sometimes I find that a feature like this would be useful when I
> work on multiple different computers and I want to just push all
> local branches to the repo at once to sync it with whatever is the
> latest version. I know that using --force is kind of frowned upon
> in the Git community, so this is probably not the best idea ...

In such a workflow, not forcing is the more reasonable option.

After seeing your push fail because you forgot that you have pushed
other changes from another machine, you first fetch and consolidate
what was done on the local machine into what you have pushed before
to the remote, and push again.  If you rebased your local work on
top of what you pushed there but forgot that you have pushed, this
time around the push will fast-forward.  If you merged your local
work, it will also fast-forward.  Either way, not forcing would have
caught you from losing some work you did on another machine, and
pushing the result can be done without forcing.