Rebron1900
V2EX  ›  问与答

请问怎么在 powershell 里获取到 git push 的执行结果?

  •  
  •   Rebron1900 · Jul 13, 2022 · 2064 views
    This topic created in 1583 days ago, the information mentioned may be changed or developed.

    因为最新笔记软件换了 Logseq ,同步方案用的 Git ,用了一个脚本自动提交修改后的笔记库。

    但是由于经常在公司和家里切换,回家里后经常提示需远端库更新,需要先 pull 一下才可以重新 push 。

    我现在修改脚本如下,想获取 git push 的执行结果,如果报错了就执行一下 git pull ,但是这种写法似乎获取不到执行结果,不知道怎么回事。

    希望大佬们指点一下。

    function Start-GitAutoCommitAndPush {
        [string] $status = (& git status)
        if (!$status.Contains("working tree clean")) {
            git add .
            git commit -m "auto commit"
        }
        if ($PushToServer) {
        	# 执行 git push ,并记录执行结果
            [string] $output = (&  git push $Server $Branch)        
            # 如果执行结果里提示需要 git pull,则 git pull 一下。
            if(!$output.Contains("git pull")){
                git pull
            }
        }
    }
    
    5 replies  •  2022-07-13 14:58:05 +08:00
    renmu123
    renmu123
    Jul 13, 2022 via Android
    1
    没必要获取结果,你就先 pull 然后 push 就完事了
    Rebron1900
    Rebron1900
    OP
    Jul 13, 2022
    2
    @renmu123 这样不太好把?我切换环境后只有第一次需要 pull
    moen
    moen
    Jul 13, 2022❤️1
    3
    git 的输出是写到 stderr 的,所以得写成 `& git push $Server $Branch 2>&1`
    Rebron1900
    Rebron1900
    OP
    Jul 13, 2022
    4
    @moen [string] $output = (& git push $Server $Branch 2>&1) 意思是这样?
    Rebron1900
    Rebron1900
    OP
    Jul 13, 2022
    5
    @moen 谢谢,解决了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4792 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 03:31 · PVG 11:31 · LAX 19:31 · JFK 22:31
    ♥ Do have faith in what you're doing.