Introduction
to Version Control and Git for absolute beginners
1.
Introduction to Version Control
What is Version Control?
Version control is a system that helps you manage changes to
your files, especially code, over time. Imagine you're working on a programming
project, and you want to keep track of the changes you make so you can go back
to a previous version if something goes wrong. Version control systems (VCS) do
just that! They record changes to a file or set of files, allowing you to
revert to specific versions, compare different versions, and collaborate with
others.
Benefits of Version Control
- Collaboration: Version control systems allow
multiple people to work on a project simultaneously. You can track who
made changes, when, and why. This is essential in team projects.
- History Tracking: Every change you make is recorded
with a timestamp and a message explaining what was done. You can look back
at the history of your project and see how it evolved.
- Rollback: If a mistake is made, you can easily
revert to a previous version of the project without losing any work.
- Branching: You can create branches to work on
different features or versions of your project without affecting the main
codebase.
2.
Introduction to Git
What is Git?
Git is a distributed version control system. This means that
every developer working on a project has a complete copy of the project's
history on their computer. Unlike centralized systems where all the history is
stored on a single server, Git allows for more flexibility and reliability. You
can work offline, and your changes can be shared with others when you're ready.
Basic Git Terminology
- Repository (Repo): A repository is like a storage
box where all your project files and the complete history of changes are
kept. It can be local (on your computer) or remote (on a platform like
GitHub).
- Commit: A commit is a snapshot of your project at a
particular point in time. It includes all the changes made since the last
commit and is accompanied by a message explaining what was changed.
- Branch: A branch is a separate line of development
in your project. It allows you to work on different features or versions
of your project independently. The main branch is often called main or master.
- Merge: Merging is the process of combining changes
from one branch into another. For example, once a new feature is complete,
you might merge the feature branch into the main branch.
- Clone: Cloning means making a copy of a repository
from a remote server to your local machine. This allows you to work on the
project locally.
- Pull: Pulling means downloading the latest changes
from a remote repository to your local repository.
- Push: Pushing means uploading your changes from
your local repository to a remote repository, making them available to
others.
3.
Setting Up Git
Installing Git
To start using Git, you need to install it on your computer.
Here's a quick guide for different operating systems:
- Windows:
- Download the Git installer from git-scm.com.
- Run the installer and follow the prompts. You can use the
default settings.
- Once installed, open the Git Bash terminal to start using
Git.
- macOS:
- Open the Terminal application.
- Type the following command and press Enter:
git --version
- If Git is not installed, you'll be prompted to install
it. Follow the instructions.
- Linux:
- Open the terminal.
- Install Git by running the following command:
sudo apt-get install git
- Once installed, you can verify by typing:
git --version
Configuring Git
Before you start using Git, you need to set up your
identity. This is important because every Git commit uses this information.
- Open your terminal (or Git Bash on Windows).
- Set your username with the following command:
git config --global user.name "Your Name"
- Set your email address with this command:
git config --global user.email "your.email@example.com"
Creating a Repository
To start a new project with Git:
- Navigate to your project directory in the terminal:
cd path/to/your/project
- Initialize a new Git repository:
git init
This creates a hidden .git folder in your project directory.
Now, your project is under version control!
4. Basic
Git Commands
Now that your project is set up, let's go over some basic
Git commands.
1. git init
- Purpose: Initializes a new Git repository in your
project directory.
- Usage: git init
2. git add
- Purpose: Adds changes to the staging area. The
staging area is where you prepare your changes before committing them.
- Usage: git add filename
To add all changes:
git add .
3. git commit
- Purpose: Commits the changes in the staging area to
the repository with a message.
- Usage:
git commit -m "Your commit message"
4. git status
- Purpose: Shows the status of changes in your
working directory. It tells you which files are modified, staged, or
untracked.
- Usage:
git status
5. git log
- Purpose: Displays the commit history for the
repository. This is useful for reviewing past commits.
- Usage:
git log
6. git branch
- Purpose: Lists all branches in your repository or
creates a new branch.
- Usage:
git branch
git branch branch-name
7. git merge
- Purpose: Merges the changes from one branch into
another.
- Usage:
git merge branch-name
8. git checkout
- Purpose: Switches to a different branch.
- Usage:
git checkout branch-name
With these basics, you're ready to start using Git to manage
your projects and collaborate with others! Remember, practice makes perfect, so
try these commands out on a small project to get comfortable with version
control.