Git Version Control Basics
Git Version Control Basics
Git is the most popular distributed version control system. In IoT development, it is used to manage firmware code, hardware design files, and project documentation. This section systematically introduces Git’s core concepts and basic operations, helping readers quickly get started with version control.
Git Core Concepts
Section titled “Git Core Concepts”Git’s three working areas:
Working Directory → Staging Area → Local Repository git add git commit- Working Directory: the files you are currently editing
- Staging Area: a collection of changes ready to be committed
- Local Repository: where Git stores all historical versions
Git Basic Workflow
Section titled “Git Basic Workflow”Init & Clone
Section titled “Init & Clone”# Initialize a new local repositorygit init
# Clone an existing remote repositorygit clone https://github.com/your-org/iot-project.gitDaily Development Trilogy: Add → Commit → Push
Section titled “Daily Development Trilogy: Add → Commit → Push”# 1. Check current statusgit status
# 2. Stage changesgit add main.c # Add a single filegit add src/ # Add an entire directorygit add -p # Interactive staging (recommended)
# 3. Commit to local repositorygit commit -m "feat: add MQTT reconnection mechanism"
# 4. Push to remote repositorygit push origin mainCommit Message Convention: Use Conventional Commits format
feat:New featurefix:Bug fixdocs:Documentation changesrefactor:Code refactoringtest:Testingchore:Build/tooling changes
Viewing History & Diffs
Section titled “Viewing History & Diffs”# View commit historygit loggit log --oneline # Compact modegit log --graph # Graphical branch view
# View unstaged changesgit diff
# View staged changesgit diff --cachedPulling Remote Updates
Section titled “Pulling Remote Updates”# Pull latest remote code and mergegit pull origin main
# Fetch without merging (review first)git fetch origingit log origin/main..HEAD # See what's ahead locallygit merge origin/main # Merge after reviewBranch Management
Section titled “Branch Management”Branches allow teams to develop in parallel.
Basic Branch Operations
Section titled “Basic Branch Operations”# List all branches (* marks current)git branch
# Create a new branchgit branch feature/temperature-sensor
# Switch branchesgit checkout feature/temperature-sensor
# Create and switch (one step)git checkout -b feature/humidity-sensor
# Delete branch (after merge)git branch -d feature/temperature-sensorCommon Branch Strategy
Section titled “Common Branch Strategy”Simplified Git Flow:
main ────────●────────────●──────── \ /feature/xyz ●──●──●──● \fix/abc ●──●- main: Stable release-ready branch
- feature/*: Feature branches, merged via PR
- fix/*: Bug fix branches
Merging & Conflict Resolution
Section titled “Merging & Conflict Resolution”# Merge feature into current branchgit merge feature/temperature-sensor
# Rebase (linear history, suitable for personal branches)git checkout feature/temperature-sensorgit rebase mainWhen two branches modify the same file region, Git reports a conflict:
# Conflict markers example<<<<<<< HEADSerial.println("Temperature: 25.5°C");=======Serial.println("Temperature: 26.3°C");>>>>>>> feature/calibration
# Resolve: edit file, keep correct version, remove markersSerial.println("Temperature: 26.3°C"); # Use calibrated value
# Mark as resolved and commitgit add sensor.cppgit commit -m "fix: resolve temperature calibration conflict"Remote Repository Collaboration
Section titled “Remote Repository Collaboration”Adding a Remote
Section titled “Adding a Remote”git remote add origin https://github.com/your-org/iot-project.gitgit remote -vPull Request Workflow
Section titled “Pull Request Workflow”# Push local branch to remotegit push origin feature/temperature-sensor
# On GitHub/GitLab, create a PR including:# - Summary of changes# - Related Issue number# - Testing methodology# - Scope of impact.gitignore Configuration
Section titled “.gitignore Configuration”For IoT projects, exclude these files:
# Build artifactsbuild/cmake-build-*/*.o*.elf*.bin*.hex
# IDE config.vscode/.idea/*.swp*.swo
# Environment (do not commit secrets).env*.env.local
# System files.DS_StoreThumbs.db
# Python cache__pycache__/*.pycCommit the .gitignore:
git add .gitignoregit commit -m "chore: add .gitignore"Undoing Changes
Section titled “Undoing Changes”# Discard working directory changes (use with caution)git checkout -- main.c
# Unstage a filegit reset HEAD main.c
# Amend last commit messagegit commit --amend -m "new message"
# Hard reset to a commit (discard history)git reset --hard <commit-hash>
# Soft reset (keep changes in working directory)git reset --soft <commit-hash>Quick Command Reference
Section titled “Quick Command Reference”| Command | Purpose | Common Flags |
|---|---|---|
git init | Initialize a repo | — |
git clone <url> | Clone a remote repo | --recursive |
git status | Check workspace status | -s (short) |
git add <file> | Stage changes | . (all), -p (interactive) |
git commit | Commit staged changes | -m (message), --amend |
git push | Push to remote | origin main, --force (risky) |
git pull | Pull and merge | --rebase |
git branch | Manage branches | -d (delete), -a (all) |
git checkout | Switch branch/file | -b (create + switch) |
git merge <b> | Merge a branch | — |
git log | View history | --oneline, --graph |
git diff | View differences | --cached (staged diff) |
Next: After mastering Git basics, the next section covers how to initialize a project repository on GitHub and configure the team collaboration environment.
We provide ESP32 ODM design-to-manufacturing services. From prototype to production — the team behind this tutorial can build it with you.
Talk to us →