📜  git log filter merge commits - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:00:55.954000             🧑  作者: Mango

Git Log Filter Merge Commits - Shell/Bash

Git is a popular version control system used by developers to manage source code. Git allows developers to track changes, collaborate on code, and manage different versions of their code.

When working with Git, developers often use the git log command to view the commit history of a repository. However, the git log command can return a lot of information, including merge commits.

Merge commits are commits that combine two or more branches in a repository. They have two or more parent commits, and can make it difficult to view the history of a repository.

To filter out merge commits from the git log command, developers can use the --no-merges flag.

How to Filter Merge Commits with Git Log

To filter out merge commits from the git log command, developers can use the following command:

git log --no-merges

This command will return the commit history of a repository, excluding merge commits.

Example

Suppose we have a repository with the following commit history:

$ git log --oneline
f9197b6 Add README.md file.
5163d2e Add LICENSE file.
9e1d1cc Initial commit.

If we merge two branches with the following commands:

$ git checkout -b feature-branch-a
$ touch feature-a.txt
$ git add feature-a.txt
$ git commit -m "Add feature-a.txt"
$ git checkout -b feature-branch-b
$ touch feature-b.txt
$ git add feature-b.txt
$ git commit -m "Add feature-b.txt"
$ git checkout master
$ git merge feature-branch-a
$ git merge feature-branch-b

The git log command will now return the following commit history:

$ git log --oneline
cdd8a5e Merge branch 'feature-branch-b'
c1f7075 Merge branch 'feature-branch-a'
f9197b6 Add README.md file.
5163d2e Add LICENSE file.
9e1d1cc Initial commit.

To filter out merge commits, we can use the --no-merges flag:

$ git log --oneline --no-merges
f9197b6 Add README.md file.
5163d2e Add LICENSE file.
9e1d1cc Initial commit.

This command returns the commit history of the repository, excluding the merge commits.

Summary

In conclusion, the git log command is a powerful tool for developers to view the commit history of a repository. However, merge commits can clutter the output of this command. To filter out merge commits, developers can use the --no-merges flag.