How do you show the list of commits that are reachable from a specified commit, excluding commits reachable from another specified commit?
Posted by AliceWk
Last Updated: July 21, 2024
In Git, you can show the list of commits reachable from a specified commit while excluding commits reachable from another specified commit using the git log command with appropriate options. Here’s the general syntax you will use:
git log <commit1> ^<commit2>
- <commit1> is the hash or reference of the commit from which you want to show reachable commits. - <commit2> is the commit you want to exclude from the results. For example, if you want to show commits reachable from commit1 but exclude those reachable from commit2, you can run:
git log <commit1> ^<commit2>
Example:
Suppose you have two commit hashes: abc123 and def456. To see the commits reachable from abc123 but not from def456, you would run:
git log abc123 ^def456
Additional Options:
You can also add formatting or additional options to the git log command to refine the output, for example: - --oneline for a brief summary. - --decorate to show branch and tag names. - --graph to visualize the commit history. Combining these options might look like this:
git log --oneline --graph abc123 ^def456
Note:
- The caret ^ operator denotes "exclude" in this context. - You can also use the .. range notation, which can provide similar functionality:
git log abc123 --not def456
This will produce the same result, showing commits reachable from abc123 but not reachable from def456.
Related Content