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 (likeorigin/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:
Modify a file but donβt stage it β
git diff
shows it.Stage the file β
git diff
shows nothing, butgit 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.
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
