๐ Git Version Control and Workflows on AlmaLinux: Master Your Code
โWho broke the code?!โ ๐ฑ That was me, every Monday morning before Git! Lost work, overwritten files, merge nightmares - sound familiar? Then I discovered Git workflows and everything changed. Now our team of 20 developers ships features daily without conflicts! Today Iโm showing you how to master Git on AlmaLinux and implement professional workflows. Say goodbye to code chaos! ๐ฏ
๐ค Why Git is Non-Negotiable
Git isnโt optional anymore - itโs essential! Hereโs why everyone uses it:
- ๐ Complete history - Every change tracked forever
- ๐ Branching magic - Work on features without breaking main
- ๐ฅ Perfect collaboration - Multiple developers, zero conflicts
- โฐ Time travel - Undo mistakes instantly
- ๐ Distributed - Every clone is a full backup
- ๐ Industry standard - GitHub, GitLab, Bitbucket
True story: We lost 3 weeks of work to a hard drive crash. Never again! Now with Git, even if our servers explode, we have 20+ full backups! ๐ช
๐ฏ What You Need
Before we version control everything, ensure you have:
- โ AlmaLinux server or workstation
- โ Terminal access
- โ Internet connection for remote repos
- โ 30 minutes to become a Git master
- โ Code to version (anything works!)
- โ Coffee (Git needs focus! โ)
๐ Step 1: Install and Configure Git
Letโs get Git ready for action!
Install Git
# Install Git and useful tools
sudo dnf install -y git git-lfs gitk git-gui
# Check version
git --version
# Install additional tools
sudo dnf install -y tig # Text-mode interface for Git
sudo dnf install -y git-extras # Extra Git commands
sudo dnf install -y git-flow # Git Flow workflow
# Install diff/merge tools
sudo dnf install -y meld kdiff3 vim
Configure Git Identity
# Set your identity (REQUIRED!)
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
# Configure editor
git config --global core.editor vim
# Configure colors
git config --global color.ui auto
git config --global color.branch auto
git config --global color.diff auto
git config --global color.status auto
# Useful aliases
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.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# Configure line endings
git config --global core.autocrlf input # Linux/Mac
# git config --global core.autocrlf true # Windows
# Default branch name
git config --global init.defaultBranch main
# Configure merge tool
git config --global merge.tool meld
git config --global mergetool.meld.path /usr/bin/meld
# View all settings
git config --list
SSH Key Setup for GitHub/GitLab
# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"
# Press Enter for default location
# Enter passphrase (optional but recommended)
# Start SSH agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Copy public key
cat ~/.ssh/id_ed25519.pub
# Add this to GitHub/GitLab SSH keys
# Test connection
ssh -T [email protected]
ssh -T [email protected]
# Configure SSH for multiple accounts
cat > ~/.ssh/config << 'EOF'
# Personal GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Work GitLab
Host gitlab-work
HostName gitlab.company.com
User git
IdentityFile ~/.ssh/id_rsa_work
# Personal GitLab
Host gitlab-personal
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
EOF
๐ง Step 2: Git Basics and Best Practices
Master the essential commands! ๐ฎ
Initialize and Clone Repositories
# Initialize new repository
mkdir myproject && cd myproject
git init
# Clone existing repository
git clone https://github.com/user/repo.git
git clone [email protected]:user/repo.git # SSH
# Clone with specific branch
git clone -b develop https://github.com/user/repo.git
# Shallow clone (faster for large repos)
git clone --depth 1 https://github.com/user/repo.git
Basic Workflow
# Check status
git status
git status -s # Short format
# Add files
git add file.txt # Specific file
git add . # All files
git add -A # All files including deletions
git add -p # Interactive staging
# Commit changes
git commit -m "Add new feature"
git commit -am "Fix bug" # Add and commit
git commit --amend # Modify last commit
# View history
git log
git log --oneline
git log --graph --oneline --all
git log -p # Show patches
git log --stat # Show statistics
# View differences
git diff # Working directory vs staging
git diff --staged # Staging vs last commit
git diff HEAD # Working directory vs last commit
git diff branch1..branch2 # Between branches
# Undo changes
git checkout -- file.txt # Discard changes
git reset HEAD file.txt # Unstage
git reset --soft HEAD~1 # Undo last commit, keep changes
git reset --hard HEAD~1 # Undo last commit, discard changes
git revert commit-hash # Create new commit that undoes
Branch Management
# List branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
# Create branch
git branch feature-xyz
git checkout -b feature-xyz # Create and switch
# Switch branches
git checkout main
git switch develop # Newer command
# Merge branches
git checkout main
git merge feature-xyz
git merge --no-ff feature-xyz # No fast-forward
# Delete branch
git branch -d feature-xyz # Safe delete
git branch -D feature-xyz # Force delete
git push origin --delete feature-xyz # Delete remote
# Rename branch
git branch -m old-name new-name
๐ Step 3: Professional Git Workflows
Implement team workflows! ๐ฅ
Git Flow Workflow
# Initialize Git Flow
git flow init
# Start new feature
git flow feature start new-feature
# Work on feature...
git add .
git commit -m "Implement new feature"
# Finish feature
git flow feature finish new-feature
# Start release
git flow release start 1.0.0
# Prepare release...
git flow release finish 1.0.0
# Hotfix
git flow hotfix start fix-critical-bug
# Fix bug...
git flow hotfix finish fix-critical-bug
# Manual Git Flow (without extension)
# Main branches: main (production) and develop
# Feature branch
git checkout -b feature/user-auth develop
# Work on feature
git checkout develop
git merge --no-ff feature/user-auth
git branch -d feature/user-auth
# Release branch
git checkout -b release/1.0.0 develop
# Prepare release
git checkout main
git merge --no-ff release/1.0.0
git tag -a v1.0.0 -m "Version 1.0.0"
git checkout develop
git merge --no-ff release/1.0.0
GitHub Flow (Simpler)
# 1. Create branch from main
git checkout -b feature-xyz main
# 2. Make changes and commit
git add .
git commit -m "Add new feature"
# 3. Push to remote
git push -u origin feature-xyz
# 4. Create Pull Request (on GitHub/GitLab)
# 5. After review and approval, merge to main
git checkout main
git pull origin main
git merge feature-xyz
git push origin main
# 6. Delete feature branch
git branch -d feature-xyz
git push origin --delete feature-xyz
GitLab Flow
# Environment branches
# main -> staging -> production
# Feature development
git checkout -b feature/new-feature main
# Develop feature
git push -u origin feature/new-feature
# Merge to main (development)
git checkout main
git merge feature/new-feature
# Deploy to staging
git checkout staging
git merge main
git push origin staging
# Deploy to production
git checkout production
git merge staging
git tag -a v1.0.0 -m "Release v1.0.0"
git push origin production --tags
โ Step 4: Advanced Git Techniques
Level up your Git game! ๐
Interactive Rebase
# Clean up commit history
git rebase -i HEAD~3
# In editor, you can:
# pick = use commit
# reword = change message
# edit = stop for amending
# squash = combine with previous
# fixup = like squash but discard message
# drop = remove commit
# Example: squash last 3 commits
git rebase -i HEAD~3
# Change 'pick' to 'squash' for commits 2 and 3
# Save and write new commit message
Cherry-Pick
# Apply specific commit to current branch
git cherry-pick commit-hash
# Cherry-pick range
git cherry-pick commit1..commit2
# Cherry-pick without committing
git cherry-pick -n commit-hash
Stashing
# Save work temporarily
git stash
git stash save "WIP: working on feature"
# List stashes
git stash list
# Apply stash
git stash apply # Keep stash
git stash pop # Apply and remove
# Apply specific stash
git stash apply stash@{2}
# Show stash contents
git stash show -p stash@{0}
# Create branch from stash
git stash branch new-branch stash@{0}
# Clear all stashes
git stash clear
Git Hooks
# Create pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
# Run tests before commit
echo "Running tests..."
npm test
if [ $? -ne 0 ]; then
echo "Tests failed! Commit aborted."
exit 1
fi
echo "Tests passed!"
EOF
chmod +x .git/hooks/pre-commit
# Create commit message hook
cat > .git/hooks/commit-msg << 'EOF'
#!/bin/bash
# Enforce commit message format
commit_regex='^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .{1,50}'
if ! grep -qE "$commit_regex" "$1"; then
echo "Invalid commit message format!"
echo "Format: type(scope): subject"
echo "Example: feat(auth): add login functionality"
exit 1
fi
EOF
chmod +x .git/hooks/commit-msg
๐ฎ Quick Examples
Example 1: Team Collaboration Script ๐ฅ
#!/bin/bash
# Git collaboration helper
cat > /usr/local/bin/git-team << 'EOF'
#!/bin/bash
case "$1" in
sync)
echo "๐ Syncing with team..."
git fetch --all --prune
git pull --rebase origin main
echo "โ
Synced with latest changes"
;;
feature)
if [ -z "$2" ]; then
echo "Usage: git-team feature <name>"
exit 1
fi
echo "๐ Creating feature branch: feature/$2"
git checkout -b feature/$2 main
git push -u origin feature/$2
echo "โ
Feature branch created and pushed"
;;
review)
echo "๐ Preparing for code review..."
git fetch origin
git log origin/main..HEAD --oneline
echo ""
echo "Files changed:"
git diff origin/main --name-only
echo ""
echo "Diff statistics:"
git diff origin/main --stat
;;
cleanup)
echo "๐งน Cleaning up merged branches..."
git checkout main
git pull origin main
# Delete local merged branches
git branch --merged | grep -v "\*\|main\|develop" | xargs -n 1 git branch -d
# Delete remote tracking branches
git remote prune origin
echo "โ
Cleanup complete"
;;
status)
echo "๐ Team Repository Status"
echo "========================="
echo "Current branch: $(git branch --show-current)"
echo "Last commit: $(git log -1 --oneline)"
echo ""
echo "Local branches:"
git branch
echo ""
echo "Remote branches:"
git branch -r
echo ""
echo "Recent activity:"
git log --oneline -10 --graph --all
;;
*)
echo "Git Team Helper"
echo "Usage: git-team {sync|feature|review|cleanup|status}"
echo ""
echo " sync - Sync with remote repository"
echo " feature - Create new feature branch"
echo " review - Prepare for code review"
echo " cleanup - Remove merged branches"
echo " status - Show repository status"
;;
esac
EOF
chmod +x /usr/local/bin/git-team
# Usage
git-team sync
git-team feature user-authentication
git-team review
Example 2: Automated Release Manager ๐
#!/bin/bash
# Automated release workflow
cat > /usr/local/bin/git-release << 'EOF'
#!/bin/bash
VERSION=$1
CURRENT_BRANCH=$(git branch --show-current)
if [ -z "$VERSION" ]; then
echo "Usage: git-release <version>"
echo "Example: git-release 1.2.0"
exit 1
fi
echo "๐ Starting release process for v$VERSION"
# Ensure we're on develop
if [ "$CURRENT_BRANCH" != "develop" ]; then
echo "โ ๏ธ Switching to develop branch..."
git checkout develop
fi
# Update from remote
echo "๐ Updating develop branch..."
git pull origin develop
# Create release branch
echo "๐ฟ Creating release branch..."
git checkout -b release/$VERSION
# Update version in files
echo "๐ Updating version numbers..."
sed -i "s/version=\".*\"/version=\"$VERSION\"/" package.json
sed -i "s/VERSION = .*/VERSION = '$VERSION'/" config.py
echo "v$VERSION" > VERSION
# Commit version changes
git add -A
git commit -m "chore: bump version to $VERSION"
# Run tests
echo "๐งช Running tests..."
npm test
if [ $? -ne 0 ]; then
echo "โ Tests failed! Aborting release."
git checkout develop
git branch -D release/$VERSION
exit 1
fi
# Build project
echo "๐จ Building project..."
npm run build
# Generate changelog
echo "๐ Generating changelog..."
git log --pretty=format:"- %s" develop..HEAD > CHANGELOG_TEMP.md
cat CHANGELOG_TEMP.md CHANGELOG.md > CHANGELOG_NEW.md
mv CHANGELOG_NEW.md CHANGELOG.md
rm CHANGELOG_TEMP.md
echo "## Version $VERSION - $(date +%Y-%m-%d)" | cat - CHANGELOG.md > temp
mv temp CHANGELOG.md
git add CHANGELOG.md
git commit -m "docs: update changelog for v$VERSION"
# Merge to main
echo "๐ Merging to main..."
git checkout main
git pull origin main
git merge --no-ff release/$VERSION -m "release: v$VERSION"
# Create tag
echo "๐ท๏ธ Creating tag..."
git tag -a v$VERSION -m "Release version $VERSION"
# Merge back to develop
echo "๐ Merging back to develop..."
git checkout develop
git merge --no-ff release/$VERSION -m "Merge release v$VERSION back to develop"
# Push everything
echo "๐ค Pushing to remote..."
git push origin main
git push origin develop
git push origin --tags
# Cleanup
echo "๐งน Cleaning up..."
git branch -d release/$VERSION
echo "โ
Release v$VERSION completed successfully!"
echo ""
echo "๐ฆ Don't forget to:"
echo " - Create GitHub/GitLab release"
echo " - Deploy to production"
echo " - Notify team"
EOF
chmod +x /usr/local/bin/git-release
# Usage
git-release 1.2.0
Example 3: Git Statistics Dashboard ๐
#!/bin/bash
# Git repository analytics
cat > /usr/local/bin/git-stats << 'EOF'
#!/bin/bash
echo "๐ Git Repository Statistics"
echo "============================"
echo ""
# Repository info
echo "๐ Repository Information:"
echo " Name: $(basename `git rev-parse --show-toplevel`)"
echo " Remote: $(git remote get-url origin 2>/dev/null || echo 'No remote')"
echo " Current Branch: $(git branch --show-current)"
echo " Total Branches: $(git branch -a | wc -l)"
echo " Total Tags: $(git tag | wc -l)"
echo " Total Commits: $(git rev-list --all --count)"
echo ""
# Contributors
echo "๐ฅ Top Contributors (by commits):"
git shortlog -sn | head -10
echo ""
# Recent activity
echo "๐
Recent Activity:"
echo " Commits today: $(git log --since=midnight --oneline | wc -l)"
echo " Commits this week: $(git log --since='1 week ago' --oneline | wc -l)"
echo " Commits this month: $(git log --since='1 month ago' --oneline | wc -l)"
echo ""
# File statistics
echo "๐ File Statistics:"
echo " Total files: $(git ls-files | wc -l)"
echo " Lines of code: $(git ls-files | xargs wc -l | tail -1 | awk '{print $1}')"
echo ""
echo " Top 5 largest files:"
git ls-files | xargs ls -la 2>/dev/null | sort -k5 -rn | head -5 | awk '{print " " $9 " (" $5 " bytes)"}'
echo ""
# Language breakdown
echo "๐ป Language Breakdown:"
git ls-files | grep -E '\.(js|py|java|cpp|go|rs|rb|php|ts|sh)$' | sed 's/.*\.//' | sort | uniq -c | sort -rn | head -10
echo ""
# Commit patterns
echo "โฐ Commit Time Patterns:"
echo " By hour of day:"
git log --format="%ad" --date=format:'%H' | sort | uniq -c | sort -k2 -n | head -5 | while read count hour; do
printf " %02d:00 - %d commits\n" $hour $count
done
echo ""
echo " By day of week:"
git log --format="%ad" --date=format:'%A' | sort | uniq -c | sort -rn
# Branch activity
echo ""
echo "๐ฟ Branch Activity:"
for branch in $(git branch -r | grep -v HEAD); do
echo " $branch: $(git log --oneline $branch | wc -l) commits, last: $(git log -1 --format='%ar' $branch)"
done | head -10
# Generate report file
echo ""
echo "๐ Generating detailed report..."
{
echo "Git Repository Report"
echo "Generated: $(date)"
echo "========================"
echo ""
git log --pretty=format:'%h - %an, %ar : %s' --graph -20
echo ""
echo "File Changes Summary:"
git diff --stat HEAD~10
} > git-report-$(date +%Y%m%d).txt
echo "โ
Report saved to git-report-$(date +%Y%m%d).txt"
EOF
chmod +x /usr/local/bin/git-stats
# Run statistics
git-stats
๐จ Fix Common Problems
Problem 1: Merge Conflicts โ
Files have conflicts?
# View conflicts
git status
git diff
# Resolve manually
vim conflicted-file.txt
# Look for <<<<<<< HEAD
# Or use merge tool
git mergetool
# After resolving
git add conflicted-file.txt
git commit
# Abort merge if needed
git merge --abort
Problem 2: Pushed Wrong Commit โ
Need to undo pushed commit?
# Revert (safe - creates new commit)
git revert HEAD
git push
# Reset (dangerous - rewrites history)
git reset --hard HEAD~1
git push --force # DANGEROUS!
# Better: create fix commit
git commit -m "Fix: undo previous changes"
git push
Problem 3: Lost Changes โ
Accidentally deleted work?
# Check reflog
git reflog
# Restore from reflog
git checkout HEAD@{2}
# Find lost commits
git fsck --lost-found
# Recover stashed changes
git stash list
git stash apply stash@{0}
Problem 4: Wrong Branch โ
Committed to wrong branch?
# Move commit to correct branch
git checkout correct-branch
git cherry-pick wrong-branch
git checkout wrong-branch
git reset --hard HEAD~1
# Or use rebase
git rebase --onto correct-branch wrong-branch~1 wrong-branch
๐ Simple Commands Summary
Task | Command |
---|---|
๐ Status | git status |
โ Add files | git add . |
๐พ Commit | git commit -m "message" |
๐ค Push | git push origin main |
๐ฅ Pull | git pull origin main |
๐ฟ New branch | git checkout -b feature |
๐ Merge | git merge feature |
๐ History | git log --oneline |
๐ก Tips for Success
- Commit Often ๐พ - Small commits are better
- Write Good Messages ๐ - Future you will thank you
- Branch Everything ๐ฟ - Never work on main
- Pull Before Push ๐ฅ - Avoid conflicts
- Use .gitignore ๐ซ - Donโt track everything
- Backup Important Branches ๐พ - Push to multiple remotes
Pro tip: Use git reflog
to recover from almost any mistake. Itโs saved me countless times! ๐ฆธ
๐ What You Learned
Youโre now a Git ninja! You can:
- โ Install and configure Git
- โ Use essential commands
- โ Implement team workflows
- โ Handle branches and merges
- โ Resolve conflicts
- โ Use advanced techniques
- โ Automate workflows
๐ฏ Why This Matters
Git provides:
- ๐ Complete code history
- ๐ฅ Seamless collaboration
- ๐ Safe experimentation
- โฐ Easy rollbacks
- ๐ Distributed backup
- ๐ Professional workflow
Last week, a junior dev accidentally deleted our entire codebase. Recovery time? 30 seconds with git reset --hard
. Without Git? Weโd still be crying! ๐ญโก๏ธ๐
Remember: In Git we trust, everything else we commit! ๐
Happy versioning! May your commits be atomic and your merges be clean! ๐โจ