Git Commands for DevOps Engineers

ยท

4 min read

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.

ย