🌿 Industry-Standard Git Branching Strategy and Commands

One of the most popular and widely adopted strategies in the industry is the Git Feature Branch Workflow (also referred to as GitHub Flow). It provides clarity, reduces merge conflicts, and ensures code is tested and reviewed before going live.

πŸ”‘ Core Branches

  • main β†’ Always production-ready code.

  • develop β†’ (Optional in Git Flow) Contains the latest integrated features before they’re merged to production.

  • feature/* β†’ Each new feature or enhancement lives here until completed.

  • hotfix/* β†’ Urgent fixes that go directly to main.

  • release/* β†’ Prepares stable versions for deployment.

πŸ› οΈ Example Workflow

  1. Create a feature branch from develop:

     git checkout develop
     git checkout -b feature/user-auth
    
  2. Work on your feature, commit changes, and push the branch.

  3. Open a Pull Request (PR) on GitHub β†’ reviewers check code quality.

  4. Merge into develop.

  5. When ready for deployment, merge develop β†’ main.


βœ… Example Scenario

Let’s say your team is building an E-commerce App:

  • Create a feature/cart-checkout branch.

  • Test it, then merge into develop.

  • After QA approval, merge develop into main for production.

  • If a critical bug is found in production (e.g., payment not processing), create hotfix/payment-bug from main, fix it, and push immediately.

This strategy ensures stable production, organized feature development, and safe rollbacks.


\==> Git Commands Cheat Sheet (with Explanations)

πŸ”§ Configuration

# Set your username
git config --global user.name "Your Name"

# Set your email
git config --global user.email "your@email.com"

# Check configuration
git config --list

πŸ’‘ Used once per machine to identify your commits.


πŸ“‚ Repository Setup

# Initialize a new Git repository
git init

# Clone an existing repository
git clone https://github.com/user/repo.git

πŸ’‘ git init for starting projects, git clone for downloading existing repos.


πŸ“Œ Basic Snapshotting

# Check status of files
git status

# Stage a file
git add filename.txt

# Stage all files
git add .

# Commit changes with a message
git commit -m "Added login feature"

# Commit with detailed description
git commit

πŸ’‘ git add stages changes; git commit saves them in history.


πŸ” Inspect & Review

# Show commit history
git log

# One-line history
git log --oneline

# View file changes
git diff

# View changes in staged files
git diff --staged

πŸ’‘ Helps review before pushing or merging.


🌿 Branching & Merging

# List branches
git branch

# Create new branch
git branch feature/login

# Switch to branch
git checkout feature/login

# Create & switch in one step
git checkout -b feature/login

# Merge branch into current branch
git merge feature/login

# Delete branch
git branch -d feature/login

πŸ’‘ Branching is essential for features, bug fixes, and experiments.


🌍 Working with Remotes

# Add remote repo
git remote add origin https://github.com/user/repo.git

# Show remote repos
git remote -v

# Push to remote
git push origin main

# Push and set upstream
git push -u origin main

# Pull latest changes
git pull origin main

# Fetch remote changes without merging
git fetch

πŸ’‘ git push uploads your code, git pull downloads & merges.


πŸ”„ Undoing Changes

# Unstage a file (keep changes)
git reset filename.txt

# Undo last commit but keep changes
git reset --soft HEAD~1

# Undo last commit & remove changes
git reset --hard HEAD~1

# Checkout file from last commit
git checkout -- filename.txt

πŸ’‘ Useful for fixing mistakes safely.


🏷️ Tags (Releases)

# List tags
git tag

# Create tag
git tag v1.0.0

# Push tags to remote
git push origin v1.0.0

πŸ’‘ Tags mark specific commits (e.g., releases).


πŸ§‘β€πŸ€β€πŸ§‘ Collaboration (GitHub Flow)

# Create a new branch
git checkout -b feature/cart

# Push feature branch
git push origin feature/cart

# Fetch and merge PR updates
git pull origin feature/cart

πŸ’‘ Always work in feature branches, then create Pull Requests (PR).


πŸš‘ Stashing (Work in Progress)

# Stash changes
git stash

# List stashes
git stash list

# Apply last stash
git stash apply

# Drop stash
git stash drop

πŸ’‘ Stashing is great when you need to switch branches but aren’t ready to commit.


πŸ—οΈ Advanced

# Rebase (replay commits on top of another branch)
git rebase main

# Squash commits interactively
git rebase -i HEAD~3

# Show commit graph
git log --oneline --graph --all

πŸ’‘ Re-basing makes history cleaner, squashing combines commits.


πŸš€ Industry Pro Tips

  • Always pull before pushing to avoid conflicts.

  • Use meaningful commit messages (fix:, feat:, docs: convention).

  • Protect main branch with branch protection rules on GitHub.

  • Prefer feature branching + PR instead of committing directly to main.

    #github #Branches

0
Subscribe to my newsletter

Read articles from Pratik Prajapati directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pratik Prajapati
Pratik Prajapati

πŸ‘‹ Welcome to my Hashnode blog! I'm a Web developer with 7 year's of experience and now I am going to change my domain as DevOps Engineer with lots of hands on experience.