All of lore.kernel.org
 help / color / mirror / Atom feed
* Adding --force-if-newer flag to git push
@ 2022-10-22 16:58 Sergey
  2022-10-22 17:48 ` Junio C Hamano
  2022-10-25  7:56 ` Erik Cervin Edin
  0 siblings, 2 replies; 3+ messages in thread
From: Sergey @ 2022-10-22 16:58 UTC (permalink / raw)
  To: git

Hello,

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?

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 because it would promote usage of this feature among the users. I just wanted to know your opinions and see if someone else would find it useul or it's just a dumb idea.

Thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Adding --force-if-newer flag to git push
  2022-10-22 16:58 Adding --force-if-newer flag to git push Sergey
@ 2022-10-22 17:48 ` Junio C Hamano
  2022-10-25  7:56 ` Erik Cervin Edin
  1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2022-10-22 17:48 UTC (permalink / raw)
  To: Sergey; +Cc: git

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.

> 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.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Adding --force-if-newer flag to git push
  2022-10-22 16:58 Adding --force-if-newer flag to git push Sergey
  2022-10-22 17:48 ` Junio C Hamano
@ 2022-10-25  7:56 ` Erik Cervin Edin
  1 sibling, 0 replies; 3+ messages in thread
From: Erik Cervin Edin @ 2022-10-25  7:56 UTC (permalink / raw)
  To: Sergey; +Cc: git

On Sat, Oct 22, 2022 at 7:02 PM Sergey <sryze@protonmail.com> wrote:
>
> 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 because it would promote usage of this feature among the users. I just wanted to know your opinions and see if someone else would find it useul or it's just a dumb idea.

Once you know the relevant options, it's pretty trivial to roll your
own script/workflow/configuration that suits your own work case. I'll
share how I solve the problem you describe. First, I'd like to mention
  git pull --rebase
which is basically
  git fetch origin && git rebase @{upstream}
or
  git pull --rebase=merges
which is basically
  git fetch origin && git rebase --rebase-merges @{upstream}
This does a fetch and afterwards, attempts to rebase any local changes
on top of the remote tracking branch. I'd recommend this approach over
force pushing. It keeps forced history rewrites away from the upstream
repository, which is generally best reserved for "truer" or more
finished work. It also allows for better recovery, in case you mess
up, chances are your history is preserved in the reflog. This can be
configured as the default either globally, per repo or per branch
https://git-scm.com/docs/git-config#Documentation/git-config.txt-pullrebase

Be aware that there are potential situations where this will not
automagically resolve the difference in history in a way that you may
expect and I'd only configure this as default if you feel comfortable
rolling back in case that situation arises. As a starting point, you
can manually initialize the rebase using @{upstream} or @{U}
 git fetch
 git rebase @{U}

If you want to investigate the divergence between a local branch and
its upstream, there are several commands that may be useful depending
on exactly how you want to compare them (often situational in my
experience.) The see which is newer you can run
  git show -q --format=fuller HEAD HEAD@{U}
but sometimes you want to know more say, how do the trees differ
  git log --online --graph HEAD...HEAD@{U}
or perhaps the difference in contents
  git diff HEAD HEAD@{U}
etc. etc.

Happy hunting!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-10-25  7:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-22 16:58 Adding --force-if-newer flag to git push Sergey
2022-10-22 17:48 ` Junio C Hamano
2022-10-25  7:56 ` Erik Cervin Edin

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.