Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in which file git stores commit history?

Tags:

git

gitolite

I want to read from the file where git stores commit history to store each commit information in my project's DB and display all histories in my project view

like image 347
gasser Avatar asked Sep 18 '11 13:09

gasser


People also ask

How do I find commit history?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.

Is git history stored locally?

Git stores all its internal data in the hidden folder . git . It is in the root of your project checkout. Unless you push commits back to the original git repository, your local copy is the only place where they are.

Where are git files stored before commit?

Before committing the code, it has to be in the staging area. The staging area is there to keep track of all the files which are to be committed.

Where are git commit messages stored?

The file is located in the . git folder, the file is named "COMMIT_EDITMSG". Show activity on this post. This will allow you to modify your commit, as well as your commit message on your local branch.


2 Answers

There's no single file you can interrogate to get the commit history. There are plenty of good explanations of git's object model (e.g. git for computer scientists, Pro Git, the git community book), but it might be useful to have a quick explanation here:

There are various types of objects in git, most importantly:

  • blobs (files) - just binary files
  • trees (directories) - a tree is a list of other objects (usually blobs and trees) with their name, a hash and a limited set of permissions
  • commits (versions) - each commit includes the hashes of its parent commits, the author, the commit message and other metadata

Each of these is identified by a hash of its contents, and this hash is known as the object name - these are the 40 digit hex strings you've probably seen in the course of using git. Each object is stored in the .git/objects/ directory, either as a loose object (one per file) or as one of many objects stored efficiently in a pack file. The file .git/HEAD represents the version that your repository is currently at, and usually contains a reference to a particular branch, represented by a file under .git/refs/heads or a reference stored in pack file. (HEAD may also point directly to a particular commit's object name.) One of these files representing a branch, such as .git/refs/heads/master, just contains an object name.

In order to traverse the history back from this branch tip, git will find the object named in that file in the object database, and recursively follow the pointers to its parents.


However, for the use case you describe (i.e. traversing the history to export it), I would strongly suggest that you do one of the following:

  • Invoke git commands to find the history. If you stick to using the so-called "plumbing" commands, their output should be stable across git versions.
  • Use the libgit2 library to interrogate the repository. libgit2 is a fully re-entrant library for interrogating git repositories, which now has bindings for many languages.
like image 105
Mark Longair Avatar answered Oct 23 '22 14:10

Mark Longair


You can get an idea of how git stores objects here: How git stores objects. Read the other chapters in that book in the "Internals and Plumbing" section to understand how it all works.

Essentially, there is no "file where the commit history is stored", it's more complex than that. You should use one of the existing APIs for your language of choice (search for "git API" and your language, there are a lot out there).

like image 20
Mat Avatar answered Oct 23 '22 16:10

Mat