Git Beginner's Guide
What is Git?
Git is a distributed version control system that helps you track changes in your code over time. Think of it as a sophisticated "save" system that remembers every version of your files and allows you to go back to any previous state.
Why Use Git?
- Track Changes: See what changed, when, and who made the changes
- Backup: Your code is stored in multiple places
- Collaboration: Multiple people can work on the same project without conflicts
- Branching: Work on different features simultaneously
- Rollback: Easily return to a previous working version
Basic Git Concepts
Repository (Repo)
A repository is a folder that contains your project files and Git's tracking information. It's like a project folder with superpowers.
Working Directory
The folder on your computer where you edit files. This is where you make changes to your code.
Staging Area (Index)
A temporary area where you prepare files before committing them. Think of it as a "loading dock" where you gather changes before saving them permanently.
Commit
A snapshot of your project at a specific point in time. Each commit has a unique ID and a message describing what changed.
Branch
A parallel version of your repository. The default branch is usually called main
or master
.
Remote
A version of your repository stored on a server (like GitHub, GitLab, or Bitbucket).
Git Workflow
The basic Git workflow follows this pattern:
- Modify files in your working directory
- Stage changes you want to include in your next commit
- Commit the staged changes to create a snapshot
- Push commits to a remote repository (optional)
Essential Git Commands
Initial Setup
# Set your name and email (required for commits)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Check your configuration
git config --list
Creating and Cloning Repositories
# Create a new repository in the current directory
git init
# Clone an existing repository from a remote location
git clone https://github.com/username/repository-name.git
# Clone into a specific folder
git clone https://github.com/username/repository-name.git my-project
Checking Status and Changes
# Check the status of your working directory
git status
# See what changes you've made to files
git diff
# See changes in staged files
git diff --staged
# View commit history
git log
# View compact commit history
git log --oneline
Staging and Committing Changes
# Stage a specific file
git add filename.txt
# Stage all changes in the current directory
git add .
# Stage all changes in the repository
git add -A
# Remove a file from staging area (unstage)
git reset filename.txt
# Commit staged changes with a message
git commit -m "Your commit message here"
# Add and commit in one step (for tracked files only)
git commit -am "Your commit message"
Working with Branches
# List all branches
git branch
# Create a new branch
git branch new-feature
# Switch to a branch
git checkout new-feature
# Create and switch to a new branch in one command
git checkout -b new-feature
# Switch to a branch (newer syntax)
git switch new-feature
# Create and switch to a new branch (newer syntax)
git switch -c new-feature
# Merge a branch into the current branch
git merge new-feature
# Delete a branch
git branch -d new-feature
Working with Remote Repositories
# List remote repositories
git remote -v
# Add a remote repository
git remote add origin https://github.com/username/repository-name.git
# Push changes to remote repository
git push origin main
# Push and set upstream branch
git push -u origin main
# Pull changes from remote repository
git pull origin main
# Fetch changes without merging
git fetch origin
Undoing Changes
# Discard changes in working directory
git checkout -- filename.txt
# Discard all changes in working directory
git checkout -- .
# Remove files from staging area
git reset HEAD filename.txt
# Undo the last commit (keeps changes in working directory)
git reset --soft HEAD~1
# Undo the last commit (removes changes)
git reset --hard HEAD~1
# Create a new commit that undoes a previous commit
git revert commit-hash
Common Git Scenarios
Starting a New Project
# Navigate to your project folder
cd my-project
# Initialize Git repository
git init
# Add all files
git add .
# Make your first commit
git commit -m "Initial commit"
# Add remote repository
git remote add origin https://github.com/username/my-project.git
# Push to remote
git push -u origin main
Daily Workflow
# Check current status
git status
# Stage your changes
git add .
# Commit with a descriptive message
git commit -m "Add user authentication feature"
# Push to remote repository
git push
Working on a Feature
# Create and switch to a new branch
git checkout -b feature-login
# Make your changes and commit them
git add .
git commit -m "Implement login functionality"
# Switch back to main branch
git checkout main
# Merge your feature
git merge feature-login
# Delete the feature branch
git branch -d feature-login
Best Practices
Commit Messages
- Use present tense: "Add feature" not "Added feature"
- Be descriptive but concise
- Start with a capital letter
- Don't end with a period
Good Examples:
- "Add user registration form"
- "Fix navigation menu bug"
- "Update README with installation instructions"
When to Commit
- Commit early and often
- Each commit should represent a logical change
- Don't commit broken code to the main branch
- Commit related changes together
Branch Naming
- Use descriptive names:
feature-user-login
,bugfix-menu-alignment
- Use lowercase and hyphens
- Be consistent with your team's naming convention
Getting Help
# Get help for a specific command
git help add
git help commit
git help merge
# Quick help
git add --help
Troubleshooting Common Issues
"Repository not found" Error
- Check if the remote URL is correct:
git remote -v
- Verify you have access to the repository
Merge Conflicts
- Open the conflicted files and look for conflict markers (
<<<<<<<
,=======
,>>>>>>>
) - Edit the files to resolve conflicts
- Stage the resolved files:
git add filename
- Complete the merge:
git commit