All posts

Git Version Control Mastery

· Pavan Y N · git, version-control, collaboration, workflow


# title: "Git Version Control Mastery: Complete Guide for DevOps" date: "2025-10-17" tags: ["git", "version-control", "collaboration", "workflow"]

Git Version Control Mastery: Complete Guide for DevOps

Git is the backbone of modern software development and DevOps practices. This comprehensive guide covers everything from fundamentals to advanced workflows.

# Why Git Matters in DevOps

Git enables:

  • →Collaboration: Multiple developers working simultaneously
  • →Version History: Complete audit trail of changes
  • →Branching: Parallel development without conflicts
  • →Rollback: Easy recovery from mistakes
  • →Integration: Seamless CI/CD pipeline integration

# Essential Git Commands

## Repository Setup

code
# Initialize new repository
git init

# Clone existing repository
git clone https://github.com/user/repo.git

# Check repository status
git status

## Basic Workflow

code
# Stage changes
git add .
git add specific-file.txt

# Commit changes
git commit -m "Descriptive commit message"

# Push to remote
git push origin main

# Pull latest changes
git pull origin main

# Branching Strategies

## GitFlow

Best for release-based workflows:

  • →main: Production-ready code
  • →develop: Integration branch
  • →feature/*: New features
  • →release/*: Release preparation
  • →hotfix/*: Emergency fixes

## Trunk-Based Development

Best for continuous deployment:

  • →Single main branch
  • →Short-lived feature branches
  • →Frequent integration
  • →Feature flags for incomplete work

# Handling Merge Conflicts

Conflicts occur when same lines are modified:

code
# Conflict markers in file
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> branch-name

# Resolution steps:
1. Edit conflicted files
2. Remove conflict markers
3. git add <file>
4. git commit

# Pull Request Best Practices

  1. •Keep PRs Small: Focus on single feature/fix
  2. •Write Clear Descriptions: Explain what and why
  3. •Add Context: Link to issues, provide screenshots
  4. •Request Specific Reviewers: Get relevant expertise
  5. •Respond Promptly: Address feedback quickly
  6. •Use Draft PRs: For work-in-progress

# Advanced Git Techniques

## Interactive Rebase

code
# Clean up commit history
git rebase -i HEAD~3

# Options: pick, squash, reword, drop

## Cherry-Pick

code
# Apply specific commit to current branch
git cherry-pick <commit-hash>

## Stash

code
# Save work temporarily
git stash save "Work in progress"

# List stashes
git stash list

# Apply stash
git stash apply stash@{0}

# Git Hooks for Automation

Pre-commit hook example:

code
#!/bin/sh
# Run tests before commit
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit aborted."
    exit 1
fi

# Best Practices

  1. •Commit Often: Small, focused commits
  2. •Write Good Messages: Clear, descriptive
  3. •Pull Before Push: Stay synchronized
  4. •Use Branches: Never commit directly to main
  5. •Review Before Merge: Code review is essential
  6. •Tag Releases: Mark important versions

# Common Mistakes to Avoid

  • →Committing sensitive data
  • →Large binary files in repository
  • →Unclear commit messages
  • →Working directly on main branch
  • →Not pulling before starting work
  • →Force pushing to shared branches

# Git in CI/CD Pipelines

Integration example:

code
# GitHub Actions
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Run tests
        run: npm test

# Conclusion

Mastering Git is essential for DevOps success. Practice these concepts, adopt a workflow that fits your team, and continuously improve your version control practices.

# Next Steps

  1. •Practice branching and merging
  2. •Set up Git hooks for your project
  3. •Implement a branching strategy
  4. •Configure CI/CD with Git integration
  5. •Learn advanced Git commands

Happy coding! 🚀