Welcome to Module 1: Basics of Git!
This module will provide an introduction to Git, covering the concept of version control and the fundamental commands required to start a new project with Git.
Introduction to Version Control Systems (VCS)
Version Control Systems (VCS) are integral to the software development process. A VCS tracks changes to a file or set of files over time so that you can recall specific versions later. It allows you to:
- Revert files back to a previous state.
- Revert the entire project back to a previous state.
- Compare changes over time.
- See who last modified something that might be causing a problem.
- Who introduced an issue and when.
There are three types of VCS: Local, Centralized (CVCS), and Distributed (DVCS). Local VCSs have a single database on the hard disk that stores all the changes to files. CVCSs operate on a single server storing all the versioned files, and multiple clients check out files from this central place. DVCS, like Git, clients don’t just check out the latest snapshot of the files but fully mirror the repository, including the history.
Git vs. Other VCS
Git, as a distributed version control system, is fundamentally different from other VCS like SVN (a centralized VCS) or Mercurial (another DVCS). Here’s how:
- Git stores file changes as snapshots in a mini filesystem, while others store information as file-based changes.
- Almost all operations in Git are local, making it faster to get the history of the project.
- Git ensures data integrity. Everything in Git is checksummed before it is stored, and is then referred to by that checksum. This makes it impossible to change the contents of any file or directory without Git knowing about it.
Popular services that use Git include GitHub, GitLab, and Bitbucket.
Installing Git is straightforward. You can download Git from git-scm.com. Installation instructions vary by operating system:
Windows: Download the latest Git for Windows installer. When you’ve successfully started the installer, you should see the Git Setup wizard screen. Follow the Next and Finish prompts to complete the installation.
Mac: The easiest way is to install the Xcode Command Line Tools. On Mavericks (10.9) or above, you can do this by trying to run git from the Terminal. If you don’t have it installed already, it will prompt you to install it.
Linux: You can install Git via a terminal command depending on your distribution. For example, if you’re on a Debian-based distribution like Ubuntu, try apt-get:
sudo apt-get update sudo apt-get install git
Verify the installation by typing
git --version in your terminal/cmd. It should display the installed version of Git.
Setting up a Git Repository
To start using Git, you have to initialize a repository:
Initialize a new Git repository: Navigate to your project directory and use the command
git init. This creates a new subdirectory named
.gitthat contains all the necessary metadata for the new repository.
Clone an existing Git repository: Cloning is getting a copy of an existing Git repository. The command is
git clone [url].
Basic Git Commands
Here are some of the most basic and commonly used Git commands:
git add [filename]or
git add .: Adds files from the working directory to the staging area.
git commit -m "Commit message": Commits the staged snapshot to the project history.
git status: Lists the status of working files and directories.
git log: Shows a list of all previous commits.
Each command serves a specific purpose in the context of managing a project’s development. The learner should understand not only how to use these commands, but when to use them.
Exercises and Practice
- Exercise 1: Install Git on your system. Verify the installation by checking the version of Git.
- Exercise 2: Initialize a new Git repository in a new directory. Add a file to the repository and make a commit.
- Exercise 3: Clone an existing Git repository from GitHub. Check the log of commits in the repository.
Remember, practice is crucial when learning a new tool like Git. Make sure to spend time experimenting with the commands you’ve learned. This hands-on practice will solidify your understanding and increase your comfort level with Git.