Learn about Git, the distributed version control system that helps developers track changes, collaborate on code, and manage project history.
What is Git?
Git is a distributed version control system that helps developers track changes in their code, collaborate with others, and maintain a complete history of their project's development.
Understanding Git
Git is a tool that records changes to files over time, allowing you to recall specific versions later. It's designed to handle everything from small to very large projects with speed and efficiency.
Key Concepts
How Git Works
The Three States
1. Working Directory: Where you make changes to files
2. Staging Area: Where you prepare changes for commit
3. Repository: Where committed changes are permanently stored
Basic Workflow
1. Modify files in your working directory
2. Stage changes to the staging area
3. Commit changes to the repository
4. Push changes to a remote repository (optional)
Essential Git Commands
Getting Started
git init # Initialize a new repository
git clone <url> # Clone an existing repository
git status # Check the status of your repository
Making Changes
git add <file> # Stage specific files
git add . # Stage all changes
git commit -m "message" # Commit staged changes
git commit -am "message" # Stage and commit all tracked files
Viewing History
git log # View commit history
git log --oneline # Compact commit history
git show <commit> # Show details of a specific commit
git diff # Show unstaged changes
git diff --staged # Show staged changes
Working with Branches
git branch # List all branches
git branch <name> # Create a new branch
git checkout <branch> # Switch to a branch
git checkout -b <name> # Create and switch to a new branch
git merge <branch> # Merge a branch into current branch
git branch -d <branch> # Delete a branch
Remote Operations
git remote add origin <url> # Add a remote repository
git push origin <branch> # Push changes to remote
git pull origin <branch> # Pull changes from remote
git fetch origin # Download changes without merging
Git Workflows
Feature Branch Workflow
1. Create a feature branch from main
2. Make changes and commit them
3. Push the feature branch to remote
4. Create a pull request
5. Review and merge the changes
Gitflow Workflow
Trunk-Based Development
Advanced Git Concepts
Rebasing
git rebase <branch> # Replay commits on top of another branch
git rebase -i <commit> # Interactive rebase
Stashing
git stash # Save changes without committing
git stash pop # Apply and remove the latest stash
git stash list # List all stashes
Cherry-Picking
git cherry-pick <commit> # Apply a specific commit to current branch
Resetting
git reset --soft <commit> # Reset to commit, keep changes staged
git reset --mixed <commit> # Reset to commit, unstage changes
git reset --hard <commit> # Reset to commit, discard all changes
Git Configuration
User Setup
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
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
Best Practices
Commit Messages
Branch Naming
feature/user-authentication
, bugfix/login-error
Regular Commits
Common Scenarios
Undoing Changes
git checkout -- <file> # Discard changes in working directory
git reset HEAD <file> # Unstage changes
git revert <commit> # Create a new commit that undoes changes
Resolving Conflicts
1. Git will mark conflicts in files
2. Edit files to resolve conflicts
3. Stage resolved files
4. Complete the merge or rebase
Working with Remote
git remote -v # List remote repositories
git remote add <name> <url> # Add a new remote
git remote remove <name> # Remove a remote
Git Tools and Services
Git Hosting Platforms
GUI Clients
IDE Integration
Learning Resources
Documentation
Interactive Learning
Git has become an essential tool in modern software development, enabling teams to collaborate effectively and maintain high-quality codebases.