gpt4 book ai didi

git - 向 "git status --porcelain"添加什么以使其表现得像 "git status"?

转载 作者:太空狗 更新时间:2023-10-29 13:14:43 27 4
gpt4 key购买 nike

An earlier question导致了一些关于如何检查 Git 存储库是否包含脏索引或未跟踪文件的想法。我从那次讨论中采纳的答案如下:

#!/bin/sh
exit $(git status --porcelain | wc -l)

这个答案背后的想法是模仿程序员的做法:运行 git status 然后检查输出。

不幸的是 git status --porcelaingit status 做的事情并不完全相同。特别是,git status 将报告未推送的更改,而 git status --porcelain 则不会。这是一个例子

[jarmo@localhost math-hl]$ git status --porcelain
[jarmo@localhost math-hl]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

所以我的问题是:我需要在原始脚本中添加什么来识别 repo 何时有未推送的更改?特别是,这是正确的方法吗?

#!/bin/bash
if [ $(git status --porcelain | wc -l) != "0" \
-o $(git log @{u}.. | wc -l) != "0" ]; then
echo "local repo is not clean"

从长远来看,依赖 git log 是否可以,还是我应该改用“管道”命令?

最佳答案

git 脚本中的想法是始终使用:

参见“What does the term porcelain mean in Git?”。
正如我所解释的,--porcelain 选项是,不幸的是(因为它的命名令人困惑),获得状态的“管道”方式,这是“可靠”的方式,这意味着它的格式不会' t 随着时间的推移而改变,这就是为什么它是推荐的脚本命令。

对于git log,最好使用git rev-list:

git rev-list HEAD@{upstream}..HEAD

参见“How to know if git repository has changes that have not been synchronized with server (origin)?

关于git - 向 "git status --porcelain"添加什么以使其表现得像 "git status"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19206816/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com