#Git Fundamentals
Git is the industry standard for version control. Every DevOps engineer must master Git.
#📋 Table of Contents
- Git Basics
- Working with Repositories
- Commits and History
- Branching
- Remote Repositories
- Undoing Changes
- Git Configuration
#Git Basics
#Installing Git
bash
1# Ubuntu/Debian
2sudo apt install git
3
4# macOS
5brew install git
6
7# Windows
8choco install git
9
10# Verify
11git --version#Initial Configuration
bash
1# Set identity
2git config --global user.name "Your Name"
3git config --global user.email "your.email@example.com"
4
5# Default branch name
6git config --global init.defaultBranch main
7
8# Editor
9git config --global core.editor "vim"
10
11# View config
12git config --list#Working with Repositories
#Creating a Repository
bash
1# Initialize new repository
2mkdir my-project
3cd my-project
4git init
5
6# Clone existing repository
7git clone https://github.com/user/repo.git
8git clone git@github.com:user/repo.git # SSH#Basic Workflow
bash
1# Check status
2git status
3
4# Stage files
5git add file.txt # Single file
6git add . # All files
7git add *.py # Pattern
8
9# Commit
10git commit -m "Add feature X"
11
12# Stage and commit together
13git commit -am "Update existing files"#Commits and History
#Creating Good Commits
bash
1# Good commit message format
2git commit -m "feat: add user authentication
3
4- Implement JWT token generation
5- Add login/logout endpoints
6- Include password hashing"#Conventional Commits
| Type | Description |
|---|---|
feat | New feature |
fix | Bug fix |
docs | Documentation |
style | Formatting |
refactor | Code refactoring |
test | Adding tests |
chore | Maintenance |
#Viewing History
bash
1# View log
2git log
3git log --oneline
4git log --graph --oneline --all
5
6# Show specific commit
7git show abc1234
8
9# View changes in file
10git log -p file.txt
11
12# Search commits
13git log --grep="feature"
14git log --author="John"#Branching
#Branch Operations
bash
1# List branches
2git branch
3git branch -a # Include remote
4
5# Create branch
6git branch feature-login
7
8# Switch branch
9git checkout feature-login
10git switch feature-login # Modern syntax
11
12# Create and switch
13git checkout -b feature-login
14git switch -c feature-login
15
16# Delete branch
17git branch -d feature-login # Safe delete
18git branch -D feature-login # Force delete#Merging
bash
1# Merge branch into current
2git checkout main
3git merge feature-login
4
5# Merge with no fast-forward (creates merge commit)
6git merge --no-ff feature-login
7
8# Abort merge
9git merge --abort#Rebasing
bash
1# Rebase current branch onto main
2git checkout feature-login
3git rebase main
4
5# Interactive rebase (squash commits)
6git rebase -i HEAD~3
7
8# Continue after resolving conflicts
9git rebase --continue
10
11# Abort rebase
12git rebase --abort#Remote Repositories
#Working with Remotes
bash
1# List remotes
2git remote -v
3
4# Add remote
5git remote add origin https://github.com/user/repo.git
6
7# Change remote URL
8git remote set-url origin new-url
9
10# Remove remote
11git remote remove origin#Push and Pull
bash
1# Push to remote
2git push origin main
3git push -u origin main # Set upstream
4
5# Push all branches
6git push --all
7
8# Pull from remote
9git pull origin main
10
11# Fetch without merging
12git fetch origin
13git fetch --all#Pull Requests Workflow
bash
1# 1. Create feature branch
2git checkout -b feature-x
3
4# 2. Make changes and commit
5git add .
6git commit -m "feat: implement feature X"
7
8# 3. Push branch
9git push -u origin feature-x
10
11# 4. Create pull request on GitHub/GitLab
12
13# 5. After merge, cleanup
14git checkout main
15git pull
16git branch -d feature-x#Undoing Changes
#Unstaging Files
bash
# Unstage file
git restore --staged file.txt
git reset HEAD file.txt # Old syntax#Discarding Changes
bash
1# Discard working directory changes
2git restore file.txt
3git checkout -- file.txt # Old syntax
4
5# Discard all changes
6git restore .#Reverting Commits
bash
1# Create new commit that undoes previous
2git revert abc1234
3
4# Revert without committing
5git revert --no-commit abc1234#Resetting
bash
1# Soft reset (keep changes staged)
2git reset --soft HEAD~1
3
4# Mixed reset (keep changes unstaged)
5git reset HEAD~1
6
7# Hard reset (discard changes)
8git reset --hard HEAD~1
9
10# Reset to remote
11git reset --hard origin/main#Git Configuration
#SSH Keys
bash
1# Generate SSH key
2ssh-keygen -t ed25519 -C "your.email@example.com"
3
4# View public key
5cat ~/.ssh/id_ed25519.pub
6
7# Test connection
8ssh -T git@github.com#.gitignore
gitignore
1# Dependencies
2node_modules/
3vendor/
4__pycache__/
5
6# Build output
7dist/
8build/
9*.pyc
10
11# IDE
12.idea/
13.vscode/
14*.swp
15
16# Environment
17.env
18.env.local
19*.log
20
21# OS
22.DS_Store
23Thumbs.db#Aliases
bash
1git config --global alias.co checkout
2git config --global alias.br branch
3git config --global alias.ci commit
4git config --global alias.st status
5git config --global alias.lg "log --oneline --graph --all"#Summary
| Action | Command |
|---|---|
| Initialize | git init |
| Clone | git clone url |
| Stage | git add . |
| Commit | git commit -m "msg" |
| Push | git push origin main |
| Pull | git pull |
| Branch | git checkout -b name |
| Merge | git merge branch |
| Status | git status |
| Log | git log --oneline |
[!TIP] Pro Tip: Use
git stashto temporarily save changes when switching branches!