If someone deleted a remote branch because the work is over and I don\'t know, I won\'t do a git fetch --prune and eventually I will push back the deleted branc
Since git 1.8.5 (Q4 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.
That means that, if you set remote.origin.prune to true:
git config remote.origin.prune true
Any git fetch or git pull will automatically prune.
Note: Git 2.12 (Q1 2017) will fix a bug related to this configuration, which would make git remote rename misbehave.
See "How do I rename a git remote?".
See more at commit 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.
If you want to always prune when you fetch, I can suggest to use Aliases.
Just type git config -e to open your editor and change the configuration for a specific project and add a section like
[alias]
pfetch = fetch --prune
the when you fetch with git pfetch the prune will be done automatically.
git config --global fetch.prune trueTo always --prune for git fetch and git pull in all your Git repositories:
git config --global fetch.prune true
This above command appends in your global Git configuration (typically ~/.gitconfig) the following lines. Use git config -e --global to view your global configuration.
[fetch]
prune = true
git config remote.origin.prune trueTo always --prune but from one single repository:
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 -e to view your local configuration.
[remote "origin"]
url = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
fetch = +refs/heads/*:refs/remotes/origin/*
prune = true
You can also use --global within the second command or use instead --local within the first command.
git config --global gui.pruneDuringFetch trueIf you use git gui you may also be interested by:
git config --global gui.pruneDuringFetch true
that appends:
[gui]
pruneDuringFetch = true
The corresponding documentations from git help config:
--globalFor 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.
--localFor writing options: write to the repository
.git/configfile. This is the default behavior.
fetch.pruneIf true, fetch will automatically behave as if the
--pruneoption was given on the command line. See alsoremote.<name>.prune.
gui.pruneDuringFetch"true" if git-gui should prune remote-tracking branches when performing a fetch. The default value is "false".
remote.<name>.pruneWhen 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). Overridesfetch.prunesettings, if any.