Search code examples
gitgit-log

Show branch names with "git log --graph --oneline --decorate" when it takes a filename


Background

  • Our team uses a git repository on GitLab to store source files of a product.
  • We are using a git-flow-like branch model to commit and merge changes to the repository.
  • I found that some feature branches are not merged unintentionally. Also, there are some feature branches that should be deleted like:
    • Branches that merged to the develop branch successfully and not deleted yet
    • Branches that abandoned (because they can't be merged successfully)
  • I want to distinguish branches that should be merged from ones that should be deleted.

What I want to do

To confirm that all the necessary changes are merged to the develop branch successfully, I am trying to show a merge graph per file like this:

git log --graph --all --oneline --decorate=full develop -- ./path/to/a/file

And if there is a file that forked and not merged like this, I want to ask the owner of the file to merge or delete the branch that contains that file.

* 4151fa9 Commit Message 4
| * 8dc5658 Commit Message 3
* | d579006 Commit Message 2
|/  
* 4df043a Commit Message 1

Problem

I want to know the name of the branch that forked and not merged (8dc5658 in above), but git log --graph --oneline --decorate doesn't show the branch name if it takes a file name as its argument.

What I want to get as the result of git log --graph --all --oneline --decorate=full develop -- ./path/to/a/file is like this:

* 4151fa9 (HEAD -> develop) Commit Message 4
| * 8dc5658 (branch-abandoned) Commit Message 3
* | d579006 Commit Message 2
|/  
* 4df043a Commit Message 1

Question

How can I show the branch name in the result of git log --graph --oneline --decorate when it takes a file name as its argument?

Note that you can show an original branch for a commit with git branch --contains COMMIT, but it's difficult to identify commits to pass to git branch --contains from a commit graph (4151fa9 and 8dc5658 in the above) for each file in the repository.


Solution

  • Try adding --simplify-by-decoration :

    # note : if you use '--all', you don't need to add 'develop'
    git log --graph --all --oneline --simplify-by-decoration -- path/to/a/file
    

    you will get :

    • all commits that are tagged with a branch or a tag (or any ref actually)
    • and all commits that modify in some way path/to/a/file

    If you don't want to see all the possible refs drawn in your graph, you may use --decorate-refs :

    # will only show local branches :
    git log ... --decorate-refs=refs/heads
    
    # will only show 'origin/master' and local tags :
    git log ... --decorate-refs=refs/remotes/origin/master --decorate-refs=refs/tags