Costa Michailidis
Costa Michailidis

Reputation: 8178

How to know which commit belongs to which branch

When I use git log --graph (or my alias for it) how can I tell which commit (*) belongs to which branch?

Here's my gitconfig

tree = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(boldblue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(black)%s%C(reset)%C(dim black)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all

Any help would rock!

Update I ended up using: git log --all --graph --format=format:'%C(bold blue)%h%C(reset) %C(dim black)%s%C(reset)%C(bold red)%d%C(reset) %C(green)by %an, %ar%C(reset)'

Upvotes: 1

Views: 728

Answers (4)

Costa Michailidis
Costa Michailidis

Reputation: 8178

I ended up using: git log --all --graph --format=format:'%C(bold blue)%h%C(reset) %C(dim black)%s%C(reset)%C(bold red)%d%C(reset) %C(green)by %an, %ar%C(reset)'

Upvotes: 0

Gary Fixler
Gary Fixler

Reputation: 6018

I just use git log --all --graph --oneline --decorate.

Git rearranges this graph as you make commits to always show merges coming in from the right toward the left. You can always tell what's a branch, and where it's merging because of this, even when it starts to get all twisty. Here's an example snippet from one of my repos, as output by the above log command:

| * af8bb24 Remove mayaaxis.py
| * 39b27d7 Remove axis.py
|/  
*   a12c712 Merge branch 'MayaObject'
|\  
| * 755c024 Elide pymel.core.animation import in core.py
| * 6de87c2 Clean up imports in core.py
| * 4b70adf Move core.RefGrid to new grid.py
| *   40d3c7d Merge branch 'absorbMayaRef' into MayaObject
| |\  
| | * 58305e5 Add core.MayaObject test for refFile property
| | * 154af30 Refactor core.MayaObject refPath to refFile
| | * d23fe77 Condense namespace work in core.MayaRef.loadRef
| | * 22c780b Add missing test for core.MayaRef namespace arg
| |/  
| * 6843970 Remove projPath arg from core.MayaRef constructor
| * 692578a Add docstrings to core.MayaObject test classes
| * acb4d75 Add core.MayaObject init defaults test
| * b8f1ae6 Stub in core.MayaObject
|/  
* 4dc6112 Make __init__.py imports relative
* abde256 Add *.swp, and *.swo to .gitignore

Note the a12c712 commit near the top - that's a merge of the former 'MayaObject' branch, which started near bottom at the 4dc6112 commit. You can see it branch off to the right at the bottom, and merge back in to the left near the top. The first commit on the 'MayaObject' branch was b8f1ae6, which forks off to the right - branching always goes off to the right, merging always comes in toward the left.

You can also see that while I was on the 'MayaObject' branch, I decided to jump off again and work on the 'absorbMayaRef' branch, which forked off of 6843970, and it's first commit was 22c780b. It merged back into 'MayaObject' at 40d3c7d, and then 'MayaObject' - as stated at the beginning - merged back into the master branch at a12c712.

Leave the auto-generated merge commit messages (e.g. "Merge branch 'absorbMayaRef' into MayaObject") intact, and you'll always be able to tell what belonged to which branch. If you delete or overwrite those messages when making merge commits, then you will not be able to tell what was on which branch.

I always make it clear in my commits what I touched, because it's usually some small change in one file. Note how the 4 commits from 22c780b to 58305e5, inclusive, all mention 'core.MayaRef' or 'core.MayaObject'. Even if I didn't have the 40d3c7d merge telling me that was the 'absorbMayaRef' branch, I'd still be able to see pretty quickly that that feature branch had to do with something in the core module, and with those 2 classes in particular. When my changes touch a bunch of files, I try to make that clear, and note in general what happened in the change, e.g. "Refactor path names across library", or "Reorganize tests in several test files".

Upvotes: 2

vonbrand
vonbrand

Reputation: 11791

To look at the git history in X you can use gitk (part of the vanilla git suite, might be a separate package in your distribution). There are several git graphical front ends for git too.

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476547

Do you really need git log?

Again tig denotes this in a nicer way: the name of the branch is denoted between square brackets on the right of the first commit.

You install tig using sudo apt-get install tig, next you make the root of the repository your current directory and run tig.

The screen will show three groups of columns: the first containing information about the commit (date and author), the second will show an ASCII art of the tree and the third will provide messages.

tig also denotes the type of the commit (initial (I)/commit (O) and merge (M)) in the tree. Tags are denoted in purple, branch heads in cyan and branch heads of remotes in orange.

Sometimes not all branches are displayed (if the number is too large). In that case you can run tig --all

A more detailed manual is available here: http://jonas.nitro.dk/tig/manual.html

Upvotes: 3

Related Questions