使用 Git fetch 或 pull 自动修剪

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18308535/
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-10 16:47:20  来源:igfitidea点击:

Automatic prune with Git fetch or pull

git

提问by Edmondo1984

If someone deleted a remote branch because the work is over and I don't know, I won't do a git fetch --pruneand eventually I will push back the deleted branch.

如果有人因为工作结束而删除了远程分支而我不知道,我不会这样做git fetch --prune,最终我会推回已删除的分支。

Is there a viable solution for forcing Git to use the prune mode when fetching / pulling without having to specify it every time?

是否有可行的解决方案来强制 Git 在获取/拉取时使用修剪模式而不必每次都指定它?

回答by VonC

Since git 1.8.5 (Q4 2013):

从 git 1.8.5(2013 年第四季度)开始

"git fetch" (hence "git pull" as well) learned to check "fetch.prune" and "remote.*.prune" configuration variables and to behave as if the "--prune" command line option was given.

git fetch”(因此也是“ git pull”)学会了检查“ fetch.prune”和“ remote.*.prune”配置变量,并且表现得好像给出了“ --prune”命令行选项。

That means that, if you set remote.origin.prune to true:

这意味着,如果您将 remote.origin.prune 设置为 true:

git config remote.origin.prune true

Any git fetchor git pullwill automatically prune.

Any git fetchorgit pull将自动修剪。

Note: Git 2.12 (Q1 2017) will fix a bug related to this configuration, which would make git remote renamemisbehave.
See "How do I rename a git remote?".

注意:Git 2.12(2017 年第一季度)将修复与此配置相关的错误,这会导致git remote rename行为不端。
请参阅“如何重命名 git 遥控器?”。



See more at commit 737c5a9:

提交 737c5a9处查看更多信息

Without "git fetch --prune", remote-tracking branches for a branch the other side already has removed will stay forever.
Some people want to always run "git fetch --prune".

To accommodate users who want to either prune always or when fetching from a particular remote, add two new configuration variables "fetch.prune" and "remote.<name>.prune":

  • "fetch.prune" allows to enable prune for all fetch operations.
  • "remote.<name>.prune" allows to change the behaviour per remote.

The latter will naturally override the former, and the --[no-]pruneoption from the command line will override the configured default.

Since --pruneis a potentially destructive operation (Git doesn't keep reflogs for deleted references yet), we don't want to prune without users consent, so this configuration will not be on by default.

如果没有“ git fetch --prune”,对方已经删除的分支的远程跟踪分支将永远保留。
有些人想一直跑“ git fetch --prune”。

为了适应想要始终修剪或从特定远程获取时修剪的用户,添加两个新的配置变量“ fetch.prune”和“ remote.<name>.prune”:

  • " fetch.prune" 允许为所有获取操作启用修剪。
  • " remote.<name>.prune" 允许更改每个遥控器的行为。

后者自然会覆盖前者,--[no-]prune命令行中的选项将覆盖配置的默认值。

由于这--prune是一个潜在的破坏性操作(Git 尚未保留已删除引用的引用日志),我们不想在未经用户同意的情况下进行修剪,因此默认情况下不会启用此配置。

回答by olibre

git config --global fetch.prune true

git config --global fetch.prune true

To always --prunefor git fetchand git pullin all your Git repositories:

要始终--prunegit fetchgit pull所有Git仓库的位置:

git config --global fetch.prune true

This above command appends in your global Git configuration (typically ~/.gitconfig) the following lines. Use git config -e --globalto view your global configuration.

上面的命令会在您的全局 Git 配置(通常~/.gitconfig)中附加以下几行。使用git config -e --global查看您的全局配置。

[fetch]
    prune = true


git config remote.origin.prune true

git config remote.origin.prune true

To always --prunebut from one single repository:

总是--prune来自一个单一的存储库:

git config remote.origin.prune true
                 #^^^^^^
                 #replace with your repo name

This above command adds in your local Git configuration (typically .git/config) the below last line. Use git config -eto view your local configuration.

上面的命令在您的本地 Git 配置(通常.git/config)中添加了下面的最后一行。使用git config -e查看您的本地配置。

[remote "origin"]
    url = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    fetch = +refs/heads/*:refs/remotes/origin/*
    prune = true

You can also use --globalwithin the second command or use instead --localwithin the first command.

您也可以--global在第二个命令中使用或--local在第一个命令中使用。



git config --global gui.pruneDuringFetch true

git config --global gui.pruneDuringFetch true

If you use git guiyou may also be interested by:

如果您使用,git gui您可能还对以下内容感兴趣:

git config --global gui.pruneDuringFetch true

that appends:

附加:

[gui]
    pruneDuringFetch = true


References

参考

The corresponding documentations from git help config:

相应的文件来自git help config

--global

  For writing options: write to global ~/.gitconfigfile rather than the repository .git/config, write to $XDG_CONFIG_HOME/git/configfile if this file exists and the ~/.gitconfigfile doesn't.

--global

  对于写入选项:写入全局~/.gitconfig文件而不是存储库.git/config$XDG_CONFIG_HOME/git/config如果此文件存在而~/.gitconfig文件不存在,则写入文件。

 

 

--local

  For writing options: write to the repository .git/configfile. This is the default behavior.

--local

  对于写入选项:写入存储库.git/config文件。这是默认行为。

 

 

fetch.prune

  If true, fetch will automatically behave as if the --pruneoption was given on the command line. See also remote.<name>.prune.

fetch.prune

  如果为 true,则 fetch 将自动表现得好像该--prune选项是在命令行上给出的。另见remote.<name>.prune

 

 

gui.pruneDuringFetch

  "true" if git-guishould prune remote-tracking branches when performing a fetch. The default value is "false".

gui.pruneDuringFetch

  如果git-gui在执行提取时应该修剪远程跟踪分支,则为“true” 。默认值为“假”。

 

 

remote.<name>.prune

  When set to true, fetching from this remote by default will also remove any remote-tracking references that no longer exist on the remote (as if the --pruneoption was given on the command line). Overrides fetch.prunesettings, if any.

remote.<name>.prune

  当设置为 true 时,默认情况下从此远程获取数据还将删除远程上不再存在的任何远程跟踪引用(就像--prune在命令行上给出了该选项一样)。覆盖fetch.prune设置(如果有)。

回答by ThanksForAllTheFish

If you want to always prunewhen you fetch, I can suggest to use Aliases.

如果你想永远prune当你fetch,我可以建议使用别名

Just type git config -eto open your editor and change the configuration for a specific project and add a section like

只需键入git config -e以打开您的编辑器并更改特定项目的配置并添加一个部分,如

[alias]
pfetch = fetch --prune   

the when you fetch with git pfetchthe prune will be done automatically.

当您使用git pfetch修剪进行提取时将自动完成。