Ratul was here

Fine grained Git Blame configuration : Practical Examples to Improve Your Development Workflow

Git is a popular version control system that allows you to manage your codebase and collaborate with others. One of the most powerful features of Git is git blame, which allows you to view the revision history of a file line-by-line. In this article, we'll explore how to use git blame to view the raw content of a file and its line-by-line revision history, and how to exclude specific commits from the revision history using the .git-blame-ignore-revs file.

Viewing the Line-by-Line Revision History for a File

Within the blame view, you can view the line-by-line revision history for an entire file.

  1. Click to open the file whose line history you want to view.
  2. In the upper-right corner of the file view, click Blame to open the blame view.
  3. To see earlier revisions of a specific line, or reblame, click the versions icon until you've found the changes you're interested in viewing.

Ignoring Commits in the Blame View

All revisions specified in the .git-blame-ignore-revs file, which must be in the root directory of your repository, are hidden from the blame view using Git's git blame --ignore-revs-file configuration setting.

  1. In the root directory of your repository, create a file named .git-blame-ignore-revs.

  2. Add the commit hashes you want to exclude from the blame view to that file. We recommend the file to be structured as follows, including comments:

    # .git-blame-ignore-revs
    # Removed semi-colons from the entire codebase
    a8940f7fbddf7fad9d7d50014d4e8d46baf30592
    # Converted all JavaScript to TypeScript
    69d029cec8337c616552756310748c4a507bd75a
    
  3. Commit and push the changes.

Now when you visit the blame view, the listed revisions will not be included in the blame. You'll see an Ignoring revisions in .git-blame-ignore-revs banner indicating that some commits may be hidden.

Bypassing .git-blame-ignore-revs in the Blame View

If the blame view for a file shows Ignoring revisions in .git-blame-ignore-revs, you can still bypass .git-blame-ignore-revs and see the normal blame view. In the URL, append a ~ to the SHA and the Ignoring revisions in .git-blame-ignore-revs banner will disappear.

Conclusion

git blame is a powerful tool for understanding the revision history of a file in Git. With the techniques we've covered in this article, you can view the raw content of a file and its line-by-line revision history, and exclude specific commits from the revision history using the .git-blame-ignore-revs file. We hope this article has been helpful in understanding Git's git blame command and its various options.

This post is inspired from a github documentation page.

#best-practice #developer #git #git-blame #github #productivity