Master Git Version Control

Learn the fundamentals of Git to track changes, collaborate with others, and manage your projects efficiently.

bash

$ git init

Initialized empty Git repository in /project/.git/


$ git add .


$ git commit -m "Initial commit"

[main (root-commit) abc1234] Initial commit

3 files changed, 25 insertions(+)


$ git push origin main

Enumerating objects: 5, done.

Counting objects: 100% (5/5), done.

Git Basics

Git is a distributed version control system that helps you track changes in your code and collaborate with others.

Version Control

Git keeps track of all changes made to your code, allowing you to revert to previous versions if needed.

Collaboration

Multiple developers can work on the same project simultaneously without overwriting each other's changes.

History

Every change is recorded with who made it and when, creating a complete project history.

Key Concepts

Repository

A repository (or repo) is a collection of files and folders that Git tracks. It contains the complete history of all changes.

Commit

A commit is a snapshot of your repository at a specific point in time. Each commit has a unique ID.

Branch

A branch is an independent line of development. The default branch is usually called "main" or "master".

Remote

A remote is a version of your repository hosted on the internet (like GitHub) that team members can access.

Git Workflow

Understanding the basic Git workflow will help you use it effectively in your projects.

Working Directory

Your local files

Staging Area

Prepared changes

Repository

Committed changes

1. Modify Files

Edit files in your working directory. Git recognizes changed files as "modified".

2. Stage Changes

Add modified files to the staging area to prepare them for committing.

3. Commit Changes

Permanently store the staged changes in your local repository with a descriptive message.

bash

$ git status

On branch main

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git restore <file>..." to discard changes in working directory)

modified: index.html

no changes added to commit (use "git add" and/or "git commit -a")


$ git add index.html


$ git commit -m "Update homepage layout"

[main 1a2b3c4] Update homepage layout

1 file changed, 5 insertions(+)

Essential Git Commands

These are the most commonly used Git commands you'll need to know.

Repository Setup

git init

Initialize a new Git repository in the current directory

git clone <repository-url>

Create a local copy of a remote repository

git remote add origin <repository-url>

Connect your local repository to a remote

Basic Workflow

git add <file>

Stage a specific file for commit

git add .

Stage all changed files for commit

git commit -m "commit message"

Commit staged changes with a message

git push origin <branch-name>

Push commits to the remote repository

Viewing Information

git status

Show the working tree status

git log

Show commit logs

git diff

Show changes between commits/staging

git show <commit-id>

Show details about a specific commit

Undoing Changes

git restore <file>

Discard changes in working directory

git reset <file>

Unstage a file while keeping changes

git reset --hard <commit-id>

Reset repository to a specific commit (caution!)

git revert <commit-id>

Create a new commit that undoes a previous commit

Working with Branches

Branches allow you to work on different features or fixes simultaneously without affecting the main codebase.

Branch Commands

git branch

List all local branches

git branch <branch-name>

Create a new branch

git checkout <branch-name>

Switch to a branch

git checkout -b <branch-name>

Create and switch to a new branch

git merge <branch-name>

Merge a branch into the current branch

git branch -d <branch-name>

Delete a branch

Branching Strategies

Feature Branch Workflow

Create a new branch for each feature or bugfix. Merge back to main when complete.

Git Flow

A more structured approach with main, develop, feature, release, and hotfix branches.

GitHub Flow

Simpler than Git Flow, with just main and feature branches plus pull requests.

bash

$ git checkout -b feature/new-header

Switched to a new branch 'feature/new-header'


# Make changes to files...


$ git add .

$ git commit -m "Add new header design"

[feature/new-header 1a2b3c4] Add new header design

2 files changed, 15 insertions(+)


$ git checkout main

Switched to branch 'main'

$ git merge feature/new-header

Updating d4e5f6a..1a2b3c4

Fast-forward

Merge Conflicts

Conflicts occur when Git can't automatically merge changes. You'll need to manually resolve them.

CONFLICT (content): Merge conflict in index.html

Automatic merge failed; fix conflicts and then commit the result.


# Open the file and look for conflict markers:

<<<<<<< HEAD

Current branch content

=======

Incoming branch content

>>>>>>> feature/new-header


# After resolving conflicts:

$ git add index.html

$ git commit

Collaboration with Git

Git enables seamless collaboration between team members working on the same project.

Remote Repository

git remote -v

List remote repositories

git fetch

Download changes from remote without merging

git pull

Fetch and merge changes from remote

git push

Upload local changes to remote

Pull Requests (PRs)

On platforms like GitHub, PRs let you propose changes and discuss them before merging into main.

Create a branch → Push changes → Open PR → Get reviews → Merge after approval.

Team Workflow

1. Always Pull Before Starting

Always pull the latest changes before starting work to avoid conflicts.

2. Work on Feature Branches

Never commit directly to main. Create a branch for each task.

3. Keep Branches Focused

Each branch should address a single feature or bugfix.

4. Commit Often

Small, frequent commits are easier to review and revert if needed.

bash

$ git pull origin main

Already up to date.


$ git checkout -b fix/login-bug

Switched to a new branch 'fix/login-bug'


# Make changes and commit...

$ git add .

$ git commit -m "Fix login validation bug"


$ git push origin fix/login-bug

Enumerating objects: 7, done.

Counting objects: 100% (7/7), done.

Delta compression using up to 8 threads

Compressing objects: 100% (4/4), done.

Writing objects: 100% (4/4), 489 bytes | 489.00 KiB/s, done.

Total 4 (delta 2), reused 0 (delta 0)

remote: Create a pull request for 'fix/login-bug' on GitHub by visiting:

remote: https://github.com/user/repo/pull/new/fix/login-bug

Best Practices

Meaningful Commit Messages

Write clear, concise messages that explain what changed and why.

Atomic Commits

Each commit should represent a single logical change.

Regular Pulls

Frequently pull changes from main to stay up-to-date.

Branch Naming

Use consistent naming like feature/xxx, fix/xxx, or bugfix/xxx.

Code Reviews

Always review code before merging to maintain quality.

.gitignore

Exclude unnecessary files (node_modules, IDE files, etc.).