Introduction
This tutorial will take you from Git basics through advanced workflows. By the end, you’ll understand how to:- Manage project history with commits
- Work with branches for parallel development
- Collaborate with others using remotes
- Resolve conflicts when they arise
- Use Git effectively in real-world scenarios
If you haven’t installed Git yet, check the Installation Guide first.
Setting Up
Before starting, configure your identity:Creating Your First Repository
Let’s import a new project into Git. Assume you have a project directory you want to track:Initialize the Repository
.git has been created.Stage All Files
Take a snapshot of all files in the current directory:This snapshot is now stored in the staging area (also called the “index”).
Making Changes
Now let’s modify some files and commit the changes:Check Staged Changes
See what’s about to be committed:Without
--cached, git diff shows unstaged changes.Commit Message Best Practices
Though not required, it’s good practice to begin commit messages with a single short line (no more than 50 characters) summarizing the change, followed by a blank line and a more thorough description.
git format-patch uses the title as the email Subject line.
Example:
Automatic Staging
Instead of runninggit add separately, you can use:
Understanding How Git Tracks Content
Git Tracks Content, Not Files
Git Tracks Content, Not Files
Many revision control systems have an
add command to start tracking a file. Git’s add command is different:- Other systems:
addtells the system to start tracking a file - Git:
addtakes a snapshot of the file’s current content and stages it
add is used for both:- New files (start tracking them)
- Modified files (stage their current content)
Viewing Project History
At any point, view your commit history:Managing Branches
Branches are Git’s killer feature. They’re lightweight pointers to commits, making them perfect for experimentation.Creating and Switching Branches
Switch to the New Branch
experimental branch.You can also use
git checkout experimental on older Git versions.Creating and Switching in One Command
Working with Divergent Branches
Let’s make different changes on different branches:Merging Branches
To combine changes fromexperimental into master:
Deleting Branches
Once a branch is merged, you can delete it:Collaboration Basics
Git excels at helping multiple people work on the same project.Cloning a Repository
Suppose Alice has a repository that Bob wants to contribute to:myrepo with a complete copy of the project history.
Making and Sharing Changes
Understanding Pull
Thepull command performs two operations:
- Fetch: Downloads changes from the remote branch
- Merge: Merges them into the current branch
Inspecting Before Merging
Alice can examine Bob’s changes before merging:HEAD..FETCH_HEAD means “show everything reachable from FETCH_HEAD but not from HEAD.”
Using Remote Shortcuts
Instead of typing the full path repeatedly, define a remote:Automatic Remote Configuration
When Bob cloned Alice’s repository, Git automatically configured a remote:- Remote name:
origin - Remote URL: Alice’s repository
- Remote tracking branch:
origin/master
Remote Protocols
Git supports multiple protocols for accessing remote repositories:Exploring History
Commit References
Git provides many ways to refer to commits:Merge Commits
Merge commits have multiple parents:Tagging Commits
Create named references to important commits:Searching History
Visualizing History
For projects with frequent merges,gitk provides a better visualization:
Undoing Changes
Dangerous Commands
- Removes the last commit from the branch
- Deletes all working directory changes
- If this branch is the only one with those commits, they’re lost forever
Safe Alternative: Revert
To undo changes you’ve already pushed:Everyday Git Workflows
Individual Developer
Essential commands for solo development:git init- Create a repositorygit add- Stage changesgit commit- Save changesgit status- Check current stategit diff- View changesgit log- View historygit branch- Manage branchesgit switch- Change branchesgit merge- Combine branchesgit tag- Mark releases
Team Participant
Additional commands for collaboration:git clone- Copy a repositorygit pull- Update from remotegit push- Share changesgit fetch- Download without merginggit remote- Manage remotes
Example: Daily Development Workflow
Next Steps
You now have a solid foundation in Git! Continue learning:Advanced Branching
Learn rebasing, cherry-picking, and advanced branch workflows
Remote Workflows
Master push, pull, and distributed collaboration
Git Internals
Understand the object database and how Git works
Best Practices
Workflows and conventions for teams
Further Reading
The tutorial has covered the essentials. To fully understand Git’s depth and power, learn about:The Object Database
The Object Database
The elegant system Git uses to store:
- Files (blobs)
- Directories (trees)
- Commits
- Tags
git help tutorial-2The Index File
The Index File
A cache of the directory tree state used to:
- Create commits
- Check out working directories
- Hold trees involved in merges
git help tutorial-2Other Useful Topics
Other Useful Topics
- git-format-patch and git-am: Convert commits to email patches
- git-bisect: Binary search through history to find bugs
- gitworkflows: Recommended workflows
- giteveryday: Essential commands for daily use
- User Manual: Comprehensive guide to Git
