Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7951077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:27:12+00:00 2026-06-04T02:27:12+00:00

How to clone a repo (with libgit2 ) I want to do exactly what

  • 0

How to clone a repo (with libgit2)

I want to do exactly what git clone does but with libgit2. What I may be asking is what git clone really does in depth.

This is what I’m doing so far:

  1. Initialize a repo
  2. Adjust the config file to add the remote
  3. Create a git_remote
  4. Download a packfile
  5. Index the packfile and write the index (gives us a .idx file)
  6. (edit) Write all the different branches to disk.
  7. (edit) Do git checkout in some way.

And now I have no idea what to do. My only guess would be to load the .idx to a git_index and use git_repository_set_index, but that didn’t show any files either.

Edit

I tested running git checkout master on the half-cloned repo, and that did the job. Now I just need to find out how to do it with libgit2, and it seems like there is some useful information in the issue tracker.

Edit 2

I will now add my current code, in hope that someone someday will find it useful, in hope to be that quick-start code I never found. Note: I’m using Obj-C and Objective-Git here, but it’s mainly plain c.

+ (GTRepository *)cloneFromRemoteURL:(NSURL *)remoteURL toLocalURL:(NSURL *)localURL
{   
// Let's suppose the URL looks like: https://github.com/libgit2/libgit2.git
// Then we need to get a URL like this too: git://github.com/libgit2/libgit2.git
// This may be a bit dodgy, but it will do for now.
const char *gitURL = [remoteURL.absoluteString stringByReplacingOccurrencesOfString:@"https://github.com/" withString:@"git://github.com/"].UTF8String;

//Setup
int error;
git_repository *repo
git_config *cfg;
git_remote *remote;

NSURL *gitDirURL = [localURL URLByAppendingPathComponent:@".git"];

error = git_repository_open(&repo, gitDirURL.path.UTF8String);
    if (error != GIT_SUCCESS) {

    git_repository_init(&repo, gitDirURL.path.UTF8String, 1);

    //Config
    git_repository_config(&cfg, repo);
    git_config_set_int32 (cfg, "core.repositoryformatversion", 0);
    git_config_set_bool (cfg, "core.filemode", 1);
    git_config_set_bool (cfg, "core.bare", 0);
    git_config_set_bool (cfg, "core.logallrefupdates", 1);
    git_config_set_bool (cfg, "core.ignorecase", 1);
    git_config_set_string (cfg, "remote.origin.url", gitURL);
    git_config_set_string (cfg, "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*");
    git_config_set_string (cfg, "branch.master.remote", "origin");
    git_config_set_string (cfg, "branch.master.merge", "refs/heads/master");

    git_repository_set_workdir(repo, localURL.path.UTF8String);

    error = git_remote_new(&remote, repo, "A remote", gitURL, "origin");

    git_repository_free(repo);
    git_repository_open (&repo, localURL.path.UTF8String);
}



git_repository_config(&cfg, repo);

// connect to repo
error = git_remote_load(&remote, repo, "origin");

error = git_remote_connect(remote, GIT_DIR_FETCH);
// get pack file

git_off_t bytes;
git_indexer_stats stats;
error = git_remote_download(remote, &bytes, &stats);

NSURL *packFolderURL = [localURL URLByAppendingPathComponent:@".git/objects/pack"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *array = [fileManager contentsOfDirectoryAtURL:packFolderURL includingPropertiesForKeys:nil options:0 error:nil];
NSLog(@"Dictionary:%@",array);
NSString *result;
for (NSURL *url in array) {
    if ([url.path rangeOfString:@".pack"].location != NSNotFound) {
        result = url.path;
    }
}
const char *packname = [result UTF8String];


// unpack pack file
if (packname != NULL) 
{
    git_indexer *indexer;
    git_indexer_stats stats2;
    int error;
    char hash[GIT_OID_HEXSZ + 1] = {0};

    error = git_indexer_new(&indexer, packname);
    error = git_indexer_run(indexer, &stats2);
    error = git_indexer_write(indexer);

    // Get the packfile's hash (which should become it's filename)
    git_oid_fmt(hash, git_indexer_hash(indexer));

    NSString *hashStr = [NSString stringWithCString:hash encoding:NSUTF8StringEncoding];
    hashStr = [NSString stringWithFormat:@"pack-%@.idx",hashStr];
    const char *indexPath = [hashStr UTF8String];

    puts(hash);
    git_index *index;
    git_index_open(&index, indexPath);
    git_index_read(index);
    git_repository_set_index(repo, index);


    git_indexer_free(indexer);
    git_remote_update_tips(remote, update_cb2); //No idea what it does, but it seems like it's important… It does make the branches appear in .git/refs/remotes/origin

}

//Somehow do git checkout master here

return [GTRepository repositoryWithURL:localURL error:nil];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:27:14+00:00Added an answer on June 4, 2026 at 2:27 am

    Since the libgit2 doesn’t explicitly mention git clone in its issue list, one lead to follow is in the sources of the original git project with:

    • builtin/clone.c
    • contrib/examples/git-clone.sh

    That last script will guide you through all the stages of a git clone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a git clone/repo on a development server, but I am now moving
I am trying to clone a repo from an SSH remote like this: git
I've got a git-svn clone of an svn repo, and I want to encourage
Sometimes I clone git repo not for a development - but just for the
i have strange behaviour of git - push is working, but clone is not
Basic question about the way git works. I clone a repo, make some changes,
I'm using Git now from my Windows machine. I clone the repo, do some
Is there a way to create a new clone of a git repo using
Assuming that project A is using git as its SCM. I clone their repo
I clone a repo to c:\mycopy I create new files in my working dir

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.