Mastering Git: The Complete Command Line Guide
Git is an essential tool for every modern developer. While GUI tools exist, mastering the command line gives you ultimate control and speed. This guide serves as a complete reference for the most common and powerful Git commands you'll use daily.
Git vs SVN: Why Git?
SVN (Centralized)
- One central server
- Network required for commits
- Slow branching & merging
Git (Distributed)
- Every dev has full history
- Commit locally, push later
- Blazing fast branching
1. Setup & Config
$ git initInitialize a new local repository
$ git config --global user.name "Your Name"Set your global username
$ git config --global user.email "email@example.com"Set your global email
$ git config --listList all global and local configurations
2. Basic Workflow
$ git add <file>Add a specific file to the staging area
$ git add .Add all changed files to staging
$ git commit -m "commit message"Commit staged changes with a message
$ git statusCheck the status of your working directory
$ git logView commit history
$ git diffShow differences between working directory and staging
3. Branching & Merging
$ git branchList all local branches
$ git branch <name>Create a new branch
$ git checkout <name>Switch to a specific branch
$ git checkout -b <name>Create and switch to a new branch
$ git merge <branch>Merge a branch into the current one
$ git branch -d <name>Delete a local branch
4. Working with Remotes
$ git clone <url>Clone an existing repository
$ git remote add origin <url>Link local repo to a remote server
$ git push origin <branch>Push local commits to remote
$ git pullFetch and merge changes from remote
$ git fetchDownload changes from remote without merging
5. Advanced & Undo
$ git reset <file>Unstage a file while keeping its changes
$ git checkout -- <file>Discard changes in working directory
$ git revert <commit>Create a new commit that undoes a previous one
$ git stashTemporarily save uncommitted changes
$ git stash popApply and remove stashed changes
$ git rebase <branch>Apply commits on top of another base tip
Conclusion
Mastering these commands will significantly improve your productivity. Remember, Git is a safety net; the more you understand how it works under the hood, the more confident you'll be in managing your codebase and collaborating with others.