I created a bare repo to publish my repository, but I can't figure out how to update the bare repo with the current state of the main repository.
-
3See this answer: stackoverflow.com/questions/315911/…– Ken BloomAug 1, 2010 at 16:04
7 Answers
If you want to duplicate all the objects from the main repo, do this inside the main repo:
git push --all <url-of-bare-repo>
Alternatively, do a fetch inside the bare repo:
git fetch <url-of-main-repo>
You cannot do a pull, because a pull wants to merge with HEAD
, which a bare repo does not have.
You can add these as remotes to save yourself some typing in the future:
git remote add <whatever-name> <url-of-other-repo>
Then you can simply do
git push --all <whatever-name>
or
git fetch <whatever-name>
depending on what repo you're in. If <whatever-name>
is origin
, you can even leave it out altogether.
Disclaimer: I'm not a git guru. If I said something wrong, I'd like to be enlightened!
Update: Read the comments!
-
9When I did a
git-fetch --all
inside the bare repository, I didn't see updates I had made to the main repo, but when pushing them from the main repo withgit push --all <url-of-bare-repo>
I do see the updates ingit log
. Presumably there's a simple explanation for this - can someone explain?– pho79Mar 1, 2012 at 20:47 -
2@Thomas - Yeah, by "didn't see" I mean
git log
doesn't show these updates in the bare repo. (Neither doesgit log --all
, and neither does a working repo that created by cloning the bare repo - either viagit log --all
or by simply looking at new files that should show up there). It's a pretty quick test to see for yourself. Mostly I'm just curious what I'm missing.– pho79Mar 15, 2012 at 23:12 -
80If your remotest repo is something like github, where you don't have access, can't run push, etc, you can do
git fetch -q origin master:master
inside your local bare repo. This will fetch the new stuff from github's master branch and update your local master branch to it.– AltreusJul 20, 2012 at 11:39 -
5@Altreus Indeed
master:master
is what is required to move HEAD forward to that of the remote repo. In my case because of a problem I couldn't connect to our bare repo anymore because git was missing. Until it gets solved I do a reverse tunnel and fetch to the central repo using:git fetch ssh://localhost:8765/... master:master
and it works like a charm. Thanks!– estaniJan 22, 2013 at 10:49 -
9
I created a repository using the following command
git clone --bare <remote_repo>
Then I tried to update the bare clone using the answer by Thomas, but it didn't work for me. To get the bare repository to update (which is what I think Let_Me_Be was asking), I had to create a mirror repository:
git clone --mirror <remote_repo>
Then I could run the following command in the mirrored repository to grab the main repository's updates:
git fetch --all
I came across this solution by reading Mirror a Git Repository By Pulling
-
25To change an existing bare repo to mirror, all you need to do is add 2 lines to the git config file at <REPO>.git/config. In the
[remote "origin"]
section, addfetch = +refs/*:refs/*
andmirror = true
Aug 30, 2013 at 14:39
The only solution besides recreating with git clone --mirror
is from Gregor:
git config remote.origin.fetch 'refs/heads/*:refs/heads/*'
then you can git fetch
and you'll see the updates. The weird thing is that before this, even though there is a remote
configured, it has no branches listed in git branch -a
.
-
1This really helped me out! Capistrano project/repo also uses this setting to allow one-liner
git remote update
to do the job. Jul 14, 2014 at 9:30 -
1If the remote has force-updated or deleted branches (and you want to reflect those changes), you may need to add
--force
and--prune
respectively to thegit fetch
line. Aug 6, 2015 at 6:04
Assuming:
$ git clone --bare https://github.com/.../foo.git
Fetch with:
$ git --git-dir=foo.git fetch origin +refs/heads/*:refs/heads/* --prune
Note: --git-dir=foo.git
is not required if you cd
to the directory first.
-
2Although I'm not aware when that occurs, if
origin
is not defined, you can always replace theorigin
part with the path/url to your original repository. e.g.$ git --git-dir=foo.git fetch https://github.com/.../foo.git +refs/heads/*:refs/heads/*
– antakJan 21, 2016 at 2:38 -
This is exactly what I was looking for. This worked perfectly. Jun 25, 2019 at 17:58
After much messing around I've found that this works for me.
Once:
git clone --mirror ssh://git@source.address:2000/repo
git remote add remote_site ssh://git@remote_site.address/repo
git config remote.origin.fetch 'refs/heads/*:refs/heads/*'
Everytime I want to sync:
cd /home/myhome/repo.git
git --bare fetch ssh://git@source.address:2000/repo
git fetch ssh://git@source.address:2000/repo
git push --mirror remote_site
-
7The third line (
git config remote.origin.fetch 'refs/heads/*:refs/heads/*'
) is the key. Good answer, thanks!– peterhMar 27, 2017 at 8:06 -
2As @peterh said, the line
git config remote.origin.fetch 'refs/heads/*:refs/heads/*'
is the answer. After hitting this command, I can justgit fetch
and the repo syncs with the remote one.– jokerJun 21, 2018 at 9:51
Add the bare repository as a remote repository, then use git push
.
-
So if I want to do this without pushing I can't use a bare repository and use a symbolic link or something like that? Aug 1, 2010 at 16:07
-
Pushing is the normal method for transferring repository contents to a bare repository, there is no need to avoid it.– PhilippAug 1, 2010 at 16:22
-
4You can't always push. For example, if the bare is in a private network, and the main repos is a public one (on GitHub, for exemple)– fanf42May 6, 2013 at 17:12
For me this combination worked:
git remote add remote_site https://github.com/project/repo.git
git fetch -u remote_site +refs/heads/*:refs/heads/*
git fetch -u remote_site +refs/tags/*:refs/tags/*
git push --mirror
-u parameter for fetch was necessary, otherwise I get the error message: "Refusing to fetch into current branch refs/heads/master of non-bare repository" (see also https://stackoverflow.com/a/19205680/4807875)