DevOps
Git Basics
Master Git fundamentals: workflow, branching, merging, and essential commands for version control in DevOps.
By TechCoder TeamLast updated: 2026-06-02
In a Nutshell
Master Git fundamentals: workflow, branching, merging, and essential commands for version control in DevOps. This hands-on tutorial focuses on practical implementation of git basics concepts.
Git Basics
Git is the most widely used version control system in software development. Understanding Git is essential for any DevOps engineer.
What is Git?
Git is a distributed version control system that tracks changes in source code during software development.
┌─────────────────────────────────────────┐
│ Git Architecture │
├─────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Remote │<--->│ Local │ │
│ │ Repository │ push│ Repository │ │
│ │ (GitHub) │pull │ (.git) │ │
│ └─────────────┘ └──────┬──────┘ │
│ │ │
│ ┌─────────┼─────────┐│
│ │ │ ││
│ Staging Working Local │
│ Index Directory Repo │
│ (git add) (edit) (commit)│
│ │
└─────────────────────────────────────────┘
Git Workflow
The Three States
- Working Directory: Where you edit files
- Staging Area (Index): Where you prepare changes
- Repository (.git): Where commits are stored
Working Dir ──git add──> Staging ──git commit──> Repository
↑ │
└────────────git checkout──────────────────────┘
Essential Git Commands
Setup and Configuration
# Check Git version
git --version
# Configure user
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default editor
git config --global core.editor "vim"
# View configuration
git config --list
git config user.name
# 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
Repository Operations
# Initialize new repository
git init
# Clone existing repository
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder
# Check repository status
git status
git status -s # Short format
# View history
git log
git log --oneline
git log --oneline --graph --all
Making Changes
# Stage files
git add filename # Stage specific file
git add . # Stage all changes
git add -A # Stage all (including deletions)
git add -p # Interactive staging
# Unstage files
git restore --staged filename
git reset HEAD filename
# Discard changes
git restore filename # Discard local changes
git checkout -- filename # Old syntax
# Commit changes
git commit -m "Descriptive message"
git commit -am "Message" # Add and commit (tracked files only)
git commit --amend # Amend last commit
Commit Best Practices
feat: add user authentication
^ ^
│ └─→ Description in imperative mood
│
└─→ Type: feat, fix, docs, style, refactor, test, chore
Types:
- feat: New feature
- fix: Bug fix
- docs: Documentation
- style: Formatting
- refactor: Code restructuring
- test: Adding tests
- chore: Maintenance
Branching and Merging
Understanding Branches
main: A---B---C---D
↑
develop: E---F---G
↑
feature: H---I
Branch Operations
# List branches
git branch
git branch -a # All branches (including remote)
git branch -vv # With tracking info
# Create branch
git branch feature-name
git checkout -b feature-name # Create and switch
git switch -c feature-name # New syntax (Git 2.23+)
# Switch branches
git checkout branch-name
git switch branch-name # New syntax
# Delete branch
git branch -d branch-name # Merged branches only
git branch -D branch-name # Force delete
# Rename branch
git branch -m old-name new-name
Merging
# Merge branch into current
git merge feature-branch
# Merge strategies
git merge --no-ff feature-branch # Always create merge commit
git merge --ff-only feature-branch # Fast-forward only
# Merge with squash (single commit)
git merge --squash feature-branch
git commit -m "Add feature X"
# Abort merge
git merge --abort
Handling Merge Conflicts
# When conflict occurs:
git status # See conflicting files
# Edit files to resolve conflicts
# Look for <<<<<<< HEAD markers
# After resolving:
git add resolved-file
git commit # Creates merge commit
Conflict Markers:
<<<<<<< HEAD
Your changes here
=======
Incoming changes here
>>>>>>> branch-name
Working with Remotes
Remote Operations
# List remotes
git remote -v
# Add remote
git remote add origin https://github.com/user/repo.git
# Remove remote
git remote remove origin
# Rename remote
git remote rename old-name new-name
# Fetch changes (no merge)
git fetch origin
git fetch --all
# Pull changes (fetch + merge)
git pull origin main
git pull --rebase origin main
# Push changes
git push origin main
git push -u origin main # Set upstream
git push origin --delete branch-name # Delete remote branch
# Sync with remote
git fetch origin
git rebase origin/main
Tracking Branches
# Push local branch to remote
git push -u origin feature-branch
# Set upstream for existing branch
git branch --set-upstream-to=origin/feature-branch
# Pull updates for current branch
git pull
# Push to current upstream
git push
Inspecting History
Viewing Commits
# Basic log
git log
git log --oneline
git log --oneline --graph
# Filter log
git log --since="2 weeks ago"
git log --author="John"
git log --grep="fix"
# File history
git log --follow filename
git log -p filename # With patches
# Statistics
git log --stat
git log --shortstat
# Pretty formats
git log --pretty=format:"%h - %an, %ar : %s"
Viewing Changes
# Working directory vs staging
git diff
# Staging vs last commit
git diff --staged
git diff --cached
# Specific commit
git show commit-hash
# Compare branches
git diff main..develop
# File at specific commit
git show commit-hash:filename
Finding Bugs
# Who changed what
git blame filename
# Search commits
git log -S "search-string"
git log -G "regex-pattern"
# Binary search for bugs
git bisect start
git bisect bad # Current is bad
git bisect good v1.0 # v1.0 was good
git bisect reset # Finish
Undoing Changes
Different Scenarios
# Unstage files (keep changes)
git restore --staged filename
git reset HEAD filename
# Discard local changes
git restore filename
git checkout -- filename
# Modify last commit
git commit --amend -m "New message"
git commit --amend --no-edit # Keep message, add changes
# Undo commits (keep changes)
git reset --soft HEAD~1
# Undo commits (discard changes)
git reset --hard HEAD~1
# Undo merge
git reset --hard ORIG_HEAD
# Revert commit (create new commit)
git revert commit-hash
Reset Modes
--soft: Move HEAD, keep staging and working dir
--mixed: Move HEAD, unstage, keep working dir (default)
--hard: Move HEAD, unstage, reset working dir
Stashing
Save Work Temporarily
# Stash changes
git stash
git stash push -m "message"
# List stashes
git stash list
# Apply stash (keep in list)
git stash apply
git stash apply stash@{2}
# Pop stash (remove from list)
git stash pop
# Drop stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
# Create branch from stash
git stash branch new-branch-name
Tags
Version Markers
# List tags
git tag
git tag -l "v1.*"
# Create lightweight tag
git tag v1.0
# Create annotated tag
git tag -a v1.0 -m "Version 1.0"
# Tag specific commit
git tag -a v1.0 commit-hash -m "Version 1.0"
# Push tags to remote
git push origin v1.0
git push origin --tags
# Delete tag
git tag -d v1.0
git push origin --delete v1.0
# Checkout tag
git checkout v1.0 # Detached HEAD
git checkout -b version1 v1.0 # With branch
Gitignore
Pattern Examples
# Compiled files
*.exe
*.dll
*.so
*.class
target/
build/
dist/
# Dependencies
node_modules/
vendor/
# IDE
.idea/
.vscode/
*.iml
# OS files
.DS_Store
Thumbs.db
# Logs and databases
*.log
*.sqlite
# Environment
.env
.env.local
config.ini
# But track this specific file
!important.log
# Global gitignore
git config --global core.excludesfile ~/.gitignore_global
# Check what's ignored
git check-ignore -v filename
# Force add ignored file
git add -f filename
Quiz
Quiz
Question 1 of 5What command stages all changes including new files and deletions?
git add .
git add -A
git stage all
git commit -a
Next Steps
Now let's explore advanced Git concepts including rebase, hooks, and working with GitHub/GitLab.