Git has become the de facto standard for version control in modern software development. Whether you're working on a solo project or collaborating with a global team, understanding Git fundamentals is crucial for your success as a developer.
Why Git Matters
Before diving into commands, let's understand why Git has become so ubiquitous:
- Distributed Version Control: Every developer has a complete copy of the repository
- Branching is Cheap: Create and merge branches with minimal overhead
- Speed: Most operations are performed locally
- Data Integrity: Git uses SHA-1 hashing to ensure data integrity
Essential Git Commands
1. Repository Initialization and Cloning
# Initialize a new repository
git init
# Clone an existing repository
git clone https://github.com/user/repo.git
# Clone with a specific branch
git clone -b branch-name https://github.com/user/repo.git
2. Basic Workflow Commands
# Check repository status
git status
# Add files to staging area
git add filename.txt
git add . # Add all changes
git add -p # Interactive staging
# Commit changes
git commit -m "Descriptive commit message"
git commit -am "Add and commit in one step"
# View commit history
git log
git log --oneline --graph --all
3. Working with Branches
# List branches
git branch
git branch -a # Include remote branches
# Create and switch to new branch
git checkout -b feature-branch
# Or using newer syntax
git switch -c feature-branch
# Switch between branches
git checkout main
git switch main
# Merge branches
git merge feature-branch
# Delete branch
git branch -d feature-branch # Safe delete
git branch -D feature-branch # Force delete
4. Remote Repository Operations
# Add remote repository
git remote add origin https://github.com/user/repo.git
# View remotes
git remote -v
# Push changes
git push origin main
git push -u origin feature-branch # Set upstream
# Pull changes
git pull origin main
# Or fetch and merge separately
git fetch origin
git merge origin/main
5. Undoing Changes
# Discard working directory changes
git checkout -- filename.txt
git restore filename.txt # Newer syntax
# Unstage files
git reset HEAD filename.txt
git restore --staged filename.txt # Newer syntax
# Amend last commit
git commit --amend -m "New commit message"
# Reset to previous commit
git reset --soft HEAD~1 # Keep changes staged
git reset --mixed HEAD~1 # Keep changes unstaged
git reset --hard HEAD~1 # Discard all changes
Understanding Git's Three States
Git manages files in three main states:
- Working Directory: Where you modify files
- Staging Area (Index): Where you prepare commits
- Repository: Where Git permanently stores commits
Working Directory → Staging Area → Repository
(git add) (git commit)
Best Practices for Git Usage
1. Write Meaningful Commit Messages
# Good
git commit -m "Add user authentication with JWT tokens"
# Bad
git commit -m "Fixed stuff"
2. Commit Early and Often
Make small, logical commits that represent a single unit of work:
# Instead of one large commit
git add .
git commit -m "Add entire feature"
# Make multiple focused commits
git add authentication.js
git commit -m "Add authentication middleware"
git add routes/user.js
git commit -m "Add user routes with authentication"
git add tests/auth.test.js
git commit -m "Add authentication tests"
3. Use .gitignore Effectively
Create a .gitignore
file to exclude unnecessary files:
# Dependencies
node_modules/
vendor/
# Environment files
.env
.env.local
# IDE files
.vscode/
.idea/
# OS files
.DS_Store
Thumbs.db
# Build outputs
dist/
build/
*.log
4. Keep Your History Clean
# Before pushing, clean up commits
git rebase -i HEAD~3 # Interactive rebase last 3 commits
# Update branch with latest changes from main
git checkout feature-branch
git rebase main
Common Git Workflows
Feature Branch Workflow
# 1. Create feature branch
git checkout -b feature/user-authentication
# 2. Make changes and commit
git add .
git commit -m "Implement user login"
# 3. Keep branch updated
git fetch origin
git rebase origin/main
# 4. Push branch
git push origin feature/user-authentication
# 5. Create pull request and merge
Hotfix Workflow
# 1. Create hotfix from production branch
git checkout production
git checkout -b hotfix/security-patch
# 2. Fix issue and commit
git add security.js
git commit -m "Fix XSS vulnerability in user input"
# 3. Merge to production and main
git checkout production
git merge hotfix/security-patch
git checkout main
git merge hotfix/security-patch
# 4. Delete hotfix branch
git branch -d hotfix/security-patch
Troubleshooting Common Issues
Merge Conflicts
# When conflicts occur
git status # See conflicted files
# Edit files to resolve conflicts
# Look for <<<<<<< HEAD markers
# After resolving
git add resolved-file.txt
git commit -m "Resolve merge conflicts"
Accidentally Committed to Wrong Branch
# Move last commit to correct branch
git checkout correct-branch
git cherry-pick wrong-branch
git checkout wrong-branch
git reset --hard HEAD~1
Lost Commits
# Find lost commits
git reflog
# Restore lost commit
git checkout -b recovery-branch abc1234
Git Configuration Tips
# Set user information
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Enable color output
git config --global color.ui auto
# Set default editor
git config --global core.editor "vim"
# Create 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.lg "log --oneline --graph --all"
# View configuration
git config --list
Conclusion
Mastering Git basics is an investment that pays dividends throughout your development career. These fundamental commands and concepts form the foundation for more advanced Git operations and workflows.
Start with these essentials, practice them regularly, and gradually expand your Git knowledge. Remember, the best way to learn Git is by using it daily in your projects.
Resources for Further Learning
- Pro Git Book - Comprehensive Git reference
- Git Documentation - Official Git documentation
- GitHub Learning Lab - Interactive Git tutorials
- Atlassian Git Tutorial - Visual Git guides
Keep practicing, and soon these commands will become second nature. Happy coding!
Share this article
David Childs
Consulting Systems Engineer with over 10 years of experience building scalable infrastructure and helping organizations optimize their technology stack.