How do you display the list of commits that are ancestors of the current commit but exclude commits that are ancestors of another specified commit?
Posted by KarenKg
Last Updated: July 06, 2024
To display a list of commits that are ancestors of the current commit but exclude commits that are ancestors of another specified commit, you can use the git log command combined with some revisions. You can achieve this by using the following command:
git log --ancestry-path --no-merges <current-commit> ^<excluded-commit>
Here’s how it works: - <current-commit>: You can replace this with HEAD to refer to the current commit (the latest commit on your current branch). - <excluded-commit>: This is the commit that you want to exclude its ancestors from the list. To use the latest commit on the current branch and exclude ancestors of another commit, the command would be:
git log --ancestry-path --no-merges HEAD ^<excluded-commit>
Explanation of Options:
- --ancestry-path: This option ensures that only the commits on the direct path from the excluded commit to the current commit are considered. - --no-merges: This option excludes merge commits from the output (if you want to see merge commits, you can omit this option). - ^<excluded-commit>: This syntax excludes all commits that are ancestors of the specified commit.
Example:
Suppose you want to see the commits up to HEAD but exclude those that are ancestors of a commit with the hash abc1234, you would use:
git log --ancestry-path --no-merges HEAD ^abc1234
This command will give you the list of commits that are reachable from the current commit (HEAD) but not from abc1234.
Related Content