Prerequisites#
Before starting, make sure you have Git installed on your machine. Download it from Git's official website. You can verify the installation by opening your terminal (PowerShell on Windows, Terminal on Mac/Linux) and running:
git --version
If you see a version number, you're ready to go!
Initial Setup#
Configure Your Identity#
Before making your first commit, tell Git who you are. This information will be attached to all your commits:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Optional but recommended: Enable colored output for better readability:
git config --global color.ui auto
Verify Your Configuration#
Check that your settings are correct:
git config --list
Getting Started with a Repository#
You have two options to start working with Git: create a new repository or clone an existing one.
Option 1: Create a New Repository#
Navigate to your project folder and initialize Git:
cd your-project-folder
git init
This creates a .git folder that tracks all your changes.
Option 2: Clone an Existing Repository#
Download an existing project from GitHub, GitLab, or another hosting service:
git clone https://github.com/username/repository-name.git
This downloads the entire project including all its history.
Basic Workflow#
This is the workflow you'll use daily when working with Git.
1. Check Your Status#
See which files have been modified:
git status
This shows you:
- Files you've changed but not staged
- Files staged and ready to commit
- Untracked files Git isn't watching yet
2. Stage Your Changes#
Add files to the staging area (preparing them for commit):
Stage a specific file:
git add filename.txt
Stage all changes:
git add .
Stage multiple specific files:
git add file1.txt file2.js file3.css
If you accidentally staged a file, you can unstage it:
git reset filename.txt
3. Review Your Changes#
Before committing, it's good practice to review what you've changed:
See unstaged changes:
git diff
See staged changes:
git diff --staged
4. Commit Your Changes#
Save your staged changes with a descriptive message:
git commit -m "Add user authentication feature"
What makes a good commit message:
- Start with a verb (Add, Fix, Update, Remove)
- Be specific but concise
- Explain _what_ changed and _why_ if not obvious
5. View Your History#
See a list of all commits:
git log
For a condensed view:
git log --oneline
Working with Branches#
Branches let you work on features without affecting the main codebase.
View Branches#
List all branches (the asterisk shows your current branch):
git branch
Create a New Branch#
git branch feature-name
Switch to a Branch#
git checkout feature-name
Shortcut: Create and switch to a new branch in one command:
git checkout -b feature-name
Merge Branches#
Once your feature is complete, merge it back into the main branch:
git checkout main
git merge feature-name
Delete a Branch#
After merging, you can delete the feature branch:
git branch -d feature-name
Collaborating with Remote Repositories#
Working with GitHub, GitLab, or other remote repositories.
View Your Remote#
See which remote repositories are connected:
git remote -v
Add a Remote#
If you initialized a local repo and want to connect it to a remote:
git remote add origin https://github.com/username/repository-name.git
Push Your Changes#
Upload your local commits to the remote repository:
First push (set upstream):
git push -u origin main
Subsequent pushes:
git push
Pull Changes#
Download and merge changes from the remote repository:
git pull
This is essential before starting work to ensure you have the latest code.
Fetch Changes#
Download changes without merging (lets you review first):
git fetch
Useful Commands for Daily Work#
Temporarily Save Changes#
If you need to switch branches but aren't ready to commit:
Save your work:
git stash
List stashed changes:
git stash list
Restore your work:
git stash pop
Discard stashed changes:
git stash drop
Move or Rename Files#
Let Git track file movements:
git mv old-filename.txt new-filename.txt
Remove Files#
Delete files and stage the removal:
git rm filename.txt
Using .gitignore#
Prevent Git from tracking certain files (like build outputs, secrets, or dependencies).
Create a .gitignore file in your project root:
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.log
# Environment variables
.env
.env.local
# OS files
.DS_Store
Thumbs.db
# IDE settings
.vscode/
.idea/
Common Scenarios & Solutions#
Scenario 1: Undo Last Commit (Keep Changes)#
Made a commit too early?
git reset --soft HEAD~1
Your changes stay staged and ready to recommit.
Scenario 2: Discard Local Changes#
Want to throw away all your uncommitted changes?
git reset --hard HEAD
Scenario 3: View a Specific File's History#
See all changes to one file:
git log --follow filename.txt
Scenario 4: Compare Branches#
See what's different between two branches:
git diff main..feature-branch
Best Practices#
Commit frequently: Small, focused commits are easier to understand and revert if needed.
Write clear messages: Your future self (and teammates) will thank you.
Pull before you push: Always get the latest changes before uploading yours to avoid conflicts.
Use branches: Keep your main branch stable by developing features in separate branches.
Review before committing: Use git diff to check what you're about to commit.
.gitignore for secrets, API keys, and credentials.Troubleshooting#
git stash, switch branches with git checkout, then git stash pop to move your uncommitted work.git pull first to get remote changes, resolve any conflicts, then push again.git reset --soft HEAD~1, switch branches, and recommit.Conclusion#
That's it! You now know the essential Git commands for daily development. The key is to practice these workflows regularly - they'll become second nature quickly. Remember to commit often, use descriptive messages, and don't be afraid to experiment with branches.
I'll make a follow-up guide on using Git visually (no commandline) in VSCode soon.