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 "[email protected]"
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 [email protected]:user/repo.git
# Clone with specific branch
git clone -b feature-branch https://github.com/user/repo.gitStatus 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.txtBranch 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-branchWorking 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 -vPush 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 originRemote 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-branchAdvanced 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 dropTagging
# 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 --tagsSubmodules
# 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 mainGit Workflows
Gitflow
- Master/Main: Production code
- Develop: Integration branch
- Feature branches: New features
- Release branches: Prepare releases
- Hotfix branches: Critical fixes
GitHub Flow
- Main branch: Production-ready code
- Feature branches: New features
- Pull requests: Review changes
- Merge: Integrate to main
Trunk-Based Development
- Single main branch
- Short-lived feature branches
- Continuous integration
- 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
fiServer-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/zzzGit 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
.venvResources
Documentation
Tools
- GitHub - Code hosting
- GitLab - CI/CD platform
- Bitbucket - Atlassian’s Git hosting
- VS Code - IDE with Git integration
- GitHub Desktop - GUI for Git