# Mastering Git: Key Commands Explained

In this article, I'll help you understand Git commands and workflows in a structured way. Let's build up your knowledge from the fundamentals to more advanced concepts.

First, let's understand what Git is: Git is a distributed version control system that helps track changes in your code over time. Think of it like a time machine for your code that also enables collaboration.

Let's start with the basic workflow:

1. Local Git Workflow
    

```bash
# Initialize a new Git repository
git init   # Creates a new .git directory to track your project

# Check status of your files
git status   # Shows which files are tracked, modified, or untracked

# Add files to staging area
git add filename    # Stage specific file
git add .           # Stage all files in current directory

# Commit your changes
git commit -m "Your message"   # Save staged changes with a descriptive message
```

Understanding the Three Areas in Git:

* Working Directory: Where you actually work on your files
    
* Staging Area (Index): A temporary area where you prepare changes for committing
    
* Repository: Where Git permanently stores changes as commits
    

2. Branch Management
    

```bash
# Create and switch to a new branch
git branch feature-name     # Create branch
git checkout feature-name   # Switch to branch
# Or do both at once:
git checkout -b feature-name

# List branches
git branch        # List local branches
git branch -a     # List all branches including remote

# Delete branch
git branch -d feature-name    # Safe delete (won't delete if changes aren't merged)
git branch -D feature-name    # Force delete
```

3. Remote Repository Operations
    

```bash
# Connect to remote repository
git remote add origin <repository-url>

# Push changes
git push origin branch-name    # Send commits to remote repository

# Pull changes
git pull origin branch-name    # Fetch and merge changes from remote

# Fetch changes without merging
git fetch origin    # Download changes but don't merge them
```

4. Merging and Conflict Resolution
    

```bash
# Merge a branch into current branch
git merge feature-branch

# If conflicts occur, you'll need to:
# 1. Open conflicted files (marked with <<<<<<< and >>>>>>>)
# 2. Resolve conflicts manually
# 3. git add the resolved files
# 4. git commit to complete the merge
```

5. History and Inspection
    

```bash
# View commit history
git log                    # Show commit history
git log --oneline         # Compact view
git log --graph --oneline # Visual representation of branches

# See changes
git diff                  # Show unstaged changes
git diff --staged        # Show staged changes
```

6. Advanced Operations
    

```bash
# Stash changes temporarily
git stash                 # Save changes without committing
git stash pop            # Restore stashed changes

# Reset changes
git reset --soft HEAD~1   # Undo last commit, keep changes staged
git reset --hard HEAD~1   # Undo last commit and discard changes

# Rebase (rewrite history)
git rebase main          # Rebase current branch onto main
```

Common Workflow Example:

1. Start a new feature:
    

```bash
git checkout main
git pull origin main
git checkout -b new-feature
```

2. Make changes and commit:
    

```bash
# Edit files
git add .
git commit -m "Implement new feature"
```

3. Keep up with main branch:
    

```bash
git checkout main
git pull origin main
git checkout new-feature
git merge main    # Or git rebase main
```

4. Push and create pull request:
    

```bash
git push origin new-feature
```

Important Tips:

* Always write clear, descriptive commit messages
    
* Commit often but logically - each commit should represent one complete change
    
* Pull changes from the main branch regularly to avoid major conflicts
    
* Use branches for new features/bugfixes
    
* Review changes before committing using `git status` and `git diff`
