Git Beginner Guide
What is Git?
Git is a distributed version control system that helps developers track changes in their code over time. It allows multiple developers to work on the same project simultaneously without conflicts and keeps a complete history of all changes.
Key Benefits
- Track Changes: See what changed, when, and by whom
- Collaboration: Multiple developers can work together seamlessly
- Branching: Work on features independently without affecting the main code
- Backup: Your complete project history is stored safely
- Revert Changes: Undo mistakes easily
Installing Git
Windows
# Download from https://git-scm.com/download/win# Or use package managerwinget install --id Git.Git -e --source wingetmacOS
# Using Homebrewbrew install git
# Or download from https://git-scm.com/download/macLinux (Ubuntu/Debian)
sudo apt-get updatesudo apt-get install gitVerify Installation
git --version# Output: git version 2.x.xBasic Configuration
After installing Git, configure your identity. This information will be attached to your commits.
# Set your namegit config --global user.name "Your Name"
# Set your emailgit config --global user.email "your.email@example.com"
# Set default branch name to 'main'git config --global init.defaultBranch main
# View your configurationgit config --listEssential Git Commands
1. Initialize a Repository
Create a new Git repository in your project folder.
# Navigate to your project foldercd my-project
# Initialize Git repositorygit init
# Output: Initialized empty Git repository in /path/to/my-project/.git/2. Clone a Repository
Copy an existing repository from a remote server (like GitHub).
# Clone a repositorygit clone https://github.com/username/repository-name.git
# Clone into a specific foldergit clone https://github.com/username/repository-name.git my-folder3. Check Repository Status
View the current state of your repository.
git statusExample Output:
On branch mainUntracked files: (use "git add <file>..." to include in what will be committed) index.html style.css
nothing added to commit but untracked files present4. Add Files to Staging Area
Select which files you want to include in your next commit.
# Add a specific filegit add index.html
# Add multiple filesgit add index.html style.css
# Add all files in current directorygit add .
# Add all files with a specific extensiongit add *.js5. Commit Changes
Save your staged changes with a descriptive message.
# Commit with a messagegit commit -m "Add homepage and styling"
# Commit with a detailed message (opens editor)git commitBest Practices for Commit Messages:
- Use present tense (“Add feature” not “Added feature”)
- Be clear and descriptive
- Keep the first line under 50 characters
- Add details in subsequent lines if needed
6. View Commit History
See all the commits made in your repository.
# View commit historygit log
# Compact view (one line per commit)git log --oneline
# View last 5 commitsgit log -5
# View commits with file changesgit log --statExample Output:
commit a1b2c3d4e5f6g7h8i9j0 (HEAD -> main)Author: Your Name <your.email@example.com>Date: Mon Dec 9 21:00:00 2025 +0530
Add homepage and styling7. View Changes
Compare differences between versions.
# View unstaged changesgit diff
# View staged changesgit diff --staged
# View changes in a specific filegit diff index.htmlBasic Git Workflow
Here’s a typical workflow for working with Git:
# 1. Check current statusgit status
# 2. Make changes to your files# (Edit index.html, add new features, etc.)
# 3. View what changedgit diff
# 4. Add files to staging areagit add index.html style.css
# 5. Commit your changesgit commit -m "Update homepage layout and add responsive styling"
# 6. View commit historygit log --onelinePractical Example: Creating Your First Repository
Let’s create a simple website project and track it with Git:
# Step 1: Create project foldermkdir my-websitecd my-website
# Step 2: Initialize Gitgit init
# Step 3: Create some filesecho "<!DOCTYPE html><html><body><h1>My Website</h1></body></html>" > index.htmlecho "body { font-family: Arial; }" > style.css
# Step 4: Check statusgit status
# Step 5: Add files to staginggit add .
# Step 6: Create first commitgit commit -m "Initial commit: Add index and style files"
# Step 7: View historygit logUnderstanding Git States
Files in Git can be in three states:
- Modified: Changed but not staged
- Staged: Marked for next commit
- Committed: Safely stored in repository
# Modified stateecho "New content" >> index.html
# Staged stategit add index.html
# Committed stategit commit -m "Update content"Common Beginner Mistakes
1. Forgetting to Add Files
# Wrong: Committing without addinggit commit -m "Update files" # Nothing to commit!
# Right: Add first, then commitgit add .git commit -m "Update files"2. Unclear Commit Messages
# Bad commit messagesgit commit -m "fix"git commit -m "update"git commit -m "changes"
# Good commit messagesgit commit -m "Fix navigation menu alignment issue"git commit -m "Update user authentication flow"git commit -m "Add responsive design for mobile devices"3. Not Checking Status
# Always check status before committinggit statusgit add .git commit -m "Your message"Quick Reference
| Command | Description |
|---|---|
git init | Initialize a new repository |
git clone <url> | Clone a repository |
git status | Check repository status |
git add <file> | Add file to staging area |
git add . | Add all files to staging |
git commit -m "message" | Commit staged changes |
git log | View commit history |
git log --oneline | View compact history |
git diff | View unstaged changes |
Next Steps
Once you’re comfortable with these basic commands, you’re ready to move on to:
- Working with remote repositories (GitHub, GitLab)
- Creating and managing branches
- Collaborating with other developers
- Resolving conflicts
Check out the Git Intermediate Guide to learn these advanced topics!