Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find out if a git commit was checked in before or after another commit given the hashes

Tags:

git

One thing I miss about using svn was the simple-numbering of revision numbers. I can easily see if the version deployed in the testing environment is before or after a certain commit.

With git using hashes for its commits, what is a way to tell if a commit was made before or after another commit?

like image 958
Glide Avatar asked Jan 08 '13 19:01

Glide


People also ask

How do you check if a commit is before another commit?

If the last commit displayed is the same than the first commit in the git rev-list command, then it is a commit reachable from the second commit. If the first commit is not reachable from the second, git rev-list should return nothing.

How do I see my 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.

How do you look at a previous commit?

If you want to look at previous commits, you can use git log and its many arguments. If you want to checkout an actual commit to view the files in an editor, just use git checkout to move to any commit you want. When you are finished, just do git checkout master to return to your current state.

How do you check changes in a particular commit?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .


4 Answers

Things, as you note, aren't so simple in Git. In particular, the definition of "before" and "after" need a little more clarification.

If you trust the people committing to your repository not to mess with timestamps, and you already know both commits are on the same branch, you can compare the timestamps of the commits and just see which one is earlier. Use the following command for each commit, and compare the result.

git log -1 --format='%ci' <commit>

With Git, you can't necessarily trust the timestamps, however, since unlike Subversion there isn't a central repository with a job of producing them. You also can't be sure that two commits are on the same branch (although that problem also exists with Subversion).

To avoid those problems, talk about whether one commit is an ancestor of another, rather than whether it comes before or after it. In the following commit graph, B and C are ancestors of A, while B is not an ancestor of C nor vice versa:

B    (master)
| C  (branch)
|/
A    (root)

To determine whether commit A is an ancestor of commit B, use the following command (based on an article at git ready):

git rev-list <commitA> | grep $(git rev-parse <commitB>)

The first part lists all the commits that are ancestors of commit A; the second part gets the full hash of commit B, then searches the ancestor list for that commit. If you see any output, commit A is an ancestor of commit B. Swap the arguments to work out if commit A is an ancestor of commit B instead.

This second form is slower, but gives you absolute certainty that one commit is an ancestor of another.

like image 69
me_and Avatar answered Oct 19 '22 22:10

me_and


git merge-base has a flag for that. It simply returns a 0/1 exit code.

git merge-base --is-ancestor <commit> <commit>
like image 30
Dane White Avatar answered Oct 19 '22 23:10

Dane White


git rev-list --count can help. Supposing 110a187 comes 4 commits before 5d41af1, then:

 $ git rev-list --count 110a187..5d41af1
4
 $ git rev-list --count 5d41af1..110a187
0

Thus something like:

test $(git rev-list --count $a..$b) == 0 && \
       echo "$a is not an ancestor of $b"
like image 32
Douglas Bagnall Avatar answered Oct 19 '22 21:10

Douglas Bagnall


Use git show on each commit to find the date of the commit. There's no way just looking at the hash to determine order, since it's nothing but a hash.

You can use custom format strings to show just what you want, check the man pages for more info.

git show --format="%ci" <commit>
like image 40
Philip Rieck Avatar answered Oct 19 '22 23:10

Philip Rieck