TekOnline

Preserving Critical Code in Git History

The Problem

In large codebases with multiple developers, critical code sections can be accidentally overwritten or removed during merges, refactoring, or cleanup operations. When this happens, it can be challenging to track down when and where the critical code was lost, especially if it happened many commits ago.

The Solution: Critical Commit Tags

The most effective way to preserve and track critical code in git history is through a consistent commit message tagging system. This approach makes it easy to search through commit history to find specific critical code sections, even months or years after they were committed.

Best Practices

  1. Use a Consistent Prefix Format[AUTH-CRITICAL] Authentication fix - DO NOT OVERWRITE [SECURITY-CRITICAL] Security patch - DO NOT REMOVE [PERF-CRITICAL] Performance optimization - PRESERVE
  2. Structure Your Commit Messagesgit commit -m "[AUTH-CRITICAL] Authentication fix - DO NOT OVERWRITE [AUTH-CRITICAL] This commit contains critical authentication code that must be preserved. Contact: [Your Name] Reason: [Brief explanation] Related Issue: #123 Last Verified: [Date]"
  3. Make Prefixes Unique
    • Use a format that won’t appear in normal commit messages
    • Consider using a project-specific prefix (e.g., [CDIS-AUTH-CRITICAL])
    • Include the word “CRITICAL” to make it stand out

How to Search for Critical Commits

# Search for commits with the critical tag
git log --grep="AUTH-CRITICAL"
# Search through all branches
git log --all --grep="AUTH-CRITICAL"

# Search including commit message body
git log --all --full-history -- "AUTH-CRITICAL"

# Search in a specific file
git log --grep="AUTH-CRITICAL" -- path/to/file.cs

Viewing Changes

# Show the changes in a specific commit
git show <commit-hash>

# Show the changes in a specific file from a critical commit
git show <commit-hash>:path/to/file.cs

Why This Approach Works

  1. Searchable: Can be found using standard git commands
  2. Preserved: Stays in git history even if code is modified
  3. Visible: Shows up in code review tools
  4. Trackable: Works across all git platforms
  5. Flexible: Can be used for different types of critical code

Alternative Approaches (and Why They’re Less Effective)

  1. Comments in Code
    • Can be removed during merges
    • Might be stripped in production builds
    • Can be accidentally deleted during refactoring
  2. #region Tags
    • Can be accidentally removed
    • Not searchable in git history
    • Might be cleaned up by automated tools
  3. TODO Comments
    • Often cleaned up by automated tools
    • Not searchable in git history
    • Can be accidentally removed
  4. XML Documentation
    • Might be stripped in production builds
    • Not searchable in git history
    • Can be accidentally removed
  5. Protected Branches
    • Only prevents future issues
    • Doesn’t help find past issues
    • Doesn’t preserve history

Implementation Tips

  1. Create a Documentation
    • List all critical commit prefixes
    • Document the purpose of each prefix
    • Include examples of proper usage
  2. Team Communication
    • Share the prefix system with your team
    • Include it in your coding standards
    • Add it to your pull request template
  3. Regular Verification
    • Periodically search for critical commits
    • Verify that critical code is still present
    • Update commit messages if needed

Example Workflow

  1. Identifying Critical Code// This authentication code is critical for the application public async Task<IActionResult> SignOut() { await _signInManager.SignOutAsync(); HttpContext.Session.Clear(); return RedirectToAction("Index", "Home"); }
  2. Making the Commitgit commit -m "[AUTH-CRITICAL] Authentication sign-out implementation [AUTH-CRITICAL] This commit contains critical authentication code that must be preserved. Contact: John Doe Reason: Core authentication functionality Related Issue: #456 Last Verified: 2024-03-20"
  3. Finding It Later# Search for the commit git log --grep="AUTH-CRITICAL" # View the changes git show <commit-hash>

Conclusion

Using a consistent commit message tagging system is the most effective way to preserve and track critical code in git history. It provides a reliable way to find important code sections, even long after they were committed, and helps prevent accidental removal or modification of critical functionality.

Remember: The key to success is consistency. Always use the same prefix format and include detailed information in your commit messages. This will make it much easier to find and preserve critical code in your codebase.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *