Fundamentals 15 min read

Stop Memorizing Git Commands: Master the 6 Core Operations That Cover 90% of Scenarios

This guide explains why most developers struggle with Git, distills the tool to six essential commands, and walks through branch management, common mistakes, rescue techniques, efficient workflows, and advanced tips such as aliases and stash, all illustrated with concrete code examples.

Data STUDIO
Data STUDIO
Data STUDIO
Stop Memorizing Git Commands: Master the 6 Core Operations That Cover 90% of Scenarios

Why You Struggle with Git

Most tutorials start with theory—distributed version control, DAGs, snapshots—making beginners feel overwhelmed, but you can use Git effectively without fully understanding its internals.

Git = Game Save System + Time Machine

Working directory : the current game screen.

Staging area : the saved progress ready to be committed.

Local repository : the collection of saved checkpoints.

Remote repository : the cloud save shared with teammates.

Seeing Git through this analogy makes every operation intuitive.

Six Core Commands for 90% of Daily Use

1. git status – Check the current state

Use whenever you are unsure of the repository status or before committing to see modified and untracked files.

# git status
# On branch main
# Changes not staged for commit:
#   modified:   README.md
#   modified:   src/index.js
# Untracked files:
#   new-file.txt
modified

: tracked file changed. untracked: new file not yet tracked.

2. git add – Stage changes

Think of it as adding items to a shopping cart.

# Add a single file
git add README.md
# Add multiple files
git add src/index.js src/utils.js
# Add everything (most common)
git add .
# Add everything but exclude patterns via .gitignore
git add --all

Use a .gitignore file to avoid committing unwanted files.

# .gitignore example
node_modules/
*.log
.env
dist/

3. git commit – Create a snapshot

Commit is the official save point.

# Basic commit
git commit -m "Fix login page styling"
# Good commit message guidelines:
# 1. ≤50 characters
# 2. Start with a verb (add, fix, update)
# 3. Describe what was done, not how
# Bad examples:
# git commit -m "update"
# git commit -m "fix bug"
# Good examples:
# git commit -m "Add user registration validation"
# git commit -m "Fix null pointer in API response"

Amend the most recent commit if needed:

# Amend after forgetting a file or typo
git add missing-file.txt
git commit --amend -m "New commit message"

4. git log – View history

Shows the list of saved checkpoints.

# Full history
git log
# One‑line summary (most useful)
git log --oneline
# Example output
# a1b2c3d (HEAD -> main) Fix login page styling
# e4f5g6h Add user registration validation
# i7j8k9l Initial commit
# Show last 3 commits
git log -3
# Graph view of merges
git log --graph --oneline

5. git push / git pull – Sync with remote

git push

uploads your local snapshots; git pull downloads others' changes.

# Push to remote
git push origin main
# Set upstream for first push
git push -u origin main
# Pull updates
git pull origin main
# Internally, pull = fetch + merge

Branch Management – The Core Collaboration Skill

Branch = Parallel Universe

The main branch is the stable universe; feature branches are experimental universes.

Basic Branch Operations

# List branches (current marked with *)
git branch
# Create a new branch
git branch feature/user-profile
# Switch to it
git checkout feature/user-profile
# Create & switch in one step (most common)
git checkout -b feature/shopping-cart
# Merge a feature back into main
git checkout main
git pull origin main
git merge feature/shopping-cart
# Delete merged branch
git branch -d feature/shopping-cart

Handling Merge Conflicts

When Git reports a conflict, it shows both versions.

# Attempt merge
git merge feature/branch
# Conflict output example
# Auto-merging src/app.js
# CONFLICT (content): Merge conflict in src/app.js
# Manual resolution needed
<<<<<<< HEAD
console.log("This is code from main")
=======
console.log("This is code from feature")
>>>>>>> feature/branch
# After editing, mark resolved
git add src/app.js
# Complete merge
git commit -m "Merge feature/branch, resolve conflict"

Emergency Rescue – Recovering from Mistakes

Common Scenarios

Committed the wrong file: git reset HEAD <em>file</em> (keep changes) or git reset HEAD (unstage all).

Want to undo the last commit: git reset --soft HEAD~1 (keep changes) or git reset --hard HEAD~1 (discard everything). Recommended: git revert HEAD (creates a new commit).

Deleted uncommitted code: git checkout -- <em>file</em> or git checkout -- . to restore all.

Reset to match remote exactly: git fetch origin then git reset --hard origin/main (warning: discards local work).

Git’s “Time Machine”: git reflog

Shows every action, even those that seem lost.

# View all refs
git reflog
# Example output
# a1b2c3d (HEAD -> main) HEAD@{0}: commit: Update feature
# e4f5g6h HEAD@{1}: reset: moving to HEAD~1
# i7j8k9l HEAD@{2}: commit: Add new feature
# Recover a lost commit by checkout or branch creation
git checkout i7j8k9l
# or
git branch recovery-branch i7j8k9l

Efficient Workflows – From Theory to Practice

Personal Project Minimal Flow

# Daily routine
git add .
git commit -m "Describe your changes"
git push

Team Collaboration Standard Flow

# 1. Start new feature
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature/your-feature
# 3. Develop
git add .
git commit -m "Complete login feature"
# (repeat commits as needed)
# 4. Push for review
git push -u origin feature/your-feature
# 5. Open Pull Request / Merge Request on GitHub or GitLab
# 6. After approval, merge to main (usually via web UI)
# 7. Clean up
git checkout main
git branch -d feature/your-feature

Advanced Tips – Boosting Efficiency

Git Aliases: Shorten Long Commands

# Add to ~/.gitconfig
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.last 'log -1 HEAD'
# Use aliases
git st   # same as git status
git co main   # same as git checkout main
git last   # view most recent commit

Stashing: Temporarily Save Work

# Save current work
git stash
# Save with description
git stash save "description"
# List stashes
git stash list
# Apply most recent stash and remove it
git stash pop
# Apply without removing
git stash apply
# Clear all stashes
git stash clear

Conclusion

Git is a “super save button”; mastering the six core commands— status, add, commit, log, push / pull —covers the vast majority of daily scenarios. Branches enable parallel development, conflicts are solvable, and mistakes can be undone with reset, revert, or reflog. Use aliases and stash to streamline your workflow, and remember that proficiency with Git is a skill, not a measure of programming ability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

workflowGitCommand-lineVersion ControlBranchingGit AliasesGit Reflog
Data STUDIO
Written by

Data STUDIO

Click to receive the "Python Study Handbook"; reply "benefit" in the chat to get it. Data STUDIO focuses on original data science articles, centered on Python, covering machine learning, data analysis, visualization, MySQL and other practical knowledge and project case studies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.