Git Tutorial Part 1: Introduction to Git and Basic Setup

Spread the love

What is Git?

Git is a distributed version control system (VCS) that helps developers track changes to their code over time. It enables multiple people to work on projects, keeping track of every change and making it simple to revert to previous versions if necessary. Git is essential for efficient code management, whether you are working alone or with a team.

Why Use Git?

  • Version Tracking : Keeps track of all changes to your files.
  • Collaboration : Multiple developers can work on the same project without overwriting one another’s work.
  • Backup : Because Git is distributed, each contributor has a full copy of the repository, which serves as a backup.
  • Branching and Merging : Easily test new features or fixes in branches without affecting the main project. Once completed, changes can be merged back in.

Basic Concepts

Before we dive into Git, let us first understand some fundamental concepts:

  • Repository (Repo) :  A set of files and their history. Can be local (on your computer) or remote (via GitHub, GitLab, etc.).
  • Commit : A snapshot of your files from a specific point in time.
  • Branch : A repository version that exists in parallel. You can create branches to work on new features and then merge them back together when they are complete.
  • Clone : Copies a remote repository to your local machine.
  • Pull/Push : Pull moves changes from the remote repository to your local machine. Push propagates your local commits to the remote repository.
  • Merge : Combines changes from multiple branches or commits.

Setting up Git

Let’s start by installing and configuring Git.

  • Windows : Download and install Git from [Git for Windows](https://git-scm.com/download/win). During installation, choose options according to your preference, but default settings are usually fine.

  • macOS : You can install Git using Homebrew with the following command:
Bash
brew install git

  • Linux : Install Git using the package manager. For Ubuntu:
Bash
sudo apt update
sudo apt install git

Verify the installation by typing the following command in your terminal:

Bash
git --version

You should see the installed version of Git.

Configuring Git

Before using Git, set up your username and email, which Git uses to associate commits with your identity.

Run these commands in your terminal, replacing <Your Name> and <your-email@example.com> with your actual details:

Bash
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

You can check your configuration with:

Bash
git config --list

Initializing a Git Repository

Now, let’s create a local Git repository:

  • Create or navigate to the folder you want to track with Git.
  • Run the following command:
Bash
git init

This initializes an empty Git repository in that folder.

Making Your First Commit

Once the repository is initialized, let’s make the first commit:

  • Create a new file (e.g., `README.md`) and add some text.
  • Run the below command
Bash
git add README.md

This stages the file, telling Git to include it in the next commit.

  • Commit the file:
Bash
git commit -m "Initial commit"

This creates a commit with a message describing the changes.

Conclusion

In this first section, we covered the fundamentals of Git, such as installing and configuring it, setting up a repository, and making your first commit. In the following section, we will go over branching, merging, and working with remote repositories.

Part 2: Branching and Merging with Git

See also  Git Tutorial Part 2: Branching and Merging with Git

1 thought on “Git Tutorial Part 1: Introduction to Git and Basic Setup”

Leave a Comment

PHP Code Snippets Powered By : XYZScripts.com