1. Overview

When we work on various projects, such as software development, it’s essential to refine the process with each update and commit. We can use Git, a versatile versioning system, to track every update and commit. We perform this with the help of Git commit ranges using double-dot and triple-dot notation. The Git commit ranges act like signposts that help us to return and examine specific parts of our code history.

In this tutorial, we explore the differences between double-dot “..” and triple-dot “…” syntax notation in Git commit ranges. Furthermore, we use the git log command to list commits and the git diff command to show differences between commits.

2. Using git log

We can use the git log command to display a record of all the commits that apply to our project. By default, it displays a list of commits made in the repository, with the most recent ones appearing first. Moreover, we can use git log to display each entry’s information, commit hash, author name, email, and more.

We can also use the git log command to display all the commits within a specified range using the Git double-dot and triple-dot commit ranges.

Let’s consider the following commit tree illustration as an example:

---A---B---C---D---E---F <== branch1
            \
             G---H <== branch2

Here, branch1 is the primary branch name master (main) and branch2 is the feature branch.

Now, let’s display the entire commit history of our repository based on the above commit tree in a compact and visually appealing way:

$ git log --all --decorate --oneline --graph
* b8434e1 (origin/feature) H
* 11dcc47 G
| * 74328c9 (HEAD -> main, origin/main, origin/HEAD) F
| * d4ac70c E
| * 6da1887 D
|/
* d0bb602 C
* 8dd752e B
* fa8c005 A

The above output shows that the main branch has commits F, E, and D that aren’t in the origin/feature branch, and the origin/feature has commits H and G that aren’t in the main branch.

2.1. Double-Dot

In Git commit history, the double-dot syntax is a common way to specify ranges. Additionally, we can use it with the git log command to find a series of commits accessible from one specific commit but not from another.

For instance, in our case, we use git log branch1..branch2 to list all commits that are accessible from branch2 but not from branch1.

Now, let’s list all the commits and see the changes that were made between two specific commits 74328c9 (F) and b8434e1 (H):

$ git log --oneline 74328c9..b8434e1
b8434e1 (origin/feature) H
11dcc47 G

It’s important to note that the double-dot notation excludes the starting point commit from the comparison. So, in the example above, the changes introduced in the master branch itself won’t be included in the log output.

2.2. Triple-Dot

Triple-dot syntax is another major way to specify ranges. We can use it to list all the commits reachable by one of two references, but not both.

The triple-dot notation provides a more precise way to explore our commit history. Furthermore, we can identify changes relative to a common ancestor, which is the latest commit that both specified commits share.

Let’s now list all the commits that are easy to access from branch2 and branch1, but not the ones common to both:

$ git log --oneline 74328c9...b8434e1
b8434e1 (origin/feature) H
11dcc47 G
74328c9 (HEAD -> main, origin/main, origin/HEAD) F
d4ac70c E
6da1887 D

Here, the output includes the main branch commits at positions F, E, and D, as well as the feature branch commits G and H. The triple-dot notation doesn’t include commits that are common to both branches, such as position C, A, and B.

3. Using git diff

git diff command is often used for comparing various types of Git objects, including commits, files, branches, etc. Moreover, we can use git diff to check what has changed in the project between any two specific commits.

3.1. Double-Dot

We can display the full difference between two commits by using a double-dot commit range along with the git diff command.

Let’s find the difference between the current commit position b8434e1 (H) of branch2 with commit position 74328c9 (F) of branch1:

$ git diff --name-only 74328c9..b8434e1
d.txt
e.txt
f.txt
g.txt
h.txt

Here, the git diff command returns the list of the names of files that are different in both branches of our Git repository.

3.2. Triple-Dot

We can use triple-dot notation with git diff to compare a main branch with a common ancestor of another branch. This simply lets us see the changes made on the feature branch by comparing it with the main branch.

As from our repo commit history, we have a common ancestor of the two branches at position d0bb602 (C). Moreover, we can find the difference between the current commit position H of branch2 with commit position C of branch1:

$ git diff --name-only 74328c9...b8434e1
g.txt
h.txt

We can also use git diff along with triple-dot syntax for reviewing pull requests. This is because it only shows the new changes made in the feature branch.

4. Difference Between git log and git diff

In Git, we often use git diff and git log commands to view what has changed between various versions of our project. We can use git log to list all the commit history of our repository. Additionally, we use git diff to show the differences between two points in the repository.

When using the git log, double-dots mean a direct comparison and triple-dots mean a comparison since divergence. Moreover, in git diff, two dots show cumulative changes, while three dots show distinct changes in the second commit only.

Here’s a summary of what using dots notation means for git diff and git log:

Command Descriptions
$ git log branch1..branch2 displays the commits that are in branch2 but not in branch1 (main)
$ git log branch1…branch2 shows the commits that are included in either branch1 or branch2, but not both
$ git diff branch1..branch2 shows the full differences between any two commits of branch1 and branch2
$ git diff branch1…branch2 displays the differences between the common ancestor of both branches and another branch

Moreover, we use these notations to compare different sets of commits, which is useful for many Git operations.

5. Conclusion

In this article, we learned some major differences between double-dot and triple-dot in Git commit ranges using practical examples.

We first looked at comparing the branches using a git log command along with double-dot and triple-dot notation. Then, we looked at the git diff command and found out how to compare the main branch with another branch. We did this comparison by using double and triple dot syntax along with the git diff command.

Lastly, we saw the difference between the git log and git diff commands and compared various sets of commit ranges.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.