Comprehensive guides, references, and resources to help you master Git.
Git is a distributed version control system that allows you to track changes in your code, collaborate with others, and maintain a history of your project. This guide will help you get started with Git.
Before you can start using Git, you need to install it on your computer:
brew install git
or download
the installer.apt-get install git
for Debian/Ubuntu).
After installing Git, you should set up your identity:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
To start tracking a project with Git:
git init
.git add .
.git commit -m "Initial commit"
.
Tip: If you're new to the command line, don't worry! There are many graphical Git clients available that make it easier to get started.
Understanding the basic Git workflow is essential for effective version control. Here are the fundamental concepts and commands you'll use regularly.
Git has three main states that your files can reside in:
git status
Shows the status of files in your working directory and staging area.
git add filename # Add a specific file
git add . # Add all files
Adds files to the staging area, preparing them for a commit.
git commit -m "Descriptive message"
Records the changes in the repository with a message describing what was changed.
git log
Shows a log of all commits, with details like author, date, and commit message.
Best Practice: Write clear, descriptive commit messages that explain what changes were made and why. This helps your team (and future you) understand the project's history.
Branching is one of Git's most powerful features. It allows you to diverge from the main line of development and continue working without affecting that main line.
git branch branch-name # Create a branch
git checkout -b branch-name # Create and switch to the branch
git checkout branch-name
git branch # List local branches
git branch -a # List all branches including remote
Merging is the process of integrating changes from one branch into another. This is typically done when a feature or bugfix is complete and ready to be incorporated into the main codebase.
git checkout main # Switch to the target branch
git merge feature-branch # Merge the feature branch into main
Sometimes Git can't automatically merge changes, resulting in conflicts that need manual resolution:
<<<<<<<
, =======
, and
>>>>>>>
)
git add
git commit
Pro Tip: Use git merge --abort
to cancel a
merge that has conflicts and start over.
Several branching strategies have emerged to help teams work effectively:
A robust workflow that uses multiple branches for features, releases, and hotfixes:
A simpler alternative with just two types of branches:
Once you're comfortable with the basics, these advanced Git features will help you become more productive and handle complex scenarios.
Rebasing is an alternative to merging that rewrites commit history to create a cleaner, linear sequence of commits:
git checkout feature-branch
git rebase main
Warning: Never rebase commits that have been pushed to a public repository. This can cause problems for other developers who have based their work on those commits.
Interactive rebasing allows you to modify commits as you rebase them:
git rebase -i HEAD~3 # Interactively rebase the last 3 commits
This opens an editor where you can:
Cherry-picking allows you to apply specific commits from one branch to another:
git cherry-pick commit-hash
Stashing allows you to save uncommitted changes temporarily so you can switch to another branch:
git stash # Stash changes
git stash list # List stashes
git stash apply # Apply the most recent stash
git stash apply stash@{n} # Apply a specific stash
git stash drop # Remove the most recent stash
git stash pop # Apply and remove the most recent stash
Submodules allow you to keep a Git repository as a subdirectory of another Git repository:
git submodule add https://github.com/example/repo.git path/to/submodule
git submodule update --init --recursive # Initialize and update submodules
Continue to Command Reference
This reference provides a quick overview of common Git commands grouped by functionality.
Command | Description |
---|---|
Setup and Configuration | |
git config
|
Configure Git settings |
git help
|
Display help information |
Creating Projects | |
git init
|
Create a new local repository |
git clone
|
Clone a repository |
Basic Snapshotting | |
git add
|
Add files to staging area |
git status
|
Check repository status |
git commit
|
Commit changes |
git reset
|
Reset staging area to match most recent commit |
git rm
|
Remove files from working directory |
git mv
|
Move or rename files |
Branching and Merging | |
git branch
|
List, create, or delete branches |
git checkout
|
Switch branches or restore files |
git merge
|
Merge branches |
git rebase
|
Reapply commits on top of another base |
git tag
|
Create, list, delete, or verify tags |
Sharing and Updating | |
git fetch
|
Download objects and refs from another repository |
git pull
|
Fetch and integrate with another repository or branch |
git push
|
Update remote refs along with associated objects |
git remote
|
Manage remote repositories |
For a complete reference of all Git commands, visit the official Git documentation.
Expand your Git knowledge with these additional resources:
The official and comprehensive Git book, written by Scott Chacon and Ben Straub.
A handy reference for the day-to-day use of Git.
A comprehensive introduction to Git for new users.
A quick introduction to Git and GitHub for beginners.
Comprehensive course covering all Git features and commands.
Master advanced Git workflows and problem-solving.
Practice Git commands in an interactive environment: