Lazy Git: Init to Commit

🧠 What is Git?

Git is an open-source, distributed version control system that tracks changes in your project over time by taking snapshots on every commit. It lets multiple developers collaborate effectively, work on different features, and revisit any point in history.


πŸ“˜ Key Concepts

πŸ” Version Control System (VCS)

  • Tracks changes to files over time.

  • Allows rollback to previous versions.

  • Enables collaboration across team members.

🌍 Distributed

  • Every developer has a full copy of the entire repo (code + history).

  • You can work offline and sync later.

πŸ“Έ Snapshot

  • Git saves snapshots (not differences) of your project at every commit.

  • Each snapshot is uniquely identified using a hash (SHA-1).


πŸ› οΈ Initial Setup After Installing Git

1. Configure Git

git config --global user.name "Your Name"        # Set your name
git config --global user.email "you@example.com" # Set your email
git config --list                                # View all global configs

πŸ’‘ Why? Your commits will be tagged with this identity. It helps track who made changes.


2. Start a Project

Create a New Repository

git init  # Initializes a Git repo in the current directory

Clone an Existing Repository

git clone <repository_url>  # Downloads the repo to your system

🧰 Core Git Commands

1. πŸ” Check File Status

git status  # Shows untracked, modified, or staged files

2. βž• Stage Changes

git add <file>   # Stage a specific file
git add .        # Stage all changes in current directory

πŸ—‚οΈ The Staging Area is like a waiting room for changes before you commit.

3. βœ… Commit Changes

git commit -m "Add login feature"   # Commit staged changes
git commit -am "Quick fix"          # Stage all files and add commit

🌿 Branching and Collaboration

1. πŸͺ΄ Work with Branches

git branch                  # List all branches
git branch <branch_name>    # Create a new branch
git checkout <branch_name>  # Switch to a branch
git checkout -b <branch_name>  # Create and switch in one step

2. ❌ Delete Branches

git branch -d <branch>           # Delete a merged branch
git branch -D <branch>           # Force delete unmerged branch
git push origin --delete <branch>  # Delete a remote branch

3. 🌐 Remote Repositories

git remote -v                        # Show remote links
git remote add origin <repo_url>     # Link to a remote repo
git push -u origin main              # Push branch & set upstream

4. πŸ”„ Sync Code

git fetch       # Download changes (no merge)
git pull        # Fetch + merge into current branch

🧠 git fetch is safe β€” it downloads the latest changes from the remote repository but does not modify your current branch. It only updates the remote tracking branches (like origin/main). This lets you review the changes first before merging them into your local branch manually.

πŸ” git pull = fetch + merge β€” it first fetches the latest changes from the remote, then automatically merges them into your current local branch. This makes your local branch up to date, but it can cause conflicts if your branch has different changes from the remote.


πŸ”€ Merge vs Rebase

1. 🧬 Merge

git checkout main
git merge feature  # Merge feature into main
  • Creates a merge commit

  • Maintains non-linear history

  • Easier for teams; retains complete history

2. 🧹 Rebase

git checkout feature
git rebase main  # Move feature commits on top of main
  • Creates a linear commit history

  • Rewrites commit hashes

  • Preferred before merging for cleaner history


πŸ” Review & Compare

1. πŸ“œ View History

git log                # Full history with details
git log --oneline      # Short history (1 line per commit)
git log --oneline -10  # Show last 10 commits
git log HEAD..origin/main --oneline  # Show commits in remote not in local

2. 🧾 Compare Changes

git diff            # Show unstaged changes
git diff --staged   # Show staged (to-be-committed) changes

πŸ’‘ Example:

  1. Modify a file but don’t stage it β†’ git diff shows it.

  2. Stage the file β†’ git diff shows nothing, but git diff --staged shows what will be committed.


βͺ Undo Changes

1. πŸ” Unstage or Reset

git reset <file>       # Unstage but keep changes
git reset --hard       # Remove all uncommitted changes (DANGEROUS)
git checkout -- <file> # Discard local changes to a file

2. ↩️ Revert a Commit

git revert <commit_hash>  # Create a new commit that undoes the specified one

πŸ’Ό Work-in-Progress with Stash

1. πŸ“¦ Stash Commands

git stash                     # Save changes
git stash list                # See stash stack
git stash apply stash@{2}     # Apply a specific stash (keep in list)
git stash pop stash@{1}       # Apply & remove a specific stash from list

πŸ”– Git Tags – Marking Releases

Tags are used to mark specific points in history (like v1.0) for reference or release.

1. 🎯 Types of Tags:

  • Lightweight Tag: Just a pointer to a commit (like a branch).

  • Annotated Tag: Includes metadata (name, date, message, GPG signature).

2. πŸ“‹ Common Tag Commands

git tag                  # List all tags
git tag v1.0.0           # Create lightweight tag
git tag -a v1.0.0 -m "Initial release"  # Annotated tag
git show v1.0.0          # Show tag details
git push origin v1.0.0   # Push tag to remote
git tag -d v1.0.0        # Delete local tag
git push origin --delete v1.0.0  # Delete remote tag

🧠 Final Notes

  • Keep your branches clean β€” delete merged branches.

  • Use rebase for a clean, linear history β€” but avoid it on shared branches.

  • Use tags for versioning and easy rollbacks.

  • Practice regularly to master Git’s power.

1
Subscribe to my newsletter

Read articles from Yogesh Kumar Khatri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yogesh Kumar Khatri
Yogesh Kumar Khatri