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.