Git Command Guide

by Sabbir Hossain

1. What is Git?

Git is a version control system. It tracks changes to your files over time, so you can:

  • Go back to older versions
  • Work on features without breaking the main code
  • Collaborate with other people without overwriting each other's work

2. Setup (Do This Once)

# Tell Git who you are
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
 
# Check your settings
git config --list

3. Starting a Project

# Create a new Git repository in the current folder
git init
 
# Copy (clone) an existing repository from GitHub/GitLab etc.
git clone https://github.com/user/repo.git

💡 init starts fresh. clone copies a project that already exists somewhere else.


4. The Basic Workflow

This is the core loop you'll repeat constantly:

git status        # See what changed
git add file.txt   # Stage a specific file
git add .          # Stage everything
git commit -m "Describe what you changed"   # Save a snapshot

Quick visual

StageCommandMeaning
Working Directory(you edit files)Your actual files on disk
Staging Areagit addFiles marked "ready to save"
Repositorygit commitPermanent snapshot saved

5. Checking History & Changes

git log                 # Full commit history
git log --oneline       # Compact one-line-per-commit view
git log --graph --all   # Visual branch history
 
git diff                # See unstaged changes
git diff --staged       # See staged changes (before commit)
 
git show <commit-hash>  # See details of one specific commit

6. Branching

Branches let you work on something new without touching the main code.

git branch                  # List all branches
git branch new-feature      # Create a new branch
git checkout new-feature    # Switch to that branch
 
# Shortcut: create + switch in one step
git checkout -b new-feature
 
# Newer Git versions (recommended)
git switch new-feature
git switch -c new-feature   # create + switch
git branch -d branch-name   # Delete a branch (safe)
git branch -D branch-name   # Force delete a branch

7. Merging & Combining Work

git checkout main           # Go to the branch you want to merge INTO
git merge new-feature       # Bring changes from new-feature into main

If there's a conflict, Git will mark the conflicting lines in the file like this:

<<<<<<< HEAD
your current code
=======
incoming code
>>>>>>> new-feature

Edit the file to keep what you want, then:

git add file.txt
git commit -m "Resolve merge conflict"

8. Working with Remotes (GitHub, GitLab, etc.)

git remote -v                     # Show connected remote repositories
git remote add origin <url>       # Connect a remote for the first time
 
git push origin main              # Upload your commits
git push -u origin main           # Upload + remember this as default
 
git pull origin main              # Download + merge latest changes
git fetch                         # Download changes WITHOUT merging them

💡 pull = fetch + merge. Use fetch when you want to review changes before merging.


9. Undoing Things

SituationCommand
Unstage a file (keep changes)git restore --staged file.txt
Discard changes in a filegit restore file.txt
Undo last commit, keep changes stagedgit reset --soft HEAD~1
Undo last commit, unstage changesgit reset HEAD~1
Undo last commit, delete changesgit reset --hard HEAD~1 ⚠️
Create a new commit that undoes an old onegit revert <commit-hash>

⚠️ --hard permanently deletes changes. Use with caution.


10. Stashing (Save Work Temporarily)

Useful when you need to switch branches but aren't ready to commit.

git stash              # Save current changes, clean working directory
git stash list          # See all stashes
git stash pop           # Reapply the most recent stash and remove it
git stash apply         # Reapply without removing from stash list
git stash drop          # Delete a stash

11. Tags (Marking Releases)

git tag v1.0.0                     # Create a lightweight tag
git tag -a v1.0.0 -m "Release 1.0" # Create an annotated tag (recommended)
git push origin v1.0.0             # Push a single tag
git push origin --tags             # Push all tags

12. Inspecting Files

git blame file.txt        # See who last changed each line
git log -p file.txt       # See full change history of one file

13. Common Real-World Scenarios

"I committed to the wrong branch"

git reset --soft HEAD~1     # Undo the commit, keep changes
git switch correct-branch
git add .
git commit -m "message"

"I want to update my branch with the latest main"

git switch my-branch
git merge main
# or, for a cleaner history:
git rebase main

"I accidentally deleted a file"

git checkout -- file.txt

"I want to see what changed before pulling"

git fetch
git diff main origin/main

14. Quick Reference Cheat Sheet

git init                    # Start a repo
git clone <url>             # Copy a repo
git status                  # Check current state
git add .                   # Stage all changes
git commit -m "msg"         # Save a snapshot
git push                    # Upload to remote
git pull                    # Download + merge from remote
git branch                  # List branches
git switch -c <name>        # Create + switch branch
git merge <branch>          # Combine branches
git log --oneline           # View history
git stash                   # Temporarily save changes
git reset --hard HEAD~1     # ⚠️ Undo + delete last commit

15. Golden Rules

  1. Commit often, with clear messages.
  2. Pull before you push to avoid conflicts.
  3. Never --force push to a shared branch unless you're sure.
  4. Use branches for new features — don't work directly on main.
  5. When in doubt, git status tells you exactly where you stand.