-1

I have setup a central backup server which has all the developer repos added as remotes. I fetch everyday from all the remotes. It gives me all the branches of developers as dev1/master, dev1/FeatureA, dev2/master, dev2/Hotfix etc. I do this so that I have backup of user repos in case a local system crashes. Things are all fine.

The problem that I face is: When say dev1's Repo has 2 branches Br1 and Br2 that are tracked on backup via dev1/Br1 and dev1.Br2 respectively. Now the developer deletes one of the branches, say Br1. I do a fetch at EOD on backup server. The backup server still shows that dev1 has Br1 branch.

How can I make fetch replace all existing branches with the new ones, i.e. also delete dev1/Br1 if the remote (dev1) repo deletes the branch?

Palec
  • 12,743
  • 8
  • 69
  • 138
Mudassir Razvi
  • 1,783
  • 12
  • 33
  • I assume you're using `fetch =` lines to do the repo branch name mappings. Just add `--prune` to your `git remote update` or `git fetch`. Note that some older git versions occasionally fail to prune with some commands, so if one doesn't work, try the other, or update your git version. – torek Jun 23 '14 at 10:36
  • also an interesting attempt: http://stackoverflow.com/questions/2129214/backup-a-local-git-repository – user3415653 Jun 23 '14 at 10:39
  • @user3415653 bundles are out of option. I am having a backup of 100+ users and bundling each and unbundling is just too costly. – Mudassir Razvi Jun 23 '14 at 12:40
  • 1
    @MudassirRazvi then use a enterprise solution like stash (https://www.atlassian.com/software/stash) – user3415653 Jun 23 '14 at 12:54
  • @user3415653: That is also out of option. Palec's answer below works awesome. Could you suggest a way I can prune tags also along with branches. That would be helpful... :) – Mudassir Razvi Jun 23 '14 at 13:00
  • @MudassirRazvi i think `git fetch --prune --tags` – user3415653 Jun 23 '14 at 13:10
  • git fetch --prune --tags for some reason deletes all remote tracking branches. never use it..:p – Mudassir Razvi Jun 23 '14 at 13:16
  • 1
    @MudassirRazvi You should really consider using stash or a similar product. – Zarathustra Jun 23 '14 at 13:28

1 Answers1

2

You need something like:

git fetch --all --prune

From the manual:

--all
    Fetch all remotes.

-p, --prune
    After fetching, remove any remote tracking branches which no longer
    exist on the remote.
Palec
  • 12,743
  • 8
  • 69
  • 138
  • Thank you! Works like a charm. But is there a way that Tags can also be pruned? Coz this way it removes branches perfectly but tags that are deleted in remote still stay – Mudassir Razvi Jun 23 '14 at 13:01
  • @MudassirRazvi `git fetch --all --prune --tags` that simple. – Zarathustra Jun 23 '14 at 13:26
  • @Zarathustra Agreed. See http://stackoverflow.com/q/10491146/2157640 and http://stackoverflow.com/q/1841341/2157640 – Palec Jun 23 '14 at 13:28
  • @MudassirRazvi If adding `--tags` does not work, you can call `git tag -l | xargs git tag -d` before the fetch as a work-around. This deletes all tags in the repository and they will be fetched from scratch. – Palec Jun 23 '14 at 13:51