在 simple-git 中移动并重新添加 remote URL 后设置上游分支跟踪
set upstream branch tracking after moving and re-adding remote URL in simple-git
以下代码检查本地存储库是否存在,并使用 simple-git 从远程存储库同步更改。我遇到了 JWT
令牌在 24 小时后过期的一些问题,这是通过删除并重新添加远程存储库 URL 解决的。
if (fs.existsSync(cachePath)) {
debug(`Local Path ${cachePath} Exists`);
// debug('Checking `git status` on local repo');
// Fetch
// FIXME: no upstream branch is set -> no tracking information for the current branch
// await git(cachePath).removeRemote('origin');
// await git(cachePath).addRemote('origin', gitURL);
// go into file and replace tocken
// FIXME: fatal branch 'master' doesn't exist
// execSync('git branch --set-upstream-to=origin/master master');
// await git(cachePath).branch(['--set-upstream-to=origin/master', 'master'], (err, data) => {
// if (err) throw new Error(err);
// });
// Show all branches
// debug('SHOW ALL BRANCHES');
// await git(cachePath).branch(['-a'], (err, data) => {
// if (err) throw new Error(err);
// });
/* CMDs
-------------------------------------------------- */
try {
execSync(`cd ${cachePath}`, { stdio: 'inherit' });
execSync('git remote -v', { stdio: 'inherit' });
execSync('git remote remove origin', { stdio: 'inherit' });
execSync(`git remote add origin ${gitURL}`, { stdio: 'inherit' });
execSync('git remote -v', { stdio: 'inherit' });
// execSync('git branch --set-upstream-to=origin/master master', { stdio: 'inherit' });
git(cachePath).branch(['-u', 'origin/master'], (err, data) => {
if (err) throw new Error(err);
console.log(data);
});
execSync('cd /home/ystanev/menlolab/runner', { stdio: 'inherit' });
} catch (e) {
throw new Error(e);
}
/* End of CMDs
-------------------------------------------------- */
debug('GIT PULL');
await git(cachePath)
.outputHandler(outputHandler)
.pull();
之前的操作似乎取消了上游分支跟踪,让我无法git fetch/pull
。在 git 输出之后,我通过执行 git branch --set-upstream-to=origin/master master
设置了跟踪,问题似乎已解决。
我已尝试通过 bash
命令完成全部操作,但我一直收到错误消息:
error: the requested upstream branch 'origin/master' does not exist
与远程仓库的通信似乎有问题,因为相同的命令 运行 在本地仓库中来自 bash
shell 没问题。
关于可能的原因有什么建议吗?
正如@torek 在评论中提到的,在设置上游之前我必须运行 git fetch
。
最终代码如下:
if (fs.existsSync(cachePath)) {
debug(`Local Path ${cachePath} Exists`);
// Fetch
await git(cachePath).removeRemote('origin');
await git(cachePath).addRemote('origin', gitURL);
await git(cachePath).fetch('origin');
await git(cachePath).branch(['-u', 'origin/master']);
debug('GIT PULL');
await git(cachePath)
.outputHandler(outputHandler)
.pull();
}
以下代码检查本地存储库是否存在,并使用 simple-git 从远程存储库同步更改。我遇到了 JWT
令牌在 24 小时后过期的一些问题,这是通过删除并重新添加远程存储库 URL 解决的。
if (fs.existsSync(cachePath)) {
debug(`Local Path ${cachePath} Exists`);
// debug('Checking `git status` on local repo');
// Fetch
// FIXME: no upstream branch is set -> no tracking information for the current branch
// await git(cachePath).removeRemote('origin');
// await git(cachePath).addRemote('origin', gitURL);
// go into file and replace tocken
// FIXME: fatal branch 'master' doesn't exist
// execSync('git branch --set-upstream-to=origin/master master');
// await git(cachePath).branch(['--set-upstream-to=origin/master', 'master'], (err, data) => {
// if (err) throw new Error(err);
// });
// Show all branches
// debug('SHOW ALL BRANCHES');
// await git(cachePath).branch(['-a'], (err, data) => {
// if (err) throw new Error(err);
// });
/* CMDs
-------------------------------------------------- */
try {
execSync(`cd ${cachePath}`, { stdio: 'inherit' });
execSync('git remote -v', { stdio: 'inherit' });
execSync('git remote remove origin', { stdio: 'inherit' });
execSync(`git remote add origin ${gitURL}`, { stdio: 'inherit' });
execSync('git remote -v', { stdio: 'inherit' });
// execSync('git branch --set-upstream-to=origin/master master', { stdio: 'inherit' });
git(cachePath).branch(['-u', 'origin/master'], (err, data) => {
if (err) throw new Error(err);
console.log(data);
});
execSync('cd /home/ystanev/menlolab/runner', { stdio: 'inherit' });
} catch (e) {
throw new Error(e);
}
/* End of CMDs
-------------------------------------------------- */
debug('GIT PULL');
await git(cachePath)
.outputHandler(outputHandler)
.pull();
之前的操作似乎取消了上游分支跟踪,让我无法git fetch/pull
。在 git 输出之后,我通过执行 git branch --set-upstream-to=origin/master master
设置了跟踪,问题似乎已解决。
我已尝试通过 bash
命令完成全部操作,但我一直收到错误消息:
error: the requested upstream branch 'origin/master' does not exist
与远程仓库的通信似乎有问题,因为相同的命令 运行 在本地仓库中来自 bash
shell 没问题。
关于可能的原因有什么建议吗?
正如@torek 在评论中提到的,在设置上游之前我必须运行 git fetch
。
最终代码如下:
if (fs.existsSync(cachePath)) {
debug(`Local Path ${cachePath} Exists`);
// Fetch
await git(cachePath).removeRemote('origin');
await git(cachePath).addRemote('origin', gitURL);
await git(cachePath).fetch('origin');
await git(cachePath).branch(['-u', 'origin/master']);
debug('GIT PULL');
await git(cachePath)
.outputHandler(outputHandler)
.pull();
}