9

I have a git repository with long and strange history. I don't know what the developers did with this repository and cannot control what they are doing with it now.

But I need to clone this repository (for redmine integration) and fetch all changes periodically.

What do I do:

git clone --bare git@git.server.com:/opt/git/repo
cd repo.git
git log

Now I can see all commits. Fine.

Next a developer make a commit in the main repository and I want to fetch all changes (all brances, tags and so on, and so on):

> git fetch --all 
Fetching origin
remote: Counting objects: 18, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 14 (delta 5), reused 0 (delta 0)
Unpacking objects: 100% (14/14), done.
From git.gmcs.ru:/opt/git/ecco
 * branch            HEAD       -> FETCH_HEAD

But if a ask the commit history I didn't see that last commit which was made in the main repository. Why ?

If I post not enough information I am ready to give you all the needed.

Thanks in advance.

Updated

Here is a brach information in the original repsitory:

git branch -a
  one
  test
* master
  release

Here is a branch information in the cloned repository:

git branch -a
  one
  test
* master
  release

I can see last commits in the master branch of original repository, but can not find them in the master branch of cloned repository.

ceth
  • 44,198
  • 62
  • 180
  • 289
  • 1
    `fatal: /usr/lib/git/git-pull` cannot be used without a working tree. I think it is because it is a bare repository. – ceth Nov 19 '12 at 12:21

4 Answers4

16

To fetch more updates into a bare repo, I do:

git config remote.origin.fetch 'refs/heads/*:refs/heads/*'

Then I can do:

git fetch
Gregor
  • 1,205
  • 11
  • 20
  • 3
    This is the best answer. All the other answers to the problem of fetching *into* a repo created by `git clone --bare` do not fix it for me. The symbolic-ref solution is not great because it is necessary to do that for each branch each time, not desirable. – Colin D Bennett Jul 24 '13 at 20:39
  • 3
    If you use `git fetch -p` it will additionally prune branches that have been removed from origin. – cdyson37 Dec 18 '14 at 10:16
7

You should use git pull

or run git merge after fetch to get fetched changes

if you have a bare repository you can not do a pull, because a pull wants to merge with HEAD, which a bare repo does not have.

to update bare repository you can add it as remote to non-bare repository and push to it.

But I think --mirror instead of --bare will work for you as is.

Compared to --bare, --mirror not only maps local branches of the source to local branches of the target, it maps all refs (including remote branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a git remote update in the target repository.

and then you can use git remote update to update mirrored repository

cnd
  • 32,616
  • 62
  • 183
  • 313
  • 3
    `fatal: /usr/lib/git/git-pull` cannot be used without a working tree. I think it is because it is a bare repository. – ceth Nov 19 '12 at 12:25
  • I'm sorry, could be, do you need to have bare repository? – cnd Nov 19 '12 at 12:45
  • 1
    Yes, as I see the Redmine require a bare repository. Moreover, this method works fine with all other repositories, bit I can not figure out what is wrong with this repo. – ceth Nov 19 '12 at 12:47
1

To fetch into your bare repository regulary configure first

git config remote.origin.fetch "+*:*"

and then simply run

git fetch --prune

to fetch all changes, including tags, new branches and even branch deletions (option --prune).

  • 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.
  • Note: Tag deletions are not fetched using this configuration.

See also https://stackoverflow.com/a/33461528/4138912.

Community
  • 1
  • 1
doak
  • 809
  • 9
  • 24
0

You can update the HEAD of your bare repository with git symbolic-ref, e.g. to point it to branch master from remote origin

$ git symbolic-ref HEAD refs/remotes/origin/master

If you use git fetch to update remotes it will update e.g. origin/master. All that was missing was to repoint the clone's HEAD.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80