nodegit: Error: Reference 'refs/remotes/master/HEAD' not found

Hi, I am trying to implement ‘git pull origin master’ command using nodegit. Used example as base https://github.com/nodegit/nodegit/blob/master/examples/pull.js

var repo;

Git.Repository.init("some/path", 0)
    // git remote add origin REMOTE
    .then(repository => {
        Git.Remote.create(repository, "origin", "sshurl");
        return repository;
    })
    // git pull origin master
    .then(repository => {
        repo = repository;
        return repo.fetch("origin", {
            callbacks: {
                credentials: (url, userName) => {
                    return Git.Cred.sshKeyFromAgent(userName);
                },
                certificateCheck: () => { 
                    return 1;
                },
                transferProgress: info => {
                    return console.log(info.receivedObjects() + ' / ' + info.totalObjects());
                }
            }
        })
    })
    .then(() => {
        return repo.mergeBranches("master", "origin/master");
    })

.git/config:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*

After that I am getting:

Error: Reference 'refs/remotes/master/HEAD' not found
    at Error (native)

What am I doing wrong? Credentials work for sure as I see fetching progress.

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 25 (1 by maintainers)

Most upvoted comments

Here is code that works for me. Note that it is not the ideal implementation but it does demonstrate the things missing with an empty clone.

// this method ensures that there is a master branch to pull into
function createMasterBranch(repository) {
  const masterRefName = 'master';
  const remoteMasterRefName = 'refs/remotes/origin/master';
  return repository.getBranch(masterRefName).then(ref => true)
  .catch(err =>
    Git.Reference.symbolicCreate(repository, 'refs/remotes/origin/HEAD', remoteMasterRefName, 1, 'Set origin head')
    .then(result =>
      repository.getReference(remoteMasterRefName)
      .then(remoteMasterRef => {
        const commitOid = remoteMasterRef.target();
        return repository.createBranch('master', commitOid) // create master to point to origin/master
        .then(masterRef => repository.checkoutBranch(masterRef)) // checkout branch to update working directory
        .then(() => true);
      })
      .catch(e => false) // if getting the remote ref fails, continue as usual
    )
  );
}

Example usage:

    repo.fetchAll(fetchOptions).then(() =>
      createMasterBranch(repo).then(succeeded =>
        succeeded ?
        repo.mergeBranches('master', 'origin/master', null, Git.Merge.PREFERENCE.FASTFORWARD_ONLY).then(() => true)
        : false
      )
    )

@carlosmn the following triggered something in my head about our use case

What git merge does when on an unborn branch is to do the moral equivalent of a fast-forward.

For us, we’ve seen this issue when trying to pull when the local master branch has no commits but the remote had a commit added. I understand why our code was failing now with your statement about what git merge does and the fact that this error is a bit of red herring. I just updated our code to create the local branch when the local version is unborn, and all is now good. (the code is a lot simpler too! 😄)

The previous code I posted was working around the error, but the symref creation is unnecessary. I just need to handle the edge case for an unborn master branch and not call NodeGit’s mergeBranches method.

Hope this is helpful for the discussion!