Git Versioning: Fundamentals, Branches, Merge Conflicts & Workflows
Git is the world’s leading distributed version control system for software development. It enables efficient collaboration, change tracking, and secure code management.
Git Fundamentals
What is Git?
Git is a distributed version control system (DVCS) designed to simplify collaboration on software projects.
Core Concepts
# Initialize a Git repository
git init
# Clone a repository
git clone https://github.com/user/repository.git
# Show status
git status
# Stage changes
git add datei.txt
git add .
# Commit changes
git commit -m "Description of change"
# Push changes
git push origin main
# Pull changes
git pull origin main
Git Architecture
# Working areas in Git
# Working Directory → Staging Area → Local Repository → Remote Repository
# Working Directory
echo "New content" > datei.txt
# Staging Area (Index)
git add datei.txt
# Local Repository
git commit -m "Commit change"
# Remote Repository
git push origin main
Git Configuration
Initial Setup
# Configure username and email
git config --global user.name "Max Mustermann"
git config --global user.email "max@example.com"
# Set default editor
git config --global core.editor "vim"
# Set default branch name (main instead of master)
git config --global init.defaultBranch main
# Display configuration
git config --list
# Repository-specific configuration
git config user.email "work@example.com"
Git Aliases
# Create useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
# Use aliases
git st # git status
git co main # git checkout main
git br # git branch
git ci -m "Message" # git commit -m "Message"
Branches in Git
Branch Basics
Branches enable parallel development on different features without affecting the main codebase.
# Create a new branch
git branch feature-login
# Switch to a branch
git checkout feature-login
# Create and switch to a branch (combined)
git checkout -b feature-login
# List all branches
git branch
git branch -a # including remote branches
# Delete a branch
git branch -d feature-login # only if merged
git branch -D feature-login # force delete
# Delete a remote branch
git push origin --delete feature-login
Branching Strategies
# Feature branch workflow
# 1. Create a new feature branch from main
git checkout main
git pull origin main
git checkout -b feature-user-authentication
# 2. Work on the feature
git add .
git commit -m "Implement user authentication"
git push origin feature-user-authentication
# 3. Create a pull request and merge
# 4. Delete the branch after merge
git checkout main
git pull origin main
git branch -d feature-user-authentication
Branch Management
# Compare branches
git diff main feature-branch
git log main..feature-branch
# Merge branches
git checkout main
git merge feature-branch
# Rebase a branch
git checkout feature-branch
git rebase main
# Cherry-pick specific commits
git checkout main
git cherry-pick <commit-hash>
# Stash temporary changes
git stash
git stash list
git stash pop
git stash apply
git stash drop
Merge Conflicts
What Are Merge Conflicts?
Merge conflicts occur when Git cannot automatically decide which version of a file to use.
Common Conflict Scenarios
# Example: Two developers modify the same line
# Developer A (branch feature-a)
echo "console.log('Hello from Feature A');" > app.js
git add app.js
git commit -m "Add feature A logging"
# Developer B (branch main)
echo "console.log('Hello from Main');" > app.js
git add app.js
git commit -m "Add main logging"
# Attempt to merge
git checkout main
git merge feature-a
# CONFLICT: Merge conflict in app.js
Resolving Conflicts
# 1. Show conflict status
git status
# 2. Open the file and edit it
# Git marks conflicts with <<<<<<<, =======, >>>>>>>
# app.js after conflict:
# <<<<<<< HEAD
# console.log('Hello from Main');
# =======
# console.log('Hello from Feature A');
# >>>>>>> feature-a
# 3. Resolve manually
# console.log('Hello from Main and Feature A');
# 4. Save the file and mark as resolved
git add app.js
git commit -m "Resolve merge conflict in app.js"
# Alternative: Abort the merge
git merge --abort
Advanced Conflict Resolution
# Use merge tools
git mergetool
# Set conflict resolution strategies
git merge -X theirs feature-branch # prefer their version
git merge -X ours feature-branch # prefer our version
# Octopus merge (merge multiple branches at once)
git merge branch1 branch2 branch3
# Subtree merge (incorporate an external project)
git subtree add --prefix=external-repo https://github.com/user/repo.git main
Git Workflows
1. Centralized Workflow
# Simplest workflow
# Everyone works directly on main
# 1. Make local changes
git add .
git commit -m "Add new feature"
# 2. Push changes
git push origin main
# 3. Resolve conflicts if needed
git pull origin main
# Resolve conflicts
git push origin main
2. Feature Branch Workflow
# Each feature gets its own branch
# 1. Create a feature branch
git checkout main
git pull origin main
git checkout -b feature-user-profile
# 2. Work on the feature
git add .
git commit -m "Add user profile page"
git push origin feature-user-profile
# 3. Create a pull request
# Via GitHub/GitLab UI or CLI
# 4. Review and merge
# Code review by team members
# Merge into main after approval
# 5. Clean up the branch
git checkout main
git pull origin main
git branch -d feature-user-profile
3. Gitflow Workflow
# Gitflow using main, develop, feature, release, and hotfix branches
# 1. Initialize the develop branch
git checkout main
git checkout -b develop
git push origin develop
# 2. Create a feature branch from develop
git checkout develop
git checkout -b feature-shopping-cart
# 3. Merge feature into develop
git checkout develop
git merge feature-shopping-cart
git push origin develop
# 4. Create a release branch
git checkout develop
git checkout -b release/v1.0.0
# 5. Merge release into main and develop
git checkout main
git merge release/v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin main --tags
git checkout develop
git merge release/v1.0.0
git push origin develop
# 6. Create a hotfix from main
git checkout main
git checkout -b hotfix/critical-bug
# Fix the bug
git checkout main
git merge hotfix/critical-bug
git checkout develop
git merge hotfix/critical-bug
4. Forking Workflow
# For open source projects or external contributions
# 1. Fork the repository (via GitHub/GitLab UI)
# 2. Clone your own repository
git clone https://github.com/yourusername/project.git
cd project
# 3. Add the upstream remote
git remote add upstream https://github.com/original/project.git
# 4. Create a feature branch
git checkout -b feature-new-functionality
# 5. Commit your changes
git add .
git commit -m "Add new functionality"
# 6. Push to your own repository
git push origin feature-new-functionality
# 7. Open a pull request to the original repository
# 8. Keep upstream in sync regularly
git fetch upstream
git checkout main
git merge upstream/main
Advanced Git Techniques
Rebase and Interactive Rebase
# Simple rebase
git checkout feature-branch
git rebase main
# Interactive rebase (reorder/edit commits)
git rebase -i HEAD~3
# Interactive rebase options:
# pick: Keep the commit as is
# reword: Change the commit message
# edit: Edit the commit
# squash: Combine with the previous commit
# fixup: Combine with the previous commit (discard message)
# drop: Remove the commit
# Interactive rebase example
# pick 1234567 Add feature A
# squash 2345678 Add feature B
# reword 3456789 Fix typo
# Force push (use with caution!)
git push --force-with-lease origin feature-branch
Git Hooks
# Hooks stored in .git/hooks/
# Pre-commit hook (runs before each commit)
#!/bin/sh
# .git/hooks/pre-commit
echo "Running pre-commit checks..."
npm run lint
npm run test
# Pre-push hook (runs before each push)
#!/bin/sh
# .git/hooks/pre-push
echo "Running pre-push tests..."
npm run test:coverage
# Enable hooks (make executable)
chmod +x .git/hooks/pre-commit
chmod +x .git/hooks/pre-push
# Set up hooks globally
git config --global init.templatedir '~/.git-templates'
Git Submodules
# Add a submodule
git submodule add https://github.com/user/library.git libs/library
# Initialize submodules
git submodule init
git submodule update
# Clone with submodules
git clone --recurse-submodules https://github.com/user/project.git
# Update submodules
git submodule update --remote
# Remove a submodule
git rm --cached libs/library
rm -rf libs/library
git commit -m "Remove submodule"
Best Practices
Commit Messages
# Good commit messages
feat: Add user authentication system
fix: Resolve memory leak in data processing
docs: Update API documentation
style: Format code according to style guide
refactor: Simplify database connection logic
test: Add unit tests for user service
chore: Update dependencies
# Format: <type>(<scope>): <description>
# Types: feat, fix, docs, style, refactor, test, chore
# Detailed commit message
feat(auth): Add OAuth2 integration
- Add Google OAuth provider
- Implement token refresh mechanism
- Add user profile synchronization
Closes #123
Branch Naming Conventions
# Good branch names
feature/user-authentication
bugfix/memory-leak-fix
hotfix/critical-security-patch
release/v1.2.0
docs/update-api-documentation
refactor/database-connection-pool
test/add-unit-tests
# Poor branch names
fix-stuff
new-feature
branch-1
temp
Repository Structure
# .gitignore
# Compiled files
*.class
*.jar
*.war
*.ear
# IDE files
.idea/
.vscode/
*.swp
*.swo
# Dependencies
node_modules/
target/
build/
# Logs
*.log
logs/
# Environment files
.env
.env.local
# .gitattributes
# Text files
*.txt text
*.md text
*.js text
*.java text
# Binary files
*.jpg binary
*.png binary
*.pdf binary
# Line endings
* text=auto eol=lf
Troubleshooting and Recovery
Common Problems
# 1. Commit landed on the wrong branch
git checkout correct-branch
git cherry-pick <wrong-commit-hash>
git checkout wrong-branch
git reset --hard HEAD~1
# 2. Restore lost commits
git reflog
git checkout -b recovery <commit-hash>
# 3. Discard unwanted changes
git reset --hard HEAD
git reset --hard HEAD~1 # Remove last commit
git clean -fd # Remove untracked files
# 4. Remote repository issues
git remote -v
git remote set-url origin https://github.com/user/new-repo.git
# 5. Restore after an accidental force push
git reflog
git reset --hard HEAD@{1}
git push --force-with-lease origin main
Performance Optimization
# Optimize the repository
git gc --prune=now
# Find large files
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sed -n 's/^blob //p' | sort --numeric-sort --key=2 | tail -10
# Use Git LFS for large files
git lfs install
git lfs track "*.psd"
git lfs track "*.zip"
git add .gitattributes
git commit -m "Track large files with Git LFS"
Git in Practice
CI/CD Integration
# .github/workflows/ci.yml
name: CI Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Run linting
run: npm run lint
- name: Build project
run: npm run build
Git for Team Collaboration
# Team guidelines
# 1. Use feature branches
# 2. Require pull requests for code review
# 3. Run automated tests on every push
# 4. Protect main and develop branches
# 5. Prefer rebase over merge for cleaner history
# Example protected branch configuration
# Via GitHub/GitLab:
# - Require pull request reviews before merging
# - Require status checks to pass before merging
# - Require branches to be up to date before merging
# - Include administrators as reviewers
Exam-Relevant Concepts
Essential Git Commands
| Command | Description | Use Case |
|---|---|---|
git init | Initialize a repository | Start a new project |
git clone | Clone a repository | Copy an existing project |
git add | Stage changes | Prepare changes for commit |
git commit | Commit changes | Save local changes |
git push | Upload changes | Sync with remote |
git pull | Download changes | Fetch and merge remote updates |
git branch | Manage branches | Create development branches |
git merge | Merge branches | Integrate changes |
git rebase | Rebase commits | Create linear history |
Typical Exam Tasks
- Explain Git workflows
- Resolve merge conflicts
- Implement branch strategies
- Use advanced Git commands
- Set up CI/CD with Git
Summary
Git is essential for modern software development:
- Distributed system: Every developer has a complete copy
- Branches: Parallel development without conflicts
- Merge: Intelligent integration of changes
- Workflows: Structured team collaboration
Book Recommendation
Level up your Git skills with this comprehensive guide designed for developers and DevOps teams. You’ll find everything from foundational concepts to advanced techniques, plus a practical Git command reference you can reach for in your daily work.
Git: Projektverwaltung für Entwickler und DevOps-Teams. Inkl. Praxistipps und Git-KommandoreferenzThis book is ideal for:
- Beginners who want to learn Git from the ground up
- Experienced developers looking to deepen their expertise
- DevOps teams implementing effective workflows
- Anyone who needs a thorough Git reference
- CI/CD: automating builds and deployments
Good Git management comes down to discipline, clear conventions, and keeping your repository well-maintained.

