Git & Linux GitDevOpsCommands

Git Commands You Will Use Every Single Day as a Developer

Why Git is Non-Negotiable for Every Developer

Git is a distributed version control system that tracks every change made to your codebase over time. Created by Linus Torvalds in 2005 to manage Linux kernel development, it has since become the universal standard for source code management across the entire software industry. Whether you work solo on a personal project or as part of a team of hundreds, understanding Git is as fundamental as understanding the programming language you work in.

Without Git, you are one catastrophic mistake away from losing weeks of work. With Git, every single change is reversible, every experiment is safely isolated in its own branch, and collaboration with other developers becomes structured and conflict-free. Git is what enables open-source projects with thousands of contributors to function cohesively.

Initial Setup — Do This Before Anything Else

Before you commit your first line of code, tell Git who you are. This information is attached to every commit you create:

git config --global user.name  "Your Full Name"
git config --global user.email "you@yourdomain.com"
git config --global core.editor "code --wait"     # VS Code as default editor
git config --global init.defaultBranch main       # use 'main' not 'master'

# Verify all your settings
git config --list

Starting a Project

# Option A: Start a brand-new local repository
git init my-project
cd my-project

# Option B: Clone an existing remote repository
git clone https://github.com/username/repository.git
cd repository

# Check the remote connections
git remote -v

The Core Daily Workflow

You will repeat this four-step cycle dozens of times every day: check status, stage changes, commit, push. Internalising this rhythm is the single most important Git skill:

# 1. Always start by seeing the current state
git status

# 2. Stage specific files, or stage everything
git add src/components/Button.js   # stage one file
git add .                          # stage all changes

# 3. Commit with a clear, conventional message
git commit -m "feat: add animated CTA button component"
git commit -m "fix: resolve mobile navbar overflow"
git commit -m "docs: update README with setup instructions"

# 4. Push to the remote repository
git push origin main

Good commit messages follow the Conventional Commits format: a type prefix (feat:, fix:, docs:, refactor:, chore:, test:) followed by a concise message in the imperative tense. This makes your project history scannable and enables automated changelog generation.

Branching — The Heart of Team Collaboration

A branch is an independent line of development. You always work on a feature or bug fix in its own branch so your changes do not affect the stable main codebase until you are ready to merge.

# See all branches (local and remote)
git branch -a

# Create and switch to a new branch
git checkout -b feature/user-authentication
# Modern Git 2.23+ syntax:
git switch -c feature/user-authentication

# Commit your work on the branch...

# Switch back to main
git switch main

# Merge the feature into main
git merge feature/user-authentication

# Delete the finished branch
git branch -d feature/user-authentication

Syncing with Remote Repositories

# Fetch updates from remote (does NOT change your local code)
git fetch origin

# Pull = fetch + merge (the most common way to sync)
git pull origin main

# Push your branch for the first time and track it
git push -u origin feature/user-authentication

# After tracking is set, just use:
git push

Viewing History and Differences

# Compact, visual commit history
git log --oneline --graph --all

# See what changed between working dir and last commit
git diff

# See what is staged and about to be committed
git diff --staged

# See changes in a specific commit
git show abc1234

# Compare two branches
git diff main..feature/login

Undoing Mistakes

One of Git's biggest selling points is that almost every mistake is reversible. Here are the tools you will reach for when things go wrong:

# Discard unstaged changes in a file (permanent!)
git restore src/App.js

# Unstage a file but keep your local changes
git restore --staged src/App.js

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

# Completely discard last commit AND changes (dangerous!)
git reset --hard HEAD~1

# Safely revert a commit that was already pushed (creates a new commit)
git revert abc1234

Stashing — Temporarily Parking Your Changes

# Save current changes without committing
git stash

# List saved stashes
git stash list

# Apply the most recent stash and remove it from the list
git stash pop

# Apply a specific stash
git stash apply stash@{2}

The .gitignore File

The .gitignore file tells Git which files and folders to never track. Every project should have one committed at the root. Common entries include:

# Dependencies
node_modules/
__pycache__/
.venv/

# Environment and secrets
.env
.env.local
*.key

# Build outputs
dist/
build/
*.pyc

# Editor files
.vscode/settings.json
.DS_Store
← Back to Blog 📚 Browse Courses