我如何检查一个git仓库是否有任何提交? [英] How can I check whether a git repository has any commits in it?

查看:928
本文介绍了我如何检查一个git仓库是否有任何提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个自定义的git命令,该命令应该只在没有提交的全新存储库中运行(请参阅这个问题)。如何在我的脚本中执行一个简单的检查,看看现有的回购是否有零提交?



基本上,下面的空白是什么?

  if ___________;然后
echoGit repo已经有提交。正在中止。
else
echoGit repo没有提交。
fi


解决方案

摘要:


  • 提交被签出:test git rev-parse HEAD&> / dev / null

  • 指向提交的ref存在:test git rev-list -n 1 --all&> / dev / $

  • 对象存在于 git fsck git count-objects ,或检查 .git / objects的内容



现在进行讨论!



如果您想知道提交是否已签出,您可以使用 git rev-parse HEAD 。将会有输出,所以你可能想要重定向到 / dev / null ,并使用退出代码。出于所有实际的目的,这将足够好 - 做正常的事情,几乎不可能在没有 HEAD 指向任何东西的情况下结束,但是可以通过删除科幻.git目录中的。根据你的脚本,这可能是重要的 - 如果你要吹掉.git目录,你确实想成为偏执狂。



如果你想看是否有任何refs提交,你可以使用 git rev-list -n 1 --all 。同样,将会有输出(遇到第一次提交的SHA1),因此重定向到 / dev / null 并检查退出代码。



最后,如果你想检查是否有任何提交 - 即使它们没有任何提交(你必须真的尝试 很难进入这种状态)我可能会检查是否存在与 git fsck git count-objects - 或者失败,列出 .git / objects 并检查除 info pack (如果没有文件 .git / HEAD ,命令往往会失败)。是的,你实际上可以有一个回报与斑点和树木,但没有承诺,但你不得不努力到达那里。这些是绝对最安全的方法,如果你的脚本是可怕的。


I am writing a custom git command that should only run in a completely new repository with no commits (see this question). How can I perform a simple check within my script to see if an existing repo has zero commits?

Essentially, what goes in the blank below?

if ___________ ; then
    echo "Git repo already has commits. Aborting.
else
    echo "Git repo has no commits. Doing whatever my script does."
fi

解决方案

Summary:

  • A commit is checked out: test git rev-parse HEAD &> /dev/null
  • A ref pointing to a commit exists: test git rev-list -n 1 --all &> /dev/null
  • Objects exist in the repo: test output of git fsck, git count-objects, or the examine the contents of .git/objects

And now for the discussion!

If you want to know whether a commit is checked out, you can use git rev-parse HEAD. There will be output, so you probably want to redirect to /dev/null and just use the exit code. For all practical purposes, this will be good enough - doing normal things, it's pretty much impossible to end up without HEAD pointing at anything. But it is possible, for example by deleting files in the .git directory. Depending on your script, this might be important - if you're about to blow away the .git directory, you want to be paranoid indeed.

If you want to see whether there are any refs at all with commits on them, you can use git rev-list -n 1 --all. Again, there will be output (the SHA1 of the first commit encountered), so redirect to /dev/null and check the exit code.

Finally, if you want to check if there are any commits - even if they aren't on any refs (you have to try really hard to get into this state) I'd probably just check for the presence of objects with git fsck or git count-objects - or failing that, list .git/objects and check for anything besides info and pack (commands tend to fail if there is no file .git/HEAD). And yes, you could actually have a repo with blobs and trees but no commits, but you'd have to try even harder to get there. These are the absolute safest methods, if your script is scary.

这篇关于我如何检查一个git仓库是否有任何提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆