git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 22, 2022, 17:48 UTC
Message-ID
<xmqqmt9nhhqm.fsf@gitster.g>
In-Reply-To
<491oWe6HDMJXQK2Ru2VbzFrlUajNlVhnIErZZ1dxUaPpCElsgpASAEQFc2Zi1R4G53hAIMHopETEJn7E_TzeeC6uOzGBffQDRK2AWYPIxKQ=@protonmail.com>
Sergey <sryze@protonmail.com> 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.

Show 5 quoted lines
> 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.

Previous: SergeyNext: Erik Cervin Edin
Message 2 of 3 in “Adding --force-if-newer flag to git push”
  1. SergeyOct 22, 2022
  2. Junio C HamanoOct 22, 2022
  3. Erik Cervin EdinOct 25, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.