Learn Git

Git is a distributed version control system that records every change to your project so you can track history, undo mistakes, and collaborate with a team without stepping on each other's work.

What Is Version Control?

Version control is a system that records changes to a set of files over time so that you can recall specific versions later. Instead of keeping folders named project-final, project-final-v2, and project-really-final, a version control system stores every meaningful change as a snapshot, along with who made it, when, and why. This lets any file, or the entire project, be restored to an earlier state at any moment.

Why Developers Use Git

Git was built to solve real problems that teams run into every day: losing work, overwriting a colleague's changes, not knowing why a line of code was added, and struggling to experiment safely. Git solves these by keeping a complete, searchable history and by making it cheap to create parallel lines of work called branches.

  • Complete history of every file, viewable and searchable at any time
  • Fully functional offline, since the whole history lives on your machine
  • Lightweight branching so you can experiment without risk
  • Distributed by design, so every clone is a full backup of the project
  • Free, open source, and maintained by a huge global community
  • The foundation for platforms like GitHub, GitLab, and Bitbucket

Example

$ git --version
git version 2.44.0

Git vs. Centralized Version Control

Older systems such as CVS or Subversion (SVN) are centralized: a single server holds the full history, and developers check out a working copy of the current files only. If the server goes down, nobody can see project history. Git is distributed: every developer's clone contains the entire history of the project, not just the latest snapshot.

AspectCentralized (e.g. SVN)Distributed (Git)
Where history livesOnly on the central serverOn every clone of the repository
Working offlineVery limitedCommit, branch, and view history freely
SpeedMost operations need the networkMost operations are local and instant
BranchingHeavyweight, often avoidedCheap and constant, encouraged

Git was created in 2005 by Linus Torvalds, the creator of Linux, to manage the Linux kernel's source code after the team lost access to the proprietary tool they had been using. It needed to be fast, handle thousands of contributors, and never lose a single change. Those requirements shaped Git into the tool used by millions of developers today.

Example

$ git help
These are common Git commands used in various situations:

start a working area
   clone     Clone a repository into a new directory
   init      Create an empty Git repository

work on the current change
   add       Add file contents to the index
   status    Show the working tree status

grow, mark and tweak your history
   commit    Record changes to the repository
   log       Show commit logs
Note: Git and GitHub are not the same thing. Git is the version control tool that runs on your computer. GitHub is a website that hosts Git repositories online and adds collaboration features like pull requests and issues. You can use Git fully without ever creating a GitHub account.

Example

$ git config --list
user.name=Not Set Yet
user.email=Not Set Yet
core.autocrlf=input
init.defaultbranch=main
Note: You do not need to install anything to keep reading, but the next chapter walks through installing Git and setting your name and email so your commits are properly identified.

Exercise: Git Introduction

What type of version control system is Git?

Frequently Asked Questions

What is the difference between Git and GitHub?
Git is the version control tool that runs on your machine. GitHub is a website that hosts Git repositories and adds pull requests, issues and access control. Git works fully offline without GitHub.
How do I undo the last commit in Git?
Use git reset --soft HEAD~1 to undo the commit but keep the changes staged, or --hard to discard them entirely. If the commit is already pushed, git revert is safer because it does not rewrite history.
What is the difference between merge and rebase?
Merge keeps both histories and adds a merge commit. Rebase replays your commits on top of the other branch, producing a straight line. Rebase gives a cleaner history but rewrites commits, so avoid it on shared branches.
How long does it take to learn Git?
A day for the everyday commands: clone, add, commit, push and pull. Branching and merging take a week or two, and conflict resolution mostly comes from experience.