blog / git-workflow-best-practices

Git Workflow: So arbeite ich täglich mit Git

Der Arbeit erleichternder Workflow

Optimierter Workflow:

  • ✅ Saubere History erzeugt
  • ✅ Merge-Konflikte minimiert
  • ✅ Code Reviews vereinfacht
  • ✅ Einfaches Rollback ermöglicht

So arbeite ich mit git.

1. Branch Strategy: Einfach aber effektiv

main (production)
  ├── develop (integration)
   ├── feature/user-authentication
   ├── feature/payment-integration
   └── bugfix/login-error
  └── hotfix/critical-security-patch

Branch Naming Conventions:

# Features
feature/user-dashboard
feature/api-integration

# Bugfixes
bugfix/login-validation
bugfix/memory-leak

# Hotfixes (für production)
hotfix/security-patch
hotfix/payment-error

# Refactoring
refactor/auth-service
refactor/database-queries

# Chores (Maintenance)
chore/update-dependencies
chore/add-tests

2. Meine Commit-Konventionen

Conventional Commits Format:

type(scope): subject

body (optional)

footer (optional)

Types die ich nutze:

feat:     Neue Features
fix:      Bug Fixes
docs:     Dokumentation
style:    Code Formatting (kein Logik-Change)
refactor: Code Refactoring
test:     Tests hinzufügen/ändern
chore:    Build, Dependencies, etc.
perf:     Performance Improvements

Beispiele:

# Good commits :)
feat(auth): add JWT token refresh mechanism

fix(api): prevent duplicate user registration
- Add unique constraint on email
- Update error handling

refactor(database): optimize query performance
- Add indexes on frequently queried columns
- Replace N+1 queries with joins
Closes #123

# Bad commits :(
"fixed stuff"
"WIP"
"asdf"
"updates"

3. Mein Daily Git Workflow

Morgens - Projekt starten:

# 1. Neueste Changes holen
git checkout develop
git pull origin develop

# 2. Neuen Feature Branch erstellen
git checkout -b feature/user-dashboard

# 3. Arbeite an deinem Feature
# ... code code code ...

Während der Arbeit:

# Commit früh und oft (lokal)
git add src/components/Dashboard.tsx
git commit -m "feat(dashboard): add user stats widget"

git add src/components/Chart.tsx
git commit -m "feat(dashboard): add activity chart"

# Status checken
git status
git log --oneline

# Diff ansehen vor dem Commit
git diff
git diff --staged

Vor dem Push - Cleanup:

# Interactive Rebase für saubere History
git rebase -i HEAD~3

# Im Editor:
# pick abc123 feat(dashboard): add user stats widget
# squash def456 fix typo
# squash ghi789 fix formatting

# Resultat: 1 sauberer Commit statt 3

4. Pull Request Workflow

Feature fertig - Zeit für PR:

# 1. Develop in deinen Branch mergen
git checkout feature/user-dashboard
git fetch origin
git rebase origin/develop

# Falls Konflikte:
# - Konflikte lösen in Files
git add .
git rebase --continue

# 2. Pushen
git push origin feature/user-dashboard

# 3. PR auf GitHub/GitLab erstellen

PR Description Template:

## What does this PR do?
Brief description of the changes

## How to test?
1. Checkout branch
2. Run `npm install`
3. Run `npm test`
4. Verify feature X works

## Screenshots
[Add screenshots if UI changes]

## Checklist
- [ ] Tests added/updated
- [ ] Documentation updated
- [ ] No console.logs left
- [ ] Passes CI

5. Code Review - Als Reviewer

# PR lokal auschecken und testen
gh pr checkout 123  # GitHub CLI

# Oder manuell:
git fetch origin pull/123/head:pr-123
git checkout pr-123

# Testen und reviewen
npm install
npm test
npm run dev

# Inline Comments auf GitHub/GitLab
# Approve oder Request Changes

6. Nach dem Merge - Cleanup

# Branch lokal löschen
git checkout develop
git branch -d feature/user-dashboard

# Remote Branch löschen (wenn nicht automatisch)
git push origin --delete feature/user-dashboard

# Alte Branches aufräumen
git fetch --prune
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -d

7. Häufige Situationen & Lösungen

”Oh no, ich habe auf dem falschen Branch committed!"

# Auf main statt feature committed
git checkout main
git log  # Finde den Commit Hash

# Commit auf richtigen Branch verschieben
git checkout feature/my-feature
git cherry-pick abc123

# Commit von main entfernen
git checkout main
git reset --hard HEAD~1

"Ich will meinen letzten Commit ändern"

# Files hinzufügen die vergessen wurden
git add forgotten-file.js
git commit --amend --no-edit

# Commit Message ändern
git commit --amend -m "feat(auth): add better description"

# NICHT amenden wenn schon gepusht!

"Ich will einen Commit rückgängig machen"

# Letzter Commit (behält Changes)
git reset --soft HEAD~1

# Letzter Commit (verwirft Changes)
git reset --hard HEAD~1

# Bereits gepusht? Revert nutzen:
git revert abc123  # Erstellt neuen Commit der Changes rückgängig macht

"Merge Konflikte lösen”

git rebase develop

# <<<<<<< HEAD
# mein code
# =======
# ihr code
# >>>>>>> develop

# 1. Konflikte manuell lösen
# 2. git add .
# 3. git rebase --continue

# Rebase abbrechen falls zu komplex:
git rebase --abort

8. Git Aliases für Produktivität

# .gitconfig
[alias]
  # Status
  st = status
  s = status -sb

  # Commits
  cm = commit -m
  ca = commit --amend
  cane = commit --amend --no-edit

  # Branches
  br = branch
  co = checkout
  cob = checkout -b

  # Logs
  lg = log --oneline --graph --decorate
  ll = log --pretty=format:'%C(yellow)%h%Creset %C(blue)%ad%Creset %s %C(green)%an%Creset' --date=short
  last = log -1 HEAD

  # Diffs
  df = diff
  dfs = diff --staged

  # Undo
  undo = reset --soft HEAD~1
  unstage = reset HEAD --

  # Cleanup
  cleanup = "!git branch --merged | grep -v '\\*\\|main\\|develop' | xargs -n 1 git branch -d"

Usage:

git st          # statt git status
git cob feature/new-thing  # statt git checkout -b
git lg          # beautiful log
git cleanup     # remove merged branches

9. .gitignore Best Practices

# Node.js
node_modules/
npm-debug.log
.env
.env.local

# Build outputs
dist/
build/
*.log

# IDE
.vscode/
.idea/
*.swp

# OS
.DS_Store
Thumbs.db

# Secrets - NIEMALS committen!
*.pem
*.key
secrets/
credentials.json

Global .gitignore:

# ~/.gitignore_global
.DS_Store
.vscode/
*.log

# Aktivieren:
git config --global core.excludesfile ~/.gitignore_global

10. Git Hooks für Qualität

Pre-commit Hook:

# .git/hooks/pre-commit
#!/bin/sh

# Run linter
npm run lint || exit 1

# Run tests
npm test || exit 1

# Prevent commits to main
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" = "main" ]; then
  echo "You can't commit directly to main branch"
  exit 1
fi

Mit Husky automatisieren:

npm install -D husky lint-staged

# package.json
{
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged",
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  "lint-staged": {
    "*.{js,ts,tsx}": [
      "eslint --fix",
      "prettier --write"
    ]
  }
}

11. Nützliche Git Commands

# Stash - Changes temporär speichern
git stash
git stash pop
git stash list
git stash apply stash@{0}

# Einzelne Files aus anderem Branch holen
git checkout develop -- src/utils/helper.js

# Alle Changes eines Authors sehen
git log --author="Max"

# Files finden die gelöscht wurden
git log --diff-filter=D --summary

# Wer hat diese Zeile geschrieben?
git blame src/app.js

# Finde Bug-einführenden Commit
git bisect start
git bisect bad
git bisect good abc123

Best Practices Zusammenfassung

:) DO:

  • Committe früh und oft (lokal)
  • Schreibe aussagekräftige Commit Messages
  • Rebase vor dem Push für saubere History
  • Teste vor dem Push
  • Erstelle PRs für jedes Feature
  • Code Review ernst nehmen

:( DON’T:

  • Direct Push zu main/develop
  • “WIP” commits pushen
  • Secrets committen
  • Große Binary Files committen
  • Force Push zu shared branches
  • Merge ohne Tests

Mein Cheatsheet

# Start des Tages
git pull
git checkout -b feature/xyz

# Während der Arbeit
git add .
git commit -m "feat: add xyz"

# Vor dem Push
git rebase -i HEAD~n  # Cleanup
git rebase origin/develop  # Update

# Push und PR
git push origin feature/xyz
# Create PR on GitHub

# Nach Merge
git checkout develop
git pull
git branch -d feature/xyz

Tools die ich nutze

  • GitHub CLI (gh) - PRs von der CLI
  • GitKraken/Tower - Visuelles Git Tool
  • VS Code GitLens - Inline blame & history
  • Conventional Commits Extension - Enforce commit format

Fazit

Ein guter Git Workflow:

  • Macht Code Reviews einfacher
  • Verhindert Chaos in der History
  • Ermöglicht einfaches Rollback
  • Verbessert Team-Kollaboration

Starte klein, baue Gewohnheiten auf, und dein Future-Self wird es dir danken!


Was ist dein Git Workflow? Teile deine Tipps!