Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check whether a git repository has any commits in it?

Tags:

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 
like image 740
Ryan C. Thompson Avatar asked Mar 30 '11 20:03

Ryan C. Thompson


People also ask

Where can I see my git commits?

On GitHub.com, you can access your project history by selecting the commit button from the code tab on your project. Locally, you can use git log . The git log command enables you to display a list of all of the commits on your current branch. By default, the git log command presents a lot of information all at once.

How do I check my git repository status?

To check the status, open the git bash, and run the status command on your desired directory. It will run as follows: $ git status.

Which git command show all commits in the current branchs history?

I think an option for your purposes is git log --oneline --decorate . This lets you know the checked commit, and the top commits for each branch that you have in your story line. By doing this, you have a nice view on the structure of your repo and the commits associated to a specific branch.


2 Answers

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.

like image 67
Cascabel Avatar answered Sep 19 '22 13:09

Cascabel


You want to use git log

If you are using cygwin or are on a linux machine gitk is a useful program to have as well.

like image 29
RDL Avatar answered Sep 20 '22 13:09

RDL