{"thread":{"id":"58674","subject":"Adding --force-if-newer flag to git push","startedAt":"2022-10-22T16:59:26Z","lastAt":"2022-10-25T07:57:00Z","messageCount":3,"participants":["Sergey","Junio C Hamano","Erik Cervin Edin"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"465567","messageId":"491oWe6HDMJXQK2Ru2VbzFrlUajNlVhnIErZZ1dxUaPpCElsgpASAEQFc2Zi1R4G53hAIMHopETEJn7E_TzeeC6uOzGBffQDRK2AWYPIxKQ=@protonmail.com","threadId":"58674","inReplyTo":null,"subject":"Adding --force-if-newer flag to git push","fromName":"Sergey","fromEmail":"sryze@protonmail.com","sentAt":"2022-10-22T16:58:55Z","receivedAt":"2022-10-22T16:59:26Z","isPatch":false,"sender":{"key":"sryze@protonmail.com","avatar":null},"body":"Hello,\n\nWould 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?\n\nSometimes 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.\n\nThanks.\n"},{"id":"465569","messageId":"xmqqmt9nhhqm.fsf@gitster.g","threadId":"58674","inReplyTo":"491oWe6HDMJXQK2Ru2VbzFrlUajNlVhnIErZZ1dxUaPpCElsgpASAEQFc2Zi1R4G53hAIMHopETEJn7E_TzeeC6uOzGBffQDRK2AWYPIxKQ=@protonmail.com","subject":"Re: Adding --force-if-newer flag to git push","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2022-10-22T17:48:33Z","receivedAt":"2022-10-22T17:48:42Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Sergey <sryze@protonmail.com> writes:\n\n> Would it be a good idea to add a new flag --force-if-newer to the\n> \"git push\" command, so that force-pushing would succeed only if\n> the last local commit's date is newer than that on the remote\n> branch?\n\nProbably not.  I do not see how we can force the clock on the\nmachine that created the commit (which may not be even yours)\nsitting at the tip on the branch you are trying to overwrite to be\nin sync with the clock on the local machine on which you created the\ncommit you are trying to use to replace it with.\n\n> Sometimes I find that a feature like this would be useful when I\n> work on multiple different computers and I want to just push all\n> local branches to the repo at once to sync it with whatever is the\n> latest version. I know that using --force is kind of frowned upon\n> in the Git community, so this is probably not the best idea ...\n\nIn such a workflow, not forcing is the more reasonable option.\n\nAfter seeing your push fail because you forgot that you have pushed\nother changes from another machine, you first fetch and consolidate\nwhat was done on the local machine into what you have pushed before\nto the remote, and push again.  If you rebased your local work on\ntop of what you pushed there but forgot that you have pushed, this\ntime around the push will fast-forward.  If you merged your local\nwork, it will also fast-forward.  Either way, not forcing would have\ncaught you from losing some work you did on another machine, and\npushing the result can be done without forcing.\n\n"},{"id":"465687","messageId":"CA+JQ7M_woJmQdaB24-eNxSRJLN4GPgTyZcPfKsXAkP+B1QOT-Q@mail.gmail.com","threadId":"58674","inReplyTo":"491oWe6HDMJXQK2Ru2VbzFrlUajNlVhnIErZZ1dxUaPpCElsgpASAEQFc2Zi1R4G53hAIMHopETEJn7E_TzeeC6uOzGBffQDRK2AWYPIxKQ=@protonmail.com","subject":"Re: Adding --force-if-newer flag to git push","fromName":"Erik Cervin Edin","fromEmail":"erik@cervined.in","sentAt":"2022-10-25T07:56:15Z","receivedAt":"2022-10-25T07:57:00Z","isPatch":false,"sender":{"key":"erik@cervined.in","avatar":null},"body":"On Sat, Oct 22, 2022 at 7:02 PM Sergey <sryze@protonmail.com> wrote:\n>\n> 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.\n\nOnce you know the relevant options, it's pretty trivial to roll your\nown script/workflow/configuration that suits your own work case. I'll\nshare how I solve the problem you describe. First, I'd like to mention\n  git pull --rebase\nwhich is basically\n  git fetch origin && git rebase @{upstream}\nor\n  git pull --rebase=merges\nwhich is basically\n  git fetch origin && git rebase --rebase-merges @{upstream}\nThis does a fetch and afterwards, attempts to rebase any local changes\non top of the remote tracking branch. I'd recommend this approach over\nforce pushing. It keeps forced history rewrites away from the upstream\nrepository, which is generally best reserved for \"truer\" or more\nfinished work. It also allows for better recovery, in case you mess\nup, chances are your history is preserved in the reflog. This can be\nconfigured as the default either globally, per repo or per branch\nhttps://git-scm.com/docs/git-config#Documentation/git-config.txt-pullrebase\n\nBe aware that there are potential situations where this will not\nautomagically resolve the difference in history in a way that you may\nexpect and I'd only configure this as default if you feel comfortable\nrolling back in case that situation arises. As a starting point, you\ncan manually initialize the rebase using @{upstream} or @{U}\n git fetch\n git rebase @{U}\n\nIf you want to investigate the divergence between a local branch and\nits upstream, there are several commands that may be useful depending\non exactly how you want to compare them (often situational in my\nexperience.) The see which is newer you can run\n  git show -q --format=fuller HEAD HEAD@{U}\nbut sometimes you want to know more say, how do the trees differ\n  git log --online --graph HEAD...HEAD@{U}\nor perhaps the difference in contents\n  git diff HEAD HEAD@{U}\netc. etc.\n\nHappy hunting!\n"}]}