Git — Version Control et Déploiement

Status: Basic Tags: Git VersionControl DevOps Deployment Created: 2026-08-09 Related: Docker, Ubuntu, CD

Introduction

Git is a distributed version control system for tracking changes in source code during software development. It enables collaboration among developers and maintains a complete history of all changes.


Installation

Install Git on Ubuntu

# Install Git
sudo apt update
sudo apt install git
 
# Configure Git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "vim"

Basic Concepts

Repository

  • Definition: A project’s directory with a hidden .git folder
  • Local: On your computer
  • Remote: On GitHub, GitLab, etc.

Commits

  • Definition: A snapshot of changes at a specific point
  • Structure: Contains changes, author, timestamp, message

Branches

  • Definition: Independent lines of development
  • Master/Main: Default branch, usually protected
  • Feature branches: For new features
  • Bugfix branches: For bug fixes

Essential Commands

Initialize and Clone

# Initialize a new repository
git init
 
# Clone an existing repository
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git
 
# Clone with specific branch
git clone -b feature-branch https://github.com/user/repo.git

Status and Log

# Check status
git status
 
# View commit history
git log
git log --oneline
git log --graph --all
 
# View specific commit
git show <commit-hash>

Add and Commit

# Add file to staging
git add file.txt
 
# Add all files
git add .
 
# Commit changes
git commit -m "Add feature X"
 
# Commit with multiple files
git commit -m "Update files" file1.txt file2.txt

Branch Operations

# List branches
git branch
 
# Create new branch
git branch feature-branch
 
# Switch to branch
git checkout feature-branch
 
# Create and switch to new branch
git checkout -b feature-branch
 
# Merge branch
git checkout main
git merge feature-branch
 
# Delete branch
git branch -d feature-branch

Working with Remote Repositories

Remote Setup

# Add remote repository
git remote add origin https://github.com/user/repo.git
 
# Set remote URL
git remote set-url origin https://github.com/user/repo.git
 
# List remotes
git remote -v

Push and Pull

# Push to remote
git push origin main
 
# Push current branch
git push
 
# Pull changes
git pull origin main
 
# Pull with rebase
git pull --rebase origin main
 
# Fetch changes
git fetch origin

Remote Branches

# Push current branch to remote
git push -u origin feature-branch
 
# List remote branches
git branch -r
 
# Delete remote branch
git push origin --delete feature-branch

Advanced Features

Stash

# Save changes
git stash save "Work in progress"
 
# List stashes
git stash list
 
# Apply stash
git stash apply
 
# Apply and remove stash
git stash pop
 
# Drop stash
git stash drop

Tagging

# Create lightweight tag
git tag v1.0.0
 
# Create annotated tag
git tag -a v1.0.0 -m "Release 1.0.0"
 
# Push tags
git push origin --tags

Submodules

# Add submodule
git submodule add https://github.com/user/repo.git path/to/submodule
 
# Update submodules
git submodule update --init --recursive
 
# Update all submodules
git submodule foreach git pull origin main

Git Workflows

Gitflow

  1. Master/Main: Production code
  2. Develop: Integration branch
  3. Feature branches: New features
  4. Release branches: Prepare releases
  5. Hotfix branches: Critical fixes

GitHub Flow

  1. Main branch: Production-ready code
  2. Feature branches: New features
  3. Pull requests: Review changes
  4. Merge: Integrate to main

Trunk-Based Development

  1. Single main branch
  2. Short-lived feature branches
  3. Continuous integration
  4. Feature flags

Git Hooks

Client-Side Hooks

#!/bin/bash
# .git/hooks/pre-commit
 
# Run tests before commit
python -m pytest tests/ -v
if [ $? -ne 0 ]; then
    echo "Tests failed! Commit aborted."
    exit 1
fi

Server-Side Hooks

#!/bin/bash
# .git/hooks/post-receive
 
# Notify team of pushes
curl -X POST -H 'Content-type: application/json' \
  --data '{"text":"New commit pushed to repository"}' \
  https://hooks.slack.com/services/xxx/yyy/zzz

Git Aliases

Useful Aliases

# Shorten commands
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.last "log -1 HEAD"

Git with Docker

Dockerfile with Git

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]

.dockerignore with Git

.git
.gitignore
*.md
__pycache__
*.pyc
.env
.venv

Resources

Documentation

Tools