Introduction to Git
Earn 25 points (50 with Pro) in two steps
- ① Read through the lesson — each section gets a ✓ as you scroll through it.
- ② When every section has a ✓, tap Complete lesson.
0 of 10 read · keep scrolling
✦ See fewer ads and earn double points — 50 a lesson instead of 25 — with Pro
Introduction to Git: The Foundation of Modern Software Development
In the landscape of modern software development, the ability to track changes, collaborate with teammates, and manage project history is not merely a convenience—it is a fundamental necessity. Whether you are working on a solo project, contributing to a massive open-source library, or working within a professional development team, version control is the safety net that prevents catastrophic data loss and provides a clear audit trail for every line of code written. Git, a distributed version control system, has become the industry standard for this purpose.
Understanding Git is more than just learning a set of commands; it is about adopting a mindset where every change is deliberate, documented, and reversible. Without a tool like Git, developers often resort to naming folders as "Project_Final," "Project_Final_v2," or "Project_New_Backup," which is an unsustainable and error-prone way to work. Git removes this chaos by providing a structured environment where you can experiment, branch out to explore new features, and merge your work back into a stable codebase with confidence.
This lesson will guide you through the core concepts of Git, how it operates under the hood, and how you can implement it in your daily workflow. We will move beyond the basic "how-to" and explore the "why," ensuring that you build a solid foundation that will serve you throughout your career in technology. By the end of this module, you will not only know how to run Git commands, but you will also understand the logic that makes Git the most powerful tool in your developer toolkit.
Not read yet
What is Version Control and Why Git?
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. Imagine you are writing a document and you decide to rewrite a paragraph. If you save the file, the old version is gone forever unless you manually created a copy. Version control systems automate this process, keeping a detailed history of who changed what, when, and—most importantly—why.
There are two main categories of version control: Centralized and Distributed. In a Centralized Version Control System (CVCS), there is a single server that contains all the versioned files, and a number of clients check out files from that central place. This has a major drawback: if the server goes down, no one can save changes or collaborate. If the hard drive on the server is corrupted, the entire history of the project is lost.
Git is a Distributed Version Control System (DVCS). In Git, clients do not just check out the latest snapshot of the files; they fully mirror the repository, including its entire history. If any server dies, and these clients were collaborating via it, any of the client repositories can be copied back up to the server to restore it. Every clone is really a full backup of all the data.
Callout: Distributed vs. Centralized Systems In a centralized system, the server is the single point of failure. If the central repository is inaccessible, your development workflow grinds to a halt. In contrast, Git's distributed nature means that every developer has the entire project history on their local machine. This allows you to commit changes, view logs, and compare versions even when you are offline or disconnected from the network.
Why Git Won the Industry
Git has become the de-facto standard for several reasons:
- Speed: Git is incredibly fast because most operations are performed locally rather than communicating with a remote server.
- Branching and Merging: Git handles branches as simple, lightweight pointers, making it incredibly easy to switch between features without affecting the main codebase.
- Integrity: Git uses a mechanism called SHA-1 hashing to ensure that the content of files is never changed without Git knowing about it. You can be certain that the file you check out today is exactly what you committed yesterday.
- Community Support: Because Git is ubiquitous, you will find answers to almost any problem you encounter on platforms like Stack Overflow or through community documentation.
Not read yet
Installing and Configuring Git
Before you can use Git, you must have it installed on your system. While Git is available on Windows, macOS, and Linux, the setup process varies slightly. Most developers prefer using the command line, though graphical interfaces exist. For this course, we will focus on the command line, as it gives you the deepest understanding of how Git functions.
Initial Configuration
Once installed, the first thing you should do is configure your identity. Git associates your commits with your name and email address. This information is embedded into every commit you make, so it is important to get it right. Open your terminal or command prompt and run the following commands:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
The --global flag ensures that these settings apply to every project on your computer. If you need to use a different email address for a specific work project, you can navigate to that project folder and run the same commands without the --global flag.
Verifying Settings
To see your configuration settings at any time, you can use:
git config --list
This will display a list of all current configuration values. If you ever need to edit these manually, you can open the global configuration file in your default text editor using:
git config --global --edit
Tip: Choosing a Text Editor Git often needs to open a text editor to ask for commit messages or configuration changes. You can set your preferred editor (like VS Code, Nano, or Vim) using:
git config --global core.editor "code --wait"for VS Code. This makes the experience much smoother.
Not read yet
The Three States of Git
Understanding the "Three States" is the single most important concept for mastering Git. Files in a Git project can exist in one of three states: Committed, Modified, and Staged.
- Modified: You have changed the file but have not committed it to your database yet.
- Staged: You have marked a modified file in its current version to go into your next commit snapshot.
- Committed: The data is safely stored in your local database.
This leads to the three main sections of a Git project: the Working Directory, the Staging Area, and the Git Directory.
- Working Directory: This is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.
- Staging Area: This is a file, generally contained in your Git directory, that stores information about what will go into your next commit.
- Git Directory: This is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.
The Basic Workflow
The basic Git workflow follows this pattern:
- You modify files in your working directory.
- You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area.
- You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
Not read yet
Initializing a Repository
To start tracking a project with Git, you need to turn your project folder into a Git repository. Navigate to your project folder in your terminal and run:
git init
This command creates a new subdirectory named .git that contains all of your necessary repository files—the skeleton of a Git repository. At this point, nothing in your project is tracked yet. To start tracking files, you must add them.
Checking Status
The git status command is your best friend. It tells you exactly what is happening in your working directory.
git status
If you just initialized a repo, it will tell you that you are on the master or main branch and that there are "Untracked files." Untracked files are files that Git sees in your directory but that you haven't added to the repository yet.
Staging and Committing Files
To tell Git to start tracking a file, you use the git add command.
git add filename.txt
Now, if you run git status again, you will see that the file is "Changes to be committed." You have moved the file from the Untracked state to the Staged state. To finalize this and save the snapshot, you use git commit.
git commit -m "Initial commit of the project"
The -m flag allows you to provide a commit message directly in the command line. A good commit message is concise but descriptive. It should explain why you made the changes, not just what you changed.
Warning: Never use empty messages While
git commitwithout the-mflag will open your text editor to allow for a longer message, avoid just leaving it blank. A commit history full of "fixed stuff" or "updates" is useless to you and your team six months down the road.
Staging Multiple Files
If you have multiple files modified, you can stage them all at once using:
git add .
The dot represents the current directory. This is a very common command, but be careful: it stages everything that has changed in your directory, including new files and deletions. If you only want to stage specific files, list them individually.
Not read yet
Understanding the Commit History
Once you have made a few commits, you will want to see what has happened in the project. The git log command provides a history of your project.
git log
This command lists all the commits in your repository, starting with the most recent. Each entry includes the commit hash (a unique string of characters), the author, the date, and the commit message.
Improving the Log View
The default git log output can be verbose. Many developers use a customized version to get a cleaner view:
git log --oneline --graph --all
--oneline: Shows each commit on a single line.--graph: Displays an ASCII graph of the branch history.--all: Shows all branches, not just the current one.
This gives you a clear visual representation of how your project has evolved over time.
Branching: The Power of Git
Branching is what makes Git truly powerful. In Git, a branch is simply a lightweight, movable pointer to one of your commits. The default branch is usually named main or master.
When you create a new branch, Git creates a new pointer for you to move around. Let's say you want to work on a new feature. You can create a branch and switch to it:
git checkout -b feature-login
This command does two things: it creates a branch named feature-login and switches your working directory to that branch. Any commits you make now will be recorded on this branch, leaving your main branch untouched.
Merging Branches
Once you have finished your work on the feature branch and committed your changes, you can switch back to the main branch and merge the feature branch into it.
git checkout main
git merge feature-login
If everything goes well, Git performs a "fast-forward" merge, moving the main pointer to the latest commit of your feature branch. If there are conflicts (e.g., you changed the same line of a file in both branches), Git will stop and ask you to resolve them manually.
Callout: The Philosophy of Branching Think of branches as parallel universes. You can experiment, break things, and try wild ideas on a feature branch without ever risking the stability of your production-ready
mainbranch. Once your feature is tested and working, you merge it back into the main universe, effectively bringing that functionality into the core project.
Not read yet
Best Practices for Git Workflows
As you begin using Git in projects, following a few industry-standard best practices will save you and your teammates a lot of headaches.
1. Commit Often, Commit Small
A commit should represent a single logical change. If you are fixing a bug and adding a new feature, those should be two separate commits. This makes it much easier to revert changes if something goes wrong later.
2. Write Meaningful Commit Messages
Follow the standard convention:
- Use a short, imperative sentence for the first line (e.g., "Add login validation").
- If you need more detail, add a blank line and then a longer description.
- Reference issue numbers if you are using a tracker like Jira or GitHub Issues.
3. Use .gitignore
Not every file belongs in your repository. Compiled binaries, temporary files, logs, and sensitive environment variables (like API keys) should never be committed. Create a file named .gitignore in your project root and list the files or patterns you want Git to ignore.
Example .gitignore:
# Ignore build artifacts
dist/
node_modules/
# Ignore log files
*.log
# Ignore environment variables
.env
4. Pull Before You Push
If you are working with others, always pull the latest changes from the remote repository before you start working or pushing your own code. This helps you catch conflicts early.
Not read yet
Common Pitfalls and How to Avoid Them
Even experienced developers run into trouble with Git. Here are some common mistakes and how to navigate them.
"I committed the wrong file!"
If you haven't pushed your code to a remote server yet, you can fix your last commit using:
git commit --amend
This opens your last commit message so you can change it, or allows you to add files that you accidentally left out of the previous commit.
"I accidentally deleted a file, but I haven't committed it yet."
If you deleted a file in your working directory and want to restore it from the last commit, use:
git checkout -- filename.txt
This effectively discards the changes you made in your working directory and replaces the file with the version currently in the repository.
"I have a merge conflict, what do I do?"
Merge conflicts happen when Git cannot automatically determine how to combine two branches. Git will mark the file with conflict markers:
<<<<<<< HEAD
code on your current branch
=======
code on the branch you are merging
>>>>>>> branch-name
You must open the file, delete the markers, edit the code to the desired final state, save the file, and then run git add <file> followed by git commit to finish the merge.
Not read yet
Comparison Table: Common Git Commands
| Command | Purpose |
|---|---|
git init |
Initializes a new local repository. |
git clone [url] |
Downloads an existing repository. |
git add [file] |
Stages a file for the next commit. |
git commit -m "[msg]" |
Saves the staged changes permanently. |
git status |
Shows the current state of files. |
git branch |
Lists, creates, or deletes branches. |
git checkout [branch] |
Switches to a different branch. |
git merge [branch] |
Combines the current branch with another. |
git log |
Displays the history of commits. |
git remote -v |
Lists connected remote repositories. |
Frequently Asked Questions
Is Git the same as GitHub?
No. Git is the version control software installed on your machine. GitHub is a hosting service for Git repositories. You can use Git without GitHub (using other services like GitLab or Bitbucket), and you can use GitHub as a place to store your code. Think of Git as the engine and GitHub as the garage where you store your car.
How do I undo a commit that I already pushed to a remote server?
If you have already pushed, you should generally use git revert. This creates a new commit that is the exact opposite of the one you want to undo. It is safer than "resetting" history because it maintains the integrity of the project timeline for other collaborators.
Can I use Git for non-code files?
Yes, but Git is optimized for text files. While it can store images and binary files, it cannot "diff" them effectively (i.e., it cannot show you what changed inside a PNG file). If your project involves large binary assets, look into Git LFS (Large File Storage).
Not read yet
Conclusion and Key Takeaways
Mastering Git is a journey, not a destination. By understanding the core mechanics—the staging area, the commit history, and the power of branching—you gain control over your development process. Git is not just about tracking files; it is about creating a reliable, collaborative, and professional workflow.
Key Takeaways:
- Distributed Nature: Git stores the entire project history locally, providing speed, offline access, and robust data protection.
- The Three States: Always be aware of whether your files are modified, staged, or committed. Use
git statusfrequently to keep track of your environment. - Branching is Essential: Never work on the main branch for new features. Create a new branch for every task to keep your code organized and stable.
- Commits are Snapshots: A commit is a point in time. Make them small, frequent, and descriptive to create a meaningful project history.
- Use
.gitignore: Keep your repository clean by excluding build files and sensitive data from the start. - Conflict Resolution: Do not fear merge conflicts. They are a natural part of collaborative work, and Git provides the tools to resolve them systematically.
- Continuous Learning: Git is vast. Start with these basics, and as you grow, explore advanced features like
rebase,cherry-pick, andhooksto further optimize your workflow.
By consistently applying these principles, you will find that Git transforms from a confusing tool you "have to use" into an indispensable partner that empowers you to write better code, collaborate more effectively, and experiment with total peace of mind. Happy coding!
Not read yet
Each section gets a ✓ as you scroll through it. Tap the button to jump to the next one.
- What is Version Control
- What is Version Control Quiz5q
- Benefits of Version Control
- Benefits of Version Control Quiz5q
- Introduction to Git
- Introduction to Git Quiz5q
- Git vs GitHub Differences
- Git vs GitHub Differences Quiz5q
- Understanding Repositories
- Understanding Repositories Quiz5q
- Commits and Commit History
- Commits and Commit History Quiz5q
- Branches and Branching Strategies
- Branches and Branching Strategies Quiz5q
- GitHub Accounts and Profiles
- GitHub Accounts and Profiles Quiz5q
- GitHub Organizations
- GitHub Organizations Quiz5q
- GitHub Enterprise Options
- GitHub Enterprise Options Quiz5q
- GitHub Flow for Collaboration
- GitHub Flow for Collaboration Quiz5q
- Markdown Basics
- Markdown Basics Quiz5q
- GitHub Desktop Application
- GitHub Desktop Application Quiz5q
- GitHub Mobile
- GitHub Mobile Quiz5q
- Repository Structure Overview
- Repository Structure Overview Quiz5q
- README Files Best Practices
- README Files Best Practices Quiz5q
- LICENSE Files and Open Source
- LICENSE Files and Open Source Quiz5q
- CONTRIBUTING Guidelines
- CONTRIBUTING Guidelines Quiz5q
- CODEOWNERS File
- CODEOWNERS File Quiz5q
- SECURITY Policy Files
- SECURITY Policy Files Quiz5q
- Creating Repositories
- Creating Repositories Quiz5q
- Repository Templates
- Repository Templates Quiz5q
- Managing Files in Repositories
- Managing Files in Repositories Quiz5q
- Repository Insights and Metrics
- Repository Insights and Metrics Quiz5q
- Stars and Feature Previews
- Stars and Feature Previews Quiz5q
- Dependency Insights
- Dependency Insights Quiz5q
- Introduction to Issues
- Introduction to Issues Quiz5q
- Pull Requests Fundamentals
- Pull Requests Fundamentals Quiz5q
- GitHub Discussions
- GitHub Discussions Quiz5q
- Linking PRs to Issues
- Linking PRs to Issues Quiz5q
- Issue and PR Templates
- Issue and PR Templates Quiz5q
- Filters and Assignments
- Filters and Assignments Quiz5q
- Introduction to GitHub Actions
- Introduction to GitHub Actions Quiz5q
- GitHub Actions Components
- GitHub Actions Components Quiz5q
- Introduction to GitHub Copilot
- Introduction to GitHub Copilot Quiz5q
- Copilot Agents and Agent Mode
- Copilot Agents and Agent Mode Quiz5q
- Multi-Model Support in Copilot
- Multi-Model Support in Copilot Quiz5q
- Copilot Plans Comparison
- Copilot Plans Comparison Quiz5q
- Introduction to GitHub Projects
- Introduction to GitHub Projects Quiz5q
- Project Layout Options
- Project Layout Options Quiz5q
- Configuring Projects
- Configuring Projects Quiz5q
- Labels and Milestones
- Labels and Milestones Quiz5q
- Project Workflows
- Project Workflows Quiz5q
- Saved Replies and Assignees
- Saved Replies and Assignees Quiz5q
- Project Insights and Analytics
- Project Insights and Analytics Quiz5q
- Two-Factor Authentication
- Two-Factor Authentication Quiz5q
- Passkeys for Authentication
- Passkeys for Authentication Quiz5q
- Repository Access Permissions
- Repository Access Permissions Quiz5q
- Organization Roles
- Organization Roles Quiz5q
- Enterprise Managed Users
- Enterprise Managed Users Quiz5q
- Copilot Policy Management
- Copilot Policy Management Quiz5q
- Benefits of Open Source
- Benefits of Open Source Quiz5q
- GitHub Sponsors
- GitHub Sponsors Quiz5q
- Supporting Open Source Projects
- Supporting Open Source Projects Quiz5q
- Following Users and Organizations
- Following Users and Organizations Quiz5q
- GitHub Marketplace
- GitHub Marketplace Quiz5q
- InnerSource Principles
- InnerSource Principles Quiz5q
Spend your points to unlock exam-style practice questions
Spend points to unlock GH-900 exam-style practice questions.
Practice for GH-900