Isaac.

Git Workflow Basics

Master essential Git commands for version control and collaboration.

By EMEPublished: February 20, 2025
gitversion-controlcollaborationbranchingworkflows

A Simple Analogy

Git is like a time machine for your code. You can save snapshots of your work (commits), create parallel timelines (branches), and travel back if something breaks. Multiple people can work on different parts without interfering.


What Is Git?

Git is a distributed version control system. It tracks changes to code, enables collaboration, and maintains a complete history of your project.


Why Use Git?

  • History: Access any previous version
  • Collaboration: Multiple developers work simultaneously
  • Branching: Isolate features without affecting main code
  • Code review: Pull requests enable peer review
  • Rollback: Undo mistakes easily
  • Merging: Combine work from different developers

Core Concepts

| Concept | Meaning | |---------|---------| | Repository | Project folder tracked by Git | | Commit | Snapshot of changes with a message | | Branch | Parallel timeline for development | | Merge | Combine changes from branches | | Remote | Hosted repository (GitHub, GitLab) |


Essential Commands

# Setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Create and clone
git init                        # Initialize repo
git clone <url>                # Clone existing repo

# Basic workflow
git status                      # See what changed
git add <file>                  # Stage changes
git commit -m "message"         # Save snapshot
git push                        # Upload to remote
git pull                        # Download updates

# Branching
git branch feature/login        # Create branch
git checkout feature/login      # Switch branch
git branch -d feature/login     # Delete branch

# Merging
git merge feature/login         # Merge into current branch

# Viewing history
git log                         # See commits
git diff                        # See changes
git show <commit>              # View specific commit

Typical Workflow

1. Create Feature Branch

git checkout -b feature/user-auth

2. Make Changes

# Edit files
git add .
git commit -m "feat: add JWT authentication"
git push origin feature/user-auth

3. Create Pull Request (GitHub)

Title: Add JWT Authentication
Description: 
- Implements JWT token generation
- Adds [Authorize] attribute
- Adds token refresh logic

4. Code Review and Merge

# After approval
git checkout main
git pull origin main
git merge feature/user-auth
git push origin main

Branching Strategies

Git Flow

main (production)
  └─ release/1.0
  └─ develop (staging)
       └─ feature/login
       └─ feature/signup
       └─ bugfix/password-reset

Trunk-Based Development

main (always deployable)
  └─ feature/small-change
  └─ feature/another-change

Practical Example

# Start feature
git checkout -b feature/payment-gateway
echo "Stripe integration" > stripe.js
git add stripe.js
git commit -m "feat: add Stripe payment integration"

# Continue working
echo "Webhook handling" >> stripe.js
git add stripe.js
git commit -m "feat: handle payment webhooks"

# Push and create PR
git push origin feature/payment-gateway

# After review and approval
git checkout main
git pull
git merge feature/payment-gateway --no-ff
git push origin main
git branch -d feature/payment-gateway

Avoiding Common Mistakes

  1. Large commits: Commit frequently, logically
  2. Unclear messages: "fix bug" → "fix NPE in payment processor"
  3. Committing secrets: Use .gitignore for sensitive files
  4. Force push on shared branches: Use --force-with-lease
  5. Merge conflicts: Pull latest before pushing

Related Concepts to Explore

  • GitHub/GitLab features (PRs, issues, CI/CD)
  • Advanced Git (rebase, cherry-pick, squash)
  • Semantic versioning and tagging
  • Git hooks (pre-commit, pre-push)

Summary

Git enables collaborative development with complete version history. Master branching, committing, and merging to work effectively in teams and maintain code quality.