git push --force-with-lease 默认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30542491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:04:18  来源:igfitidea点击:

push --force-with-lease by default

git

提问by Dan Fabulich

I just learned about git push --force-with-lease. It's pretty awesome. But, of course, I don't use force that often, and so I'm worried that I might forget about this nifty feature the next time I need it.

我刚刚了解了git push --force-with-lease. 真是太棒了。但是,当然,我不经常使用强制,所以我担心下次我需要它时我可能会忘记这个漂亮的功能。

Is there a way to configure git so git push -fwill automatically use --force-with-leaseunless I intentionally override it with --no-force-with-lease?

有没有办法配置 git 以便git push -f自动使用,--force-with-lease除非我故意用 覆盖它--no-force-with-lease

(I can't imagine ever wanting to use force without lease!)

(我无法想象曾经想在没有租约的情况下使用武力!)

回答by Sascha Wolf

AFAIK there is no configuration available to tell git to always use force-with-leaseinstead of force. This seems to be a good example for a feature request; if you have no problem to dive into the git code base you could implement it yourself and submit it for review.

AFAIK 没有可用的配置来告诉 git 总是使用force-with-lease而不是force. 这似乎是功能请求的一个很好的例子;如果您在深入研究 git 代码库方面没有问题,则可以自己实现并提交以供审核。

EDITAs it stands, this is still true in April 2019.

编辑就目前而言,2019 年 4 月仍然如此。

Until then the only option I see is, as so often, to create an aliaswhich serves this purpose.

在那之前,我看到的唯一选择是,像往常一样,创建一个alias服务于这个目的的。

Create an alias

创建别名

To create an alias one would use git config --global alias.<alias-name> <command>, in our case I would suggest something similar to this.

要创建一个会使用的别名git config --global alias.<alias-name> <command>,在我们的例子中,我会建议类似的东西。

git config --global alias.pushf "push --force-with-lease"

This will create an entry in your global .gitconfigfile (which you can usually find in your home directory). After this you can simply use git pushfto force-with-lease.

这将在您的全局.gitconfig文件中创建一个条目(通常可以在您的主目录中找到)。在此之后,你可以简单地使用git pushf,以强制与租赁

Get your hands dirty

弄脏你的手

If you want to implement the feature yourself but aren't sure where to start, you should at first take a look at the documentationdirectory in the git repository. Here you can find the coding guidelinesand information on how to submit patches.

如果您想自己实现该功能但不确定从哪里开始,您应该首先查看git 存储库中的文档目录。您可以在此处找到有关如何提交补丁编码指南和信息。

You can find all these links and more on the official community page.

您可以在官方社区页面上找到所有这些链接和更多内容。

回答by VonC

I'm worried that I might forget about this nifty feature the next time I need it.

我担心下次需要时可能会忘记这个漂亮的功能。

Git 2.13 (Q2 2017) explains why there is no "protection" against this push option being forgotten, because even if you do notforget it at the git pushlevel, it might still be ignored.

Git 2.13(2017 年第二季度)解释了为什么没有“保护”来防止忘记这个推送选项,因为即使你没有git push级别忘记它,它仍然可能被忽略。

See commit f17d642(19 Apr 2017) by ?var Arnfj?re Bjarmason (avar).
(Merged by Junio C Hamano -- gitster--in commit 46bdfa3, 26 Apr 2017)

请参阅?var Arnfj?re Bjarmason ( ) 的提交 f17d642(2017 年 4 月 19 日(由Junio C Hamano合并-- --2017 年 4 月 26 日提交 46bdfa3 中avar
gitster

push: document & test --force-with-leasewith multiple remotes

Document & test for cases where there are two remotes pointing to the same URL, and a background fetch & subsequent git push --force-with-leaseshouldn't clobber un-updated references we haven't fetched.

Some editors like Microsoft's VSC have a feature to auto-fetch in the background, this bypasses the protections offered by --force-with-lease& --force-with-lease=<refname>, as noted in the documentation being added here.

push--force-with-lease使用多个遥控器记录和测试

记录和测试有两个遥控器指向同一个 URL 的情况,并且后台获取和后续git push --force-with-lease不应该破坏我们尚未获取的未更新引用。

一些编辑器(如 Microsoft 的 VSC)具有在后台自动获取的功能,这绕过了--force-with-lease&提供的保护--force-with-lease=<refname>,如此处添加的文档中所述。

So the documentation for git pushnow includes:

所以现在的文档git push包括:

general note on safety: supplying this option without an expected value, i.e. as --force-with-leaseor --force-with-lease=<refname>interacts very badly with anything that implicitly runs git fetchon the remote to be pushed to in the background, e.g. git fetch originon your repository in a cronjob.

The protection it offers over --forceis ensuring that subsequent changes your work wasn't based on aren't clobbered, but this is trivially defeated if some background process is updating refs in the background. We don't have anything except the remote tracking info to go by as a heuristic for refs you're expected to have seen & are willing to clobber.

If your editor or some other system is running git fetchin the background for you a way to mitigate this is to simply set up another remote:

关于安全的一般说明:在没有预期值的情况下提供此选项,即作为--force-with-lease--force-with-lease=<refname>与任何隐式运行git fetch在远程上以在后台推送的东西交互非常糟糕,例如git fetch origin在 cronjob 中的存储库中。

它提供的保护--force是确保您的工作不是基于的后续更改不会受到破坏,但是如果某些后台进程正在后台更新 refs,这将被轻而易举地击败。除了远程跟踪信息之外,我们没有任何东西可以作为您希望看到并愿意破坏的参考的启发式方法。

如果您的编辑器或其他一些系统git fetch在后台为您运行,一种缓解这种情况的方法是简单地设置另一个远程:

git remote add origin-push $(git config remote.origin.url)
git fetch origin-push

Now when the background process runs git fetch originthe references on origin-pushwon't be updated, and thus commands like:

现在,当后台进程运行时git fetch origin,引用origin-push不会更新,因此命令如下:

git push --force-with-lease origin-push

Will fail unless you manually run git fetch origin-push.
This method is of course entirely defeated by something that runs git fetch --all, in that case you'd need to either disable it or do something more tedious like:

git fetch              # update 'master' from remote
git tag base master    # mark our base point
git rebase -i master   # rewrite some commits
git push --force-with-lease=master:base master:master

I.e. create a basetag for versions of the upstream code that you've seen and are willing to overwrite, then rewrite history, and finally force push changes to masterif the remote version is still at base, regardless of what your local remotes/origin/masterhas been updated to in the background.

除非您手动运行,否则将失败git fetch origin-push
这种方法当然完全被运行的东西打败了git fetch --all,在这种情况下,你需要禁用它或做一些更乏味的事情,比如:

git fetch              # update 'master' from remote
git tag base master    # mark our base point
git rebase -i master   # rewrite some commits
git push --force-with-lease=master:base master:master

base即为您已经看到并愿意覆盖的上游代码版本创建一个标签,然后重写历史记录,最后master如果远程版本仍然在base,则强制推送更改,无论您的本地remotes/origin/master已更新为背景。

回答by Jessica Knight

My solution was to create a wrapper script, and use an alias so that I always use it in place of the real git.

我的解决方案是创建一个包装脚本,并使用别名,以便我始终使用它代替真正的git.

Whenever I try to git push -f, I see the following:

每当我尝试时git push -f,我都会看到以下内容:

? git push -f
use this instead so you don't cause race conditions in the 
repo: git push --force-with-lease

Some advantages of this script are:

此脚本的一些优点是:

  • it trains me to habitually use --force-with-lease, so i don't get nagged when i get it wrong
  • if, for some reason, we really do need to force push, git push --forcewill work.
  • 它训练我习惯性地使用--force-with-lease,所以当我弄错时我不会被唠叨
  • 如果出于某种原因,我们确实需要强制推送,git push --force将会起作用。

How to implement it:

如何实施:

  1. create a custom script that will pass through any params to git, except for -f
  2. alias that script so we use it instead of git
  1. 创建一个自定义脚本,它将通过任何参数传递给 git,除了 -f
  2. 为该脚本取别名,以便我们使用它而不是 git

These instructions assume Linux or Mac, running bash. I haven't tried this with zsh or Windows, but I assume it'll work there too.

这些说明假设 Linux 或 Mac 运行 bash。我还没有在 zsh 或 Windows 上尝试过这个,但我认为它也可以在那里工作。

~/.bash_profile:

~/.bash_profile

alias git=~/.git_wrapper.sh

~./git_wrapper.sh:

~./git_wrapper.sh

#!/bin/bash
for arg in "$@"; do
    if [ "$arg" = "push" ]; then
        ispush=1
    elif [ "$ispush" = 1 -a "$arg" = '-f' ]; then
        echo "hey idiot, use this instead so you don't cause race conflicts in the repo: git push --force-with-lease"
        exit 1
    fi
done

git "$@"

With those changes, restart your terminal and gitshould now get uppity when you try to force push.

进行这些更改后,重新启动您的终端,git现在当您尝试强制推送时应该会感到骄傲。

回答by Nicolas

For people using OMYZSH you can simply use ggfl.

对于使用 OMYZSH 的人,您可以简单地使用ggfl.

回答by neu242

I want to be reminded that I shouldn't use -f, but I don't want to be fooled into believing that -fmeans --force-with-lease. So this is my take:

我想被提醒我不应该使用-f,但我不想被愚弄相信这-f意味着--force-with-lease。所以这是我的看法:

git() {
  if [[ $@ == 'push -f'* ]]; then
    echo Hey stupid, use --force-with-lease instead
  else
    command git "$@"
  fi
}

Add to your .bash_profile, .bashrcor .zshrc.

添加到您的.bash_profile,.bashrc.zshrc.

回答by paulodiovani

You can create a bash function that replaces gitand use --force-with-leaseinstead of --force

您可以创建一个 bash 函数来替换git和使用--force-with-lease而不是--force

# replaces `git push --force` with `git push --force-with-lease`
git() {
  if [[ $@ == 'push -f'* || $@ == 'push --force'* ]]; then
    command git push --force-with-lease
  else
    command git "$@"
  fi
}

or, in one line:

或者,在一行中:

git() { if [[ $@ == 'push -f'* || $@ == 'push --force'* ]]; then command git push --force-with-lease; else command git "$@"; fi; }

Just add it to your ~/.bashrcor ~/.zshrc.

只需将其添加到您的~/.bashrc~/.zshrc.