Git Essential Guide for Beginners
Git is the most popular version control system used by developers worldwide. Whether you’re working on personal projects or collaborating with teams, understanding Git is essential for modern software development.
Basic Git Workflow
1. Initialize a Repository
Create a new Git repository:
git init
Or clone an existing repository:
git clone https://github.com/username/repository.git
2. Check Status
See what files have changed:
git status
3. Stage Changes
Add files to the staging area:
git add filename.txt # Add specific file
git add . # Add all files
git add *.js # Add all JavaScript files
4. Commit Changes
Save changes with a descriptive message:
git commit -m "Add user authentication feature"
5. View History
See commit history:
git log # Detailed history
git log --oneline # Simplified history
Working with Branches
Branches allow you to work on different features without affecting the main codebase.
Create and Switch Branches
git branch feature-login # Create new branch
git checkout feature-login # Switch to branch
git checkout -b feature-login # Create and switch in one command
List Branches
git branch # List local branches
git branch -a # List all branches (local and remote)
Merge Branches
git checkout main # Switch to main branch
git merge feature-login # Merge feature branch into main
Delete Branches
git branch -d feature-login # Delete merged branch
git branch -D feature-login # Force delete unmerged branch
Working with Remote Repositories
Add Remote Repository
git remote add origin https://github.com/username/repository.git
Push Changes
git push origin main # Push to main branch
git push -u origin main # Push and set upstream
Pull Changes
git pull origin main # Pull from main branch
git fetch # Download changes without merging
Essential Git Commands Reference
File Operations
git add <file> # Stage specific file
git add . # Stage all changes
git rm <file> # Remove file from Git and filesystem
git mv <old> <new> # Rename/move file
Commit Operations
git commit -m "message" # Commit with message
git commit -am "message" # Stage and commit modified files
git commit --amend # Modify last commit
Branch Operations
git branch # List branches
git branch <name> # Create branch
git checkout <branch> # Switch branch
git merge <branch> # Merge branch
Remote Operations
git remote -v # Show remote URLs
git push <remote> <branch> # Push branch to remote
git pull <remote> <branch> # Pull branch from remote
git clone <url> # Clone repository
History and Information
git log # Show commit history
git log --oneline # Compact history
git show <commit> # Show commit details
git diff # Show unstaged changes
git diff --staged # Show staged changes
Rebase and Stash Operations
git rebase -i HEAD~n # Interactive rebase last n commits
git rebase main # Rebase current branch onto main
git commit --fixup=<hash> # Create fixup commit
git commit --squash=<hash> # Create squash commit
git rebase -i --autosquash # Auto-arrange fixup/squash commits
git stash # Stash changes
git stash -u # Stash including untracked files
git stash pop # Apply and remove stash
Common Git Workflows
Advanced Feature Development Workflow
For more complex feature development with clean commit history:
- Create feature branch:
git checkout -b feature/advanced-search - Work and commit incrementally:
git add . && git commit -m "Add search API endpoint" git add . && git commit -m "Implement search UI components" - Fix issues in previous commits:
# Fix something in the API commit git add api.js git commit --fixup=<api-commit-hash> # Or use squash for more changes git add ui.js git commit --squash=<ui-commit-hash> - Clean up history before merging:
git stash -u # Stash any uncommitted work git rebase -i --autosquash main - Push feature branch:
git push --force-with-lease origin feature/advanced-search - Create pull request and merge
Hotfix Workflow
- Create hotfix branch from main:
git checkout -b hotfix/critical-bug - Fix the issue and commit:
git commit -m "Fix critical bug" - Merge into main:
git checkout main && git merge hotfix/critical-bug - Tag the release:
git tag v1.0.1 - Push changes:
git push origin main --tags
Best Practices
Commit Messages
Write clear, descriptive commit messages:
# Good
git commit -m "Add user authentication with JWT tokens"
git commit -m "Fix memory leak in image processing"
# Bad
git commit -m "Update"
git commit -m "Fix stuff"
Commit Frequency
- Commit early and often
- Each commit should represent a logical unit of work
- Don’t commit broken code to main branch
Branch Naming
Use descriptive branch names:
feature/user-authentication
bugfix/login-error
hotfix/security-patch
Handling Common Issues
Undo Last Commit (Keep Changes)
git reset --soft HEAD~1
Undo Last Commit (Discard Changes)
git reset --hard HEAD~1
Discard Unstaged Changes
git checkout -- filename.txt # Specific file
git checkout -- . # All files
Remove File from Staging
git reset HEAD filename.txt
View Remote Repository URL
git remote -v
Change Remote URL
git remote set-url origin https://github.com/username/new-repo.git
Git Rebase
Rebase is a powerful Git feature that allows you to modify commit history, making it cleaner and more linear.
Interactive Rebase
Interactive rebase lets you edit, squash, reorder, or delete commits:
git rebase -i HEAD~3 # Rebase last 3 commits
git rebase -i main # Rebase from main branch
Rebase Options
During interactive rebase, you can:
pick- Keep commit as isreword- Change commit messageedit- Stop to modify commitsquash- Combine with previous commitfixup- Like squash but discard messagedrop- Remove commit entirely
Rebase vs Merge
Merge creates a merge commit:
git checkout main
git merge feature-branch # Creates merge commit
Rebase applies commits on top of another branch:
git checkout feature-branch
git rebase main # Moves commits to tip of main
Advanced Rebase Workflow
Here’s a common workflow for updating code in existing commits:
# 1. Stage your changes
git add <file>
# 2. Create a fixup commit for a specific commit
git commit --fixup=<commit_id>
# 3. Stash any unstaged changes (including untracked files)
git stash -u
# 4. Auto-squash fixup commits with interactive rebase
git rebase -i --autosquash <commit_id>^
# 5. Force push (use with caution on shared branches)
git push --force-with-lease
Alternative using squash:
git add <file>
git commit --squash=<commit_id>
git stash -u
git rebase -q --autosquash <commit_id>^
git push -f
Rebase Best Practices
Do:
- Rebase feature branches before merging
- Use interactive rebase to clean up commit history
- Use
--force-with-leaseinstead of-ffor safer force push
Don’t:
- Rebase commits that have been pushed to shared branches
- Rebase public history that others depend on
Common Rebase Commands
# Continue rebase after resolving conflicts
git rebase --continue
# Abort rebase and return to original state
git rebase --abort
# Skip current commit during rebase
git rebase --skip
# Rebase onto specific commit
git rebase --onto <new-base> <old-base> <branch>
Handling Rebase Conflicts
When conflicts occur during rebase:
- Fix conflicts in files
- Stage resolved files:
git add <file> - Continue rebase:
git rebase --continue
Or abort if needed: git rebase --abort
Git Stash
Temporarily save work without committing:
git stash # Stash tracked files
git stash -u # Stash including untracked files
git stash -a # Stash including ignored files
git stash push -m "WIP" # Stash with message
# List stashes
git stash list
# Apply stash
git stash pop # Apply and remove from stash
git stash apply # Apply but keep in stash
# Drop stash
git stash drop stash@{0} # Drop specific stash
git stash clear # Clear all stashes
.gitignore File
Create a .gitignore file to exclude files from version control:
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.min.js
# Environment files
.env
.env.local
# IDE files
.vscode/
.idea/
*.swp
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
logs/
Git Aliases (Optional)
Make Git commands shorter with aliases:
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
Now you can use git co instead of git checkout, git st instead of git status, etc.
Conclusion
Git is a powerful tool that becomes more valuable as you learn its features. Start with basic commands like add, commit, and push, then gradually explore advanced features like branching, merging, and rebasing. The key is consistent practice and building good habits from the beginning.
Remember: Git is designed to be safe - you can almost always recover from mistakes. Don’t be afraid to experiment and learn through hands-on experience.
Additional Resources
Happy coding!