簡體   English   中英

在 simple-git 中移動和重新添加遠程 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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM