A version control system (VCS), also known as revision control or source
control, is software that tracks and manages changes to files over time,
primarily used for software development but applicable to any type of file. It
allows developers to collaborate, revert to previous versions, and compare
changes, making it a crucial tool for managing projects and ensuring code
integrity.
Key Concepts and Benefits:
Tracking Changes:
VCS records all modifications to files, including additions, deletions, and
modifications, allowing users to see what has been changed, when, and by
whom.
Reverting to Previous Versions:
If a mistake is made or a previous version is needed, VCS enables users to
easily revert to any saved version of the code or files.
Collaboration:
VCS facilitates collaboration among multiple developers by providing a
centralized or distributed system for sharing and integrating changes.
Branching:
VCS allows for the creation of separate branches, which are essentially
independent lines of development, to isolate changes and experiments.
Merging:
VCS enables the integration of changes from different branches back into
the main line of development.
Backup and Recovery:
VCS acts as a backup system for code and files, allowing for recovery in case
of data loss or corruption.
Types of Version Control Systems:
Centralized Version Control Systems (CVCS):
These systems have a single central repository where all code and changes are
stored. Examples include CVS and Subversion (SVN).
Distributed Version Control Systems (DVCS):
These systems distribute the entire repository to each developer's local
machine, allowing for offline work and faster operations. Git is the most
popular example of a DVCS.
Hybrid Version Control Systems:
These systems combine features of both centralized and distributed systems.
Popular VCS Examples:
Git: A widely used DVCS known for its flexibility, speed, and branching
capabilities.
Subversion (SVN): A centralized VCS that is still used in some projects.
Mercurial: A DVCS similar to Git, but with a different design philosophy.
Perforce: A centralized VCS often used for large projects with complex
workflows.
CVS: An older, centralized VCS that has largely been replaced by newer
systems like Git and SVN.
Why is Version Control Important?
Version control is essential for modern software development and other
collaborative projects for several reasons:
Reduces Risks:
It minimizes the risk of losing work due to human error or system failures.
Improves Collaboration:
It enables teams to work together efficiently and effectively on the same
project.
Enhances Code Quality:
It helps track changes, identify bugs, and maintain a clear history of the
project's evolution.
Facilitates Teamwork:
It allows for better communication, coordination, and knowledge sharing
among team members.
Increases Productivity:
By streamlining the development process and reducing errors, VCS helps
teams work faster and more efficiently.
GIT
What is Git?
Git is a popular version control system.
It was created by Linus Torvalds in 2005, and has been
maintained by Junio Hamano since then.
It is used for:
Tracking code changes
Tracking who made changes
Coding collaboration
Key Git Concepts
Repository: A folder where Git tracks your project and its
history.
Clone: Make a copy of a remote repository on your
computer.
Stage: Tell Git which changes you want to save next.
Commit: Save a snapshot of your staged changes.
Branch: Work on different versions or features at the
same time.
Merge: Combine changes from different branches.
Pull: Get the latest changes from a remote repository.
Push: Send your changes to a remote repository.
Working with Git
Initialize Git on a folder, making it a Repository
Git now creates a hidden folder to keep track of changes
in that folder
When a file is changed, added or deleted, it is
considered modified
You select the modified files you want to Stage
The Staged files are Committed, which prompts Git to
store a permanent snapshot of the files
Git allows you to see the full history of every commit.
You can revert back to any previous commit.
Git does not store a separate copy of every file in every
commit, but keeps track of changes made in each commit!
Why Git?
Over 70% of developers use Git!
Developers can work together from anywhere in the world.
Developers can see the full history of the project.
Developers can revert to earlier versions of a project.
Note: Most Git actions (like staging, committing, and viewing
history) happen on your own computer.
Only Push and Pull interact with remote servers like GitHub,
GitLab, or Bitbucket to upload or download changes.
How to Install Git
You can download Git for free from git-scm.com.
Windows: Download and run the installer.
Click “Next” to accept the recommended settings.
This will install Git and Git Bash.
macOS: If you use Homebrew, open Terminal and
type brew install git.
Or, download the .dmg file and drag Git to your
Applications folder.
Linux: Open your terminal and use your package
manager.
For example, on Ubuntu: sudo apt-get install git
After installation, you will be able to use Git from your terminal
or command prompt.
Tip for Beginners: Installing Git is safe and you can always
uninstall it later if you want.
Git Bash
Git Bash is a terminal for Windows that lets you use Git
commands.
Look at our Bash Tutorial to learn more about Bash.
After installing Git, you can find Git Bash in your Start menu.
You can use Git Bash just like the Command Prompt, but with
extra Unix commands (like ls and pwd).
Example: Open Git Bash
Click Start, type "Git Bash", and open the app.
Example: First Command in Git Bash
ls
Desktop Documents Downloads Pictures
This command lists the files in your current folder.
Verifying Your Installation
After installing, check that Git works by opening your terminal
(or Git Bash on Windows) and running:
Example: Check Git Vers
ion
git --version
git version 2.43.0.windows.1
If Git is installed, you will see something like git version
X.Y.Z
If you see an error, try closing and reopening your terminal, or
check that Git is in your PATH.
Default Editor
During installation, Git asks you to pick a default text editor.
This is the program that will open when you need to write
messages (like for commits).
Example: Set VS Code as Default Editor
git config --global core.editor "code --wait"
If you're not sure, just pick the default (Notepad on Windows).
You can always change this later.
Example: Set Notepad as Default Editor
git config --global core.editor "notepad"
PATH Environment
Choosing to add Git to your PATH means you can use Git
commands in any terminal window.
This is highly recommended for most users to do this during
installation.
If you skip this, you'll only be able to use Git in Git Bash (on
Windows) or Terminal (on macOS and Linux).
Example: Check if Git is in PATH
git --version
git version 2.43.0.windows.1
If you see an error, you need to add Git to your PATH.
How to Add Git to PATH after Installation
Windows:
1. If you missed the option during installation, search
for "Environment Variables" in the Start menu and
open it.
2. Click "Environment Variables..." and find the "Path"
variable under "System variables".
3. Click "Edit", then "New", and add the path to your
Git bin and cmd folders
(e.g., C:\Program Files\Git\bin and C:\Program
Files\Git\cmd).
4. Click OK to save. Restart your terminal.
macOS:
1. If you installed with Homebrew, your PATH is usually
set automatically.
2. If not, open Terminal and add this line to
your ~/.zshrc or ~/.bash_profile:
3.export PATH="/usr/local/bin:$PATH"
4. Save the file and
run source ~/.zshrc or source ~/.bash_profile.
Linux:
1. Most package managers add Git to PATH
automatically.
2. If not, add this line to your ~/.bashrc or ~/.profile:
3.export PATH="/usr/bin:$PATH"
4. Save the file and
run source ~/.bashrc or source ~/.profile.
After adding Git to your PATH, open a new terminal window and
run git --version to check that it works everywhere.
Line Endings
Git can convert line endings in text files.
On Windows, it's usually best to select "Checkout Windows-
style, commit Unix-style line endings".
This helps prevent problems when you share code with people
using different operating systems.
Updating or Uninstalling Git
Update: Download and run the latest installer, or use
your package manager
(e.g., brew upgrade git or sudo apt-get upgrade git).
It's a good idea to keep Git up to date for the latest
features and security fixes.
Uninstall: Use "Add or Remove Programs" on Windows,
or your package manager on Mac/Linux.
Configuration of GIT
Configure Git
Now let Git know who you are.
This is important for version control systems, as each Git
commit uses this information:
User Name
Your name will be attached to your commits. Set it with:
Example
git config --global user.name "Your Name"
Email Address
Your email is also attached to your commits. Set it with:
Example
git config --global user.email "you@example.com"
Change the user name and email to your own.
Use --global to set the value for every repository on your
computer.
Use --local (the default) to set it only for the current
repository.
Viewing Your Configuration
You can see all your Git settings with:
Example: List All Settings
git config --list
user.name=Your Name
user.email=you@example.com
core.editor=code --wait
alias.st=status
init.defaultbranch=main
...
To view a specific value, use:
Example: View a Specific Setting
git config user.name
Your Name
Changing or Unsetting Config Values
To change a value, just run the git config command again
with the new value.
To remove a setting, use --unset:
Example: Unset an Alias
git config --global --unset code.editor
Default Branch Name
Set the default branch name for new repositories (for
example, main instead of master):
Example: Set Default Branch Name
git config --global init.defaultBranch main
Configuration Levels
There are three levels of configuration:
System (all users): git config --system
Global (current user): git config --global
Local (current repo): git config --local
The order of precedence is:
Local (current repo)
Global (current user)
System (all users)
The reason to use the different levels is that you can set
different values for different users or repositories.
This can be used for example to set different default branches
for different repositories and users.
Example: Set a Local Config
Local settings only apply to the current repository.
git config user.name "Project Name"
Example: Set a Global Config
Global settings apply to all repositories for the current user.
git config --global user.name "Global Name"
Example: Set a System Config
System settings apply to all repositories for all users.
git config --system user.name "System Name"
Creating Git Folder
Start by creating a new folder for our project:
Example
mkdir myproject
cd myproject
mkdir creates a new directory.
cd changes our working directory.
Now we are in the correct directory and can initialize Git!
Note: Open Git Bash Here (Windows)
If you're using Windows, you can open Git Bash directly in your
project folder:
Right-click the folder in File Explorer
Select Git Bash Here
This opens a terminal window in the correct location.
Initialize Git
Now that we are in the correct folder, we can initialize Git on
that folder:
Example
git init
Initialized empty Git repository in
/Users/user/myproject/.git/
What is a Repository?
A Git repository is a folder that Git tracks for changes.
The repository stores all your project's history and
versions.
What Happens When You Run git init?
Git creates a hidden folder called .git inside your project.
This is where Git stores all the information it needs to track
your files and history.
Example: Show Hidden .git Folder (Linux/macOS)
ls -a
. .. .git
What is a New File?
A new file is a file that you have created or copied into your
project folder, but haven't told Git to watch.
Here are the key things to know:
Create a new file (with a text editor)
ls - List files in the folder
git status - Check which files are tracked
Understand untracked and tracked files
Create a New File
Your new Git repository is empty.
Let's add a file using your favorite text editor, and save it in
your project folder.
If you need help creating a file, see our HTML Editors page.
For this example, we'll use a simple HTML file:
Example: Simple HTML File
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
<p>This is the first file in my new Git Repo.</p>
</body>
</html>
Save this as index.html in your project folder.
ADVERTISEMENT
List Files in the Directory
To see which files are in your project folder, use
the ls command:
Example
ls
index.html
ls lists all files in the current folder.
You should see index.html in the output.
Check File Status with git status
Now check if Git is tracking your new file:
Example
git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be
committed)
index.html
nothing added to commit but untracked files present
(use "git add" to track)
Git sees index.html, but it is untracked (not yet added to the
repository).
What is an Untracked File?
An untracked file is any file in your project folder that Git is
not yet tracking.
These are files you've created or copied into the folder, but
haven't told Git to watch.
What is a Tracked File?
A tracked file is a file that Git is watching for changes.
To make a file tracked, you need to add it to the staging area
Troubleshooting
File not showing up with ls: Make sure you saved it in
the correct folder.
Use pwd to check your current directory.
File not listed in git status: Make sure you are in the
correct folder and that you saved the file.
What is the Staging Environment?
The staging environment (or staging area) is like a waiting
room for your changes.
You use it to tell Git exactly which files you want to include in
your next commit.
This gives you control over what goes into your project history.
Here are some key commands for staging:
git add <file> - Stage a file
git add --all or git add -A - Stage all changes
git status - See what is staged
git restore --staged <file> - Unstage a file
Stage a File with git add
To add a file to the staging area, use git add <file>:
Example
git add index.html
Now index.html is staged. You can check what is staged
with git status:
Example
git status
On branch master
No commits yet
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: index.html
Stage Multiple Files (git add --all, git add -A)
You can stage all changes (new, modified, and deleted files) at
once:
Example
git add --all
git add -A does the same thing as git add --all.
Check Staged Files with git status
See which files are staged and ready to commit:
Example
git status
On branch master
No commits yet
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: README.md
new file: bluestyle.css
new file: index.html
How to Unstage a File
If you staged a file by mistake, you can remove it from the
staging area (unstage it) with:
Example
git restore --staged index.html
Now index.html is no longer staged. You can also
use git reset HEAD index.html for the same effect.
Troubleshooting
Staged the wrong file? Use git restore --
staged <file> to unstage it.
Forgot to stage a file? Just run git add <file> again
before you commit.
Not sure what's staged? Run git status to see what
will be committed.