简体   繁体   English

在 simple-git 中移动和重新添加远程 URL 后设置上游分支跟踪

[英]set upstream branch tracking after moving and re-adding remote URL in simple-git

The following piece of code checks if the local repository exist, and sync the changes from remote repository using simple-git .以下代码检查本地存储库是否存在,并使用simple-git同步远程存储库中的更改。 I was having some issues with JWT token expiring after 24 hours, this was fixed by removing and re-adding the remote repository URL.我遇到了一些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();

The previous operation seems to unset upstream branch tracking, leaving me unable to git fetch/pull .之前的操作似乎取消了上游分支跟踪,让我无法git fetch/pull Following the git output I've set tracking by executing git branch --set-upstream-to=origin/master master , the issue seems to be fixed.在 git 输出之后,我通过执行git branch --set-upstream-to=origin/master master设置了跟踪,问题似乎已解决。

I've tried to do the entire thing though the bash commands, I keep receiving error:我试图通过bash命令完成整个事情,但我不断收到错误消息:

error: the requested upstream branch 'origin/master' does not exist

There seem to be trouble with communication with remote repo, as the same commands run just fine from bash shell in local repo.与远程存储库的通信似乎存在问题,因为相同的命令在本地存储库中的bash shell 中运行得很好。

Any advice as to possible cause?关于可能的原因有什么建议吗?

As mentioned by @torek in the comments, I had to run git fetch before setting the upstream.正如@torek 在评论中提到的,我必须在设置上游之前运行git fetch

The final code is bellow:最终代码如下:

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM