简体   繁体   English

如何查看本地git repo和远程repo是否是同一个提交?

[英]How to see if local git repo and remote repo are same commit?

I have a local git repo, and I have a remote git repo. 我有一个本地git仓库,我有一个远程git仓库。 What is a simple command to see what commit the remote repo is, and what commit the local repo is, so I can simple see if I'm up to date? 什么是一个简单的命令来查看提交远程仓库的是什么,以及本地仓库的提交是什么,所以我可以简单地看看我是否是最新的?

This is going to be automated in a program, so I don't want lots of complicated stuff that I would have to parse. 这将在一个程序中自动化,所以我不想要很多复杂的东西,我将不得不解析。 Preferably it would be cool to have both local & remote output the same text, with only the commit changing between the two. 优选地,使本地和远程输出都具有相同的文本是很酷的,只有提交在两者之间变化。 Any ideas? 有任何想法吗?

A Programmatic Solution 程序化解决方案

If you want a programmatic solution, you can look at the commits stored in each head and compare them. 如果需要编程解决方案,可以查看存储在每个头中的提交并进行比较。 For example: 例如:

remote=$(
    git ls-remote -h origin master |
    awk '{print $1}'
)
local=$(git rev-parse HEAD)

printf "Local : %s\nRemote: %s\n" $local $remote

if [[ $local == $remote ]]; then
    echo "Commits match."
else
    echo "Commits don't match."
fi

Sample Output 样本输出

Local : 9e1b4dc286acb442f7f604be7916db660b9d70cd
Remote: 9e1b4dc286acb442f7f604be7916db660b9d70cd
Commits match.

Assuming that your remote repository is referred to by the remote origin , and you're interested in the branch master , you can do: 假设您的远程存储库由远程origin引用,并且您对分支master感兴趣,则可以执行以下操作:

git fetch origin

And then compare the output of: 然后比较输出:

git rev-parse master

... and: ......和:

git rev-parse origin/master

If the object names that are output by those two commands are the same, then your master and the master in origin were the same at the point when the git fetch origin was run. 如果这两个命令输出的对象名称相同,那么在运行git fetch origin时, mastermaster in origin是相同的。

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

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