Devops
DevopsIntermediate

Version Control Best Practices: Git Workflows for DevOps Teams

DeveloperHat Team
5 min read
GitVersion ControlCollaborationBest Practices

TL;DR

Learn effective Git workflows and best practices for managing code in DevOps environments.

Version Control Best Practices: Git Workflows for DevOps Teams

Effective version control is crucial for DevOps success. This guide explores best practices and workflows for managing code with Git in a DevOps environment.

$1

$1

1. Feature Branch Workflow

2. Gitflow Workflow

3. Trunk-Based Development

4. Release Branch Model

$1

``mermaid

graph TD

A[Feature Branch] --> B[Development]

B --> C[Release]

C --> D[Main]

E[Trunk-Based] --> F[Main]

G[Feature Toggle] --> F

`

$1

$1

`bash

Create feature branch

git checkout -b feature/new-feature main

Make changes and commit

git add .

git commit -m "feat: add new feature"

Push changes

git push origin feature/new-feature

Create pull request

Review and merge

`

$1

`yaml

branch_naming:

features: feature/*

bugfix: fix/*

hotfix: hotfix/*

release: release/*

commit_conventions:

- feat: new features

- fix: bug fixes

- docs: documentation

- style: formatting

- refactor: code restructuring

- test: adding tests

- chore: maintenance

`

$1

$1

`bash

Initialize Gitflow

git flow init

Start feature

git flow feature start new-feature

Finish feature

git flow feature finish new-feature

Create release

git flow release start 1.0.0

git flow release finish 1.0.0

`

$1

`ini

[gitflow "branch"]

master = main

develop = develop

[gitflow "prefix"]

feature = feature/

release = release/

hotfix = hotfix/

support = support/

versiontag = v

`

$1

$1

`bash

Work directly on main

git checkout main

git pull origin main

Create short-lived feature branch if needed

git checkout -b feature/quick-fix

Make changes

git checkout main

git merge feature/quick-fix

`

$1

`javascript

const features = {

newFeature: {

enabled: process.env.ENABLE_NEW_FEATURE === 'true',

rolloutPercentage: 25

}

};

function isFeatureEnabled(feature, userId) {

return features[feature].enabled &&

(hash(userId) % 100) < features[feature].rolloutPercentage;

}

`

$1

$1

`markdown

$1

[Describe the changes]

$1

  • [ ] Bug fix
  • [ ] New feature
  • [ ] Breaking change
  • [ ] Documentation update
  • $1

  • [ ] Unit tests
  • [ ] Integration tests
  • [ ] Manual testing
  • $1

  • [ ] Code follows style guidelines
  • [ ] Documentation updated
  • [ ] Tests added/updated
  • [ ] Reviewed by team member
  • `

    $1

    `yaml

    .github/workflows/pr-checks.yml

    name: PR Checks

    on: [pull_request]

    jobs:

    validate:

    runs-on: ubuntu-latest

    steps:

    - uses: actions/checkout@v2

    - name: Run tests

    run: |

    npm install

    npm test

    - name: Lint code

    run: npm run lint

    `

    $1

    $1

    `json

    {

    "protection": {

    "required_status_checks": {

    "strict": true,

    "contexts": [

    "continuous-integration/jenkins",

    "security-scan"

    ]

    },

    "required_pull_request_reviews": {

    "required_approving_review_count": 2

    },

    "restrictions": {

    "users": [],

    "teams": ["devops-team"]

    }

    }

    }

    `

    $1

    `bash

    Merge commit

    git merge --no-ff feature/branch

    Squash merge

    git merge --squash feature/branch

    Rebase

    git rebase main feature/branch

    `

    $1

    $1

    `bash

    Tag release

    git tag -a v1.0.0 -m "Release version 1.0.0"

    git push origin v1.0.0

    Create release branch

    git checkout -b release/1.0.0 main

    `

    $1

    `javascript

    // changelog-config.js

    module.exports = {

    preset: 'angular',

    releaseRules: [

    {type: 'feat', release: 'minor'},

    {type: 'fix', release: 'patch'},

    {type: 'docs', release: 'patch'},

    {type: 'breaking', release: 'major'}

    ]

    };

    `

    $1

    $1

    `bash

    #!/bin/sh

    .git/hooks/pre-commit

    Run tests

    npm test

    Run linter

    npm run lint

    Check commit message format

    commit_msg=$(cat "$1")

    if ! echo "$commit_msg" | grep -qE '^(feat|fix|docs|style|refactor|test|chore):'; then

    echo "Invalid commit message format"

    exit 1

    fi

    `

    $1

    `yaml

    GitLab CI configuration

    stages:

    - test

    - build

    - deploy

    test:

    stage: test

    script:

    - npm install

    - npm test

    build:

    stage: build

    script:

    - docker build -t app:$CI_COMMIT_SHA .

    deploy:

    stage: deploy

    script:

    - kubectl apply -f k8s/

    `

    $1

    $1

    `bash

    Good commit messages

    git commit -m "feat: add user authentication system"

    git commit -m "fix: resolve memory leak in worker process"

    git commit -m "docs: update API documentation"

    `

    $1

    `bash

    Clean up old branches

    git branch --merged main | grep -v '^[ ]main$' | xargs git branch -d

    Fetch and prune

    git fetch --prune

    `

    $1

    `yaml

    review_checklist:

    code_quality:

    - No hardcoded values

    - Proper error handling

    - Code follows DRY principle

    security:

    - No sensitive data exposed

    - Input validation

    - Proper authentication

    testing:

    - Unit tests added

    - Integration tests updated

    - Edge cases covered

    ``

    $1

    Effective Git workflows in DevOps require:

    1. Clear branching strategy

    2. Consistent commit practices

    3. Automated testing and CI/CD

    4. Regular code reviews

    5. Proper release management

    Remember to:

  • Keep branches short-lived
  • Write meaningful commit messages
  • Automate repetitive tasks
  • Maintain clean history
  • Document processes
  • $1

    1. [Git Documentation](https://git-scm.com/doc)

    2. [GitHub Flow Guide](https://guides.github.com/introduction/flow/)

    3. [GitLab Flow](https://docs.gitlab.com/ee/topics/gitlab_flow.html)

    4. [Conventional Commits](https://www.conventionalcommits.org/)

    $1

    Here are essential resources for mastering Git workflows:

    1. [Git Documentation](https://git-scm.com/doc) - Official Git documentation

    2. [GitHub Flow Guide](https://guides.github.com/introduction/flow/) - GitHub's workflow guide

    3. [GitLab Flow](https://docs.gitlab.com/ee/topics/gitlab_flow.html) - GitLab's workflow documentation

    4. [Trunk Based Development](https://trunkbaseddevelopment.com/) - Guide to trunk-based development

    5. [Git Branching Strategies](https://www.atlassian.com/git/tutorials/comparing-workflows) - Atlassian's comparison of workflows

    6. [Git Best Practices](https://www.git-tower.com/learn/git/ebook/en/command-line/appendix/best-practices) - Git Tower's best practices guide

    7. [Advanced Git Concepts](https://git-scm.com/book/en/v2) - Pro Git Book

    8. [Git Workflow Tools](https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified) - Git tools and utilities

    9. [Code Review Best Practices](https://google.github.io/eng-practices/review/) - Google's code review guide

    10. [Git Hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) - Automating Git workflows

    These resources provide comprehensive information about Git workflows and best practices.

    Why This Matters

    Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.

    Trade-offs to Consider

    Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.

    When NOT to Use This

    Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.

    Decision Framework

    Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.