Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pull/fetch with Git *INTO* a bare repository?

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.

like image 614
Christian Specht Avatar asked Oct 25 '11 20:10

Christian Specht


People also ask

How do you pull a bare repository?

refs/heads/xyz on the other hand explicitly represents a branch. So even though you can type git fetch origin foo:bar to grab their foo branch as named bar in your repository, you can more explicitly type git fetch origin refs/heads/foo:refs/heads/bar to do the same thing.

How do you git fetch and pull?

Git Fetch is the command that tells the local repository that there are changes available in the remote repository without bringing the changes into the local repository. Git Pull on the other hand brings the copy of the remote directory changes into the local repository.

How do I pull a remote branch to a local repository?

Use git branch -a (both local and remote branches) or git branch -r (only remote branches) to see all the remotes and their branches. You can then do a git checkout -t remotes/repo/branch to the remote and create a local branch. There is also a git-ls-remote command to see all the refs and tags for that remote.


3 Answers

Try

git fetch https://github.com/SamSaffron/dapper-dot-net.git master:master
like image 92
Michael Krelin - hacker Avatar answered Nov 03 '22 11:11

Michael Krelin - hacker


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.
like image 43
doak Avatar answered Nov 03 '22 11:11

doak


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.

like image 39
Enze Chi Avatar answered Nov 03 '22 11:11

Enze Chi