使用git fetch或pull自动修剪

时间:2013-08-19 07:30:21

标签: git

如果某人删除了远程分支,因为工作结束而且我不知道,我将不会执行git fetch --prune,最终我将推回已删除的分支。

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

4 个答案:

答案 0 :(得分:319)

Since git 1.8.5 (Q4 2013)

  

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

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

--prune

任何git config remote.origin.prune true git fetch都会自动修剪。

注意:Git 2.12(2017年第1季度)将修复与此配置相关的错误,这会导致git pull行为异常。
请参阅“How do I rename a git remote?”。


commit 737c5a9了解详情:

  

如果没有“git remote rename”,另一方已经删除的分支的远程跟踪分支将永久保留。
  有些人希望始终运行“git fetch --prune”。

     

要容纳想要一直修剪或从特定遥控器获取的用户,请添加两个新的配置变量“git fetch --prune”和“fetch.prune”:

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

后者自然会覆盖前者,命令行中的remote.<name>.prune选项将覆盖配置的默认值。

     

由于--[no-]prune是潜在的破坏性操作(Git不保留已删除引用的reflog),我们不希望在没有用户同意的情况下进行修剪,因此默认情况下不会启用此配置。

答案 1 :(得分:89)

git config --global fetch.prune true

要在所有Git存储库中--prunegit fetch始终git pull

git config --global fetch.prune true

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

[fetch]
    prune = true

git config remote.origin.prune true

始终--prune但是来自一个单独的存储库:

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

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

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

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

git config --global gui.pruneDuringFetch true

如果您使用git gui,您可能也会感兴趣:

git config --global gui.pruneDuringFetch true

追加:

[gui]
    pruneDuringFetch = true

参考

来自git help config的相应文件:

  

--global

     

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

  

--local

     

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

  

fetch.prune

     

如果为true,则fetch将自动表现为在命令行上给出--prune选项。另请参阅remote.<name>.prune

  

gui.pruneDuringFetch

     

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

  

remote.<name>.prune

     

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

答案 2 :(得分:19)

如果您希望在prune时始终fetch,我建议您使用Aliases

只需输入git config -e即可打开您的编辑器并更改特定项目的配置,并添加类似

的部分
[alias]
pfetch = fetch --prune   

当您使用git pfetch获取时,修剪将自动完成。

答案 3 :(得分:1)

<块引用>

最终我会推回已删除的分支

这是我认为您应该解决的问题。如果您将 git 配置为推送您不尝试推送的分支,这可能是一个问题。我个人更喜欢这样设置,当我明确指定要推送的分支时,我只推送一个分支。

有关如何在 git 存储库中配置推送设置的一些指导,请参阅 https://stackoverflow.com/a/948397/3753318

相关问题