1

When I clone a repository with git (terminal) and then do a checkout to the remote develop branch with nodegit it works just fine.
But I ran into the problem, that when I want to switch back to the remote master branch, with the provided example, I can't figure it out.

Cloning with SSH

git clone git@mygit.com/projects/myproject.git

Checkout to develop works with the following code, but doesn't work for switching back, because the master branch already exists locally.

git.Repository.open(appDir)
   .then((repo) => {
       return repo.getHeadCommit()
                  .then((targetCommit) => {
                      return repo.createBranch(repositoryConfig.branch, targetCommit, false);
                  })
                  .then((reference) => {
                      return repo.checkoutBranch(reference, {});
                  })
                  .then(() => {
                      return repo.getReferenceCommit('refs/remotes/origin/' + repositoryConfig.branch);
                  })
                  .then((commit) => {
                      git.Reset.reset(repo, commit, 3, {});
                  })
                  .catch((err) => {
                      reject(err);
                  });
   })
   .then(() => {
       resolve('Checking out branch ' + repositoryConfig.branch + ' done');
   })
   .catch((err) => {
       reject(err);
   });

I tried it with this code, which resets my local copy to the master branch, but doesn't "follow" it, because a pull tells me after, that I'm X commits behind and that I should do a pull.

git.Repository.open(appDir)
   .then((repo) => {
       return repo.getBranch('refs/remotes/origin/' + repositoryConfig.branch)
                  .then((reference) => {
                      return repo.checkoutBranch(reference, {});
                  })
                  .then(() => {
                      return repo.getReferenceCommit('refs/remotes/origin/' + repositoryConfig.branch);
                  })
                  .then((commit) => {
                      git.Reset.reset(repo, commit, 3, {});
                  })
                  .catch((err) => {
                      reject(err);
                  });
   })
   .then(() => {
       console.log('Checking out branch ' + repositoryConfig.branch + ' done');
       resolve();
   })
   .catch((err) => {
       reject(err);
   });

In repositoryConfig.branch I set the branch to enable switching.

I'm not completely into git and how it works with attaching/detaching HEAD and vice versa.
So would appreciate any help.

  • When switching back to local `master` branch, first fetch the remote (`git fetch`) the you can do any of: 1. Delete the local `master` the checkout to `master` branch with the history of `origin/master` (`git branch -D master; git checkout master`) 2. checkout to `master` & hard reset to `origin/master` (`git checkout master; git reset --hard origin/master`) – Sajib Khan Sep 05 '18 at 06:15
  • @SajibKhan That's what I do in the second code block as you mentioned at 2. Checkout branch, getting the commit and resetting to it. After that my local copy is set to master but pull and switching to develop doesn't work anymore, because im still following develop. – Daniel Bonhard Sep 05 '18 at 06:43
  • Make sure you are resetting local `master` with latest `remote/master` commit. "a pull tells me after, that I'm X commits behind..." this message should come when local is not updated with remote – Sajib Khan Sep 05 '18 at 06:47
  • Have you tried after `git fetch`? – Sajib Khan Sep 05 '18 at 06:49
  • @SajibKhan Doesn't work with nodegit. I added `repo.fetchAll` before but my local copy still follows develop and not master. – Daniel Bonhard Sep 05 '18 at 07:04

0 Answers0