Git - Deep Dive
The article will give you a basic understanding of Git Commands as a DevOps Engineer.
# Git Step-by-Step Guide for Beginners
## Phase 1: Initial Setup and Configuration
### Step 1: Installing and Configuring Git
# Set your identity
git config — global user. name “Your Name”
git config — global user.email “your.email@example.com”
# Check your configuration
git config — list
### Step 2: Starting with a Repository
# Initialize a new repository
git init
# Or clone an existing repository
git clone <repository-url>
## Phase 2: Basic Operations (Daily Commands)
### Step 3: Checking Status and Making Changes
# Check repository status
git status
# Add files to the staging area
git add filename.txt # Add specific file
git add . # Add all changes
# View differences
git diff # View unstaged changes
git diff — staged # View staged changes
### Step 4: Committing Changes
# Create a commit
git commit -m “Your descriptive message”
# View commit history
git log
git log — oneline # Simplified view
## Phase 3: Remote Repository Operations
### Step 5: Working with Remotes
# Push changes to remote
git push origin main
# Get updates from remote
git fetch # Download changes without merging
git pull # Download and merge changes
```
## Phase 4: Branch Management
### Step 6: Working with Branches
# View branches
git branch
# Create a new branch
git branch feature-name
# Switch branches
git checkout feature-name
# Or create and switch in one command
git checkout -b new-feature
# Delete branch (after merging)
git branch -d feature-name
## Phase 5: Collaboration Basics
### Step 7: Merging Changes
# Merge a branch into the current branch
git merge feature-name
# Handle merge conflicts if they occur
# Edit conflicted files manually
git add <resolved-files>
git commit -m “Merge conflict resolved”
## Phase 6: Undoing Changes
### Step 8: Recovery Commands
# Undo staged changes
git reset filename
# Undo commits (be careful!)
git reset — soft HEAD~1 # Undo the last commit, keep changes
git reset — hard HEAD~1 # Undo last commit, discard changes
# Revert a commit (safer than reset)
git revert commit-hash
## Phase 7: Advanced Features
### Step 9: Temporary Storage
# Save changes temporarily
git stash
# Re-apply stashed changes
git stash pop
### Step 10: Advanced History Management
# Modify last commit
git commit — amend -m “New message”
# Cherry-pick commits
git cherry-pick commit-hash
# Interactive rebase
git rebase -i HEAD~3
## Common Workflows for Beginners
### Starting a New Feature
1. Update the main branch
git checkout main
git pull origin main
2. Create a feature branch
git checkout -b feature-name
3. Make changes and commit
git add .
git commit -m “Add feature X”
4. Push changes
git push origin feature-name
### Updating Your Branch with Main
git checkout main
git pull origin main
git checkout your-branch
git merge main
## Best Practices for Beginners
1. Commit Messages
- Write clear, descriptive messages
- Use present tense (“Add feature” not “Added feature”)
- Keep the first line under 50 characters
2. Commit Frequency
- Commit small, logical changes
- Commit often
- Each commit should be self-contained
3. Branch Management
- Keep the main/master branch clean
- Create new branches for features
- Delete merged branches
4. Before Committing
- Review changes with git status
and git diff
- Ensure no sensitive data is included
- Test your changes
## Common Mistakes to Avoid
1. Committing sensitive information
2. Working directly on the main branch
3. Not pulling before starting new work
4. Creating large, unfocused commits
5. Using force push (`git push -f`)
## Troubleshooting Common Issues
### Issue 1: Accidentally committed to the wrong branch
git checkout correct-branch
git cherry-pick wrong-branch
git checkout wrong-branch
git reset — hard HEAD~1
### Issue 2: Need to undo last commit
# Keep changes staged
git reset — soft HEAD~1
# Discard changes completely
git reset — hard HEAD~1
### Issue 3: Stashed changes disappeared
git stash list
git reflog