Git Basics

Section 1: Git Basics

Creating a New Git Repository

Initializing a New Git Repository

To start version controlling a new project, navigate to the project's root directory and run:

git init

This command initializes a new Git repository, creating a hidden .git directory to store version control information.

Understanding the Purpose of the .git Directory

The .git directory contains all the metadata and configuration files needed for version control. It's essential not to modify or delete this directory manually, as it houses critical information about your Git repository.

Git Workflow

Basic Git Commands

Staging Changes with git add

Before committing changes, you need to stage them using the git add command. To stage a specific file:

git add <filename>

To stage all changes:

git add .

Committing Changes with git commit

Once changes are staged, commit them using the git commit command:

git commit -m "Your commit message"

This creates a new commit with the changes and a descriptive commit message.

Reviewing Changes with git diff

To review the changes made to your files before committing, use the git diff command:

git diff

This shows the differences between your working directory and the last committed state.


Branching in Git

Creating Branches with git branch

Branching is a powerful Git feature that allows you to work on different features or fixes simultaneously. To create a new branch:

git branch <branch-name>

This creates a new branch but doesn't switch to it immediately.

Switching Between Branches with git checkout

To switch to the branch you created or an existing branch:

git checkout <branch-name>

This command helps you navigate between different branches in your Git repository.

This covers the basics of starting a Git repository, understanding the .git directory, and using essential Git commands for staging, committing, and branching. As you progress, you'll explore more advanced Git concepts and workflows.