23

I'm writing a tool to backup all my repositories from Bitbucket (which supports Git and Mercurial) to my local machine.

It already works for Mercurial, where I do it like this:

  • create a new empty repository without a working copy on the local machine
    (the same like a bare Git repository)
  • pull from the remote repository into the local empty repository

Now I'm trying to do the same with Git.

I already found out that I can't directly pull to a bare repository and that I should use fetch instead.

So I tried it:

C:\test>git fetch https://github.com/SamSaffron/dapper-dot-net.git
remote: Counting objects: 1255, done.
remote: Compressing objects: 100% (1178/1178), done.
remote: Total 1255 (delta 593), reused 717 (delta 56)
Receiving objects: 100% (1255/1255), 13.66 MiB | 706 KiB/s, done.
Resolving deltas: 100% (593/593), done.
From https://github.com/SamSaffron/dapper-dot-net
 * branch            HEAD       -> FETCH_HEAD

Obviously Git did fetch something, but the local repository is empty after that.
(git log says fatal: bad default revision 'HEAD')

What am I doing wrong?

Disclaimer:
I have only very, very basic Git knowledge (I usually use Mercurial).
And I'm using Windows, if that matters.

Community
  • 1
  • 1
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • possible duplicate of [git log and show on a bare repo](http://stackoverflow.com/questions/6214711/git-log-and-show-on-a-bare-repo) – CharlesB Oct 25 '11 at 21:02
  • @CharlesB: None of the answers in this link work for me. Not even things like `git branch -va` that worked for the asker, not the suggested `git log branchname` (I tried `master`), nor the "To visualize everything in the repository..." command at the end of the answer. – Christian Specht Oct 25 '11 at 21:30

4 Answers4

20

Try

git fetch https://github.com/SamSaffron/dapper-dot-net.git master:master
Kyle Strand
  • 15,941
  • 8
  • 72
  • 167
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
16

To backup the remote repository into your bare repository regulary configure first

git config remote.origin.url https://github.com/SamSaffron/dapper-dot-net.git
git config remote.origin.fetch "+*:*"

and then simply run

git fetch --prune

to backup.

  • You probably can skip the fist configuration addition as this should has been already set while cloning the remote repository.
  • Please also mind the enclosing double quotation marks (") in the above command to protect the asterix (*) not to be interpreted from your shell.
  • The plus sign is needed to allow non-fastforward updates. That is probably your intention if you want to backup the current state of your remote.
  • Option --prune is used to also delete by now non-existent branches.
starfry
  • 9,273
  • 7
  • 66
  • 96
doak
  • 809
  • 9
  • 24
  • 3
    A good explanation with all the pertinent facts in one place, especially useful is the point about double-quotes and the need for the `+` prefixing the [refspec](https://git-scm.com/book/en/v2/Git-Internals-The-Refspec). – starfry May 16 '16 at 12:09
4

I think you if you really want to backup. You can try $ git clone --mirror XXXX command. it will get almost everything from repository. Hope it is helpful.

Enze Chi
  • 1,733
  • 17
  • 28
  • 2
    Yes, but I want the backup to run regularly. So after the first run, the local repository already exists --> I have to run `pull`/`fetch`/whatever anyway and make sure that this pulls **everything** as well. – Christian Specht Oct 26 '11 at 05:40
  • 1
    After you mirrored you bare. you can use 'git fetch --all --progress -v' to update your local bares. – Enze Chi Oct 26 '11 at 22:06
2
$ git fetch https://github.com/SamSaffron/dapper-dot-net.git +refs/heads/*:refs/heads/* --prune
antak
  • 19,481
  • 9
  • 72
  • 80