Git Commands for DevOps Engineers
As a ๐ ๏ธ DevOps engineer, mastering ๐ Git is essential for managing ๐ source code, collaborating with ๐ฅ team members, and automating โ๏ธ CI/CD pipelines. Below is a comprehensive guide to ๐ Git commands tailored for ๐ ๏ธ DevOps workflows.
1. Setup and Configuration
Set Up User Details:
# Configure ๐ค username and ๐ง email
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
# Check ๐ ๏ธ configuration
git config --list
Set Up Aliases:
# Create shortcuts for ๐ common commands
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
2. Repository Management
Create and Clone Repositories:
# Initialize a new ๐ Git repository
git init
# Clone an existing ๐ repository
git clone <repository_url>
3. Working with Branches
Branch ๐ฟ Operations:
# Create a new ๐ฟ branch
git branch <branch_name>
# Switch to a ๐ฟ branch
git checkout <branch_name>
# Create and switch to a ๐ฟ branch
git checkout -b <branch_name>
# List all ๐ฟ branches
git branch -a
Merge ๐ฟ Branches:
# Merge a ๐ฟ branch into the current ๐ฟ branch
git merge <branch_name>
Delete ๐ฟ Branches:
# Delete a local ๐ฟ branch
git branch -d <branch_name>
# Delete a remote ๐ branch
git push origin --delete <branch_name>
4. Making Changes
Track ๐ Changes:
# Check the status of the ๐ฅ๏ธ working directory
git status
# Add ๐ files to the staging area
git add <file_name>
# Add all ๐ changes
git add .
# Commit ๐ changes
git commit -m "Your commit message"
# Amend the last commit
git commit --amend
Stash ๐ฆ Changes:
# Stash ๐ฆ uncommitted changes
git stash
# Apply stashed ๐ฆ changes
git stash apply
# List all stashed ๐ฆ changes
git stash list
5. Collaborating with Remote ๐ Repositories
Push and Pull ๐ Changes:
# Push ๐ changes to a remote ๐ repository
git push origin <branch_name>
# Pull ๐ changes from a remote ๐ repository
git pull origin <branch_name>
Fetch ๐ Changes:
# Fetch ๐ changes from a remote ๐ repository
git fetch origin
Resolve โก Conflicts:
# Show โก conflicts
git status
# Resolve โก conflicts manually, then add the resolved file
git add <file_name>
# Continue the merge ๐ process
git commit
6. Tracking and Debugging ๐
View Logs and History ๐:
# View commit ๐ history
git log
# View a simplified commit ๐ history
git log --oneline
# View changes in a ๐ file
git diff <file_name>
Revert ๐ Changes:
# Revert a commit by creating a new ๐ commit
git revert <commit_hash>
# Reset to a specific commit (destructive ๐ฅ)
git reset --hard <commit_hash>
# Reset ๐ files to the last commit
git checkout -- <file_name>
7. Working with Tags ๐ท๏ธ
Tag Management:
# Create a ๐ท๏ธ tag
git tag <tag_name>
# Push a ๐ท๏ธ tag to a remote ๐ repository
git push origin <tag_name>
# List all ๐ท๏ธ tags
git tag
8. Advanced ๐ Git Commands
Cherry-Pick Commits ๐:
# Apply a specific ๐ commit from another ๐ฟ branch
git cherry-pick <commit_hash>
Rebase ๐:
# Rebase the current ๐ฟ branch onto another ๐ฟ branch
git rebase <branch_name>
Squash Commits ๐ฅ:
# Combine multiple commits into one during a rebase ๐
git rebase -i <commit_hash>
9. Automating ๐ Git Commands
Create ๐ช Hooks:
# Example: Pre-commit ๐ช hook to run tests
nano .git/hooks/pre-commit
Add the following script:
#!/bin/sh
# Run ๐งช tests before committing
npm test
Make the ๐ช hook executable:
chmod +x .git/hooks/pre-commit
By mastering these ๐ Git commands, youโll be better equipped to handle the complexities of ๐ ๏ธ DevOps workflows, enabling seamless ๐ฅ collaboration and effective ๐ source code management.
ย