Master Git: Your Complete Guide to Learning Version Control

Master Git: Your Complete Guide to Learning Version Control

Sundaram Kumar JhaSundaram Kumar Jha

Introduction

Git is a distributed version control system created by Linus Torvalds in 2005. It’s designed to handle everything from small to large projects with speed and efficiency. Whether you are a software developer or a team of developers working on collaborative projects, Git provides a systematic approach to tracking changes, managing versions, and collaborating efficiently.

In this comprehensive course, we’ll walk you through all the essentials and advanced features of Git. From setting up your repository to advanced branching strategies, you'll gain a deep understanding of Git's architecture and usage.


1: What is Git and Why Use It?

1.1 Git vs. Other Version Control Systems

Git stands out from other VCSs like Subversion (SVN), Mercurial, and CVS because it’s distributed, meaning every developer has the full project history on their local machine. This makes it faster and allows for offline work.

Key Advantages:

  • Speed: Git performs most operations locally, which is faster than server-based systems.

  • Branching and Merging: Branches are lightweight in Git and can be created or merged quickly.

  • Distributed Development: Every developer has a full copy of the repository, making it decentralized.

  • Integrity: Every file and commit in Git is checksummed with a hash (SHA-1), ensuring file integrity.

1.2 Installing Git

To start, install Git on your system:

  • For Windows: Download from git-scm.com and follow the installer.

  • For MacOS: Use brew install git (if you have Homebrew installed).

  • For Linux: Use sudo apt install git (for Debian/Ubuntu) or sudo dnf install git (for Fedora).

Verify the installation using:

git --version

2: Git Basics

2.1 Initializing a Repository

To create a new Git repository, use the git init command:

git init my-project

This will initialize a new Git repository in the folder my-project.

2.2 Adding Files to Git

Once you've made some changes, add them to the staging area:

git add <filename> 
git add .   # Adds all changes

2.3 Committing Changes

After staging files, you can commit the changes with:

git commit -m "Add feature A"

Commits in Git are snapshots of the project, not differences, which makes tracking the full state of your project at any point easy.

2.4 Checking the Commit History

You can see all the commits with:

git log

3: Git Architecture

3.1 Objects in Git

Git operates with four main objects:

  • Blob: Contains file data.

  • Tree: Represents directories and their contents.

  • Commit: Points to a tree object and contains metadata.

  • Tag: Points to a commit, representing a fixed point in history.

3.2 SHA-1 Hashing

Git uses SHA-1 to create unique identifiers for each object. This ensures file integrity, as even a minor change in content will produce a completely different hash.

3.3 The Three States in Git

Files in Git can reside in one of three states:

  1. Modified: The file has been changed.

  2. Staged: The file is marked to be included in the next commit.

  3. Committed: The file is stored in the local database.


4: Branching and Merging

4.1 What is a Branch?

A branch in Git represents an independent line of development. By default, Git creates a master or main branch:

git branch new-feature

Switch to the branch with:

git checkout new-feature

4.2 Merging Branches

Once your feature is complete, you can merge it back into the main branch:

git checkout main
git merge new-feature

If there are conflicting changes, Git will ask you to resolve the conflicts manually.


5: Remote Repositories

5.1 Adding a Remote Repository

A remote repository is stored on a server, which allows team collaboration. To add a remote:

git remote add origin https://github.com/username/repo.git

5.2 Pushing and Pulling Changes

Push your local commits to the remote server:

git push origin main

Fetch the latest changes from the remote:

git pull origin main

6: Working with Tags

Tags are a way to mark specific points in history, such as release versions.

6.1 Creating Tags

To create a tag:

git tag v1.0.0

6.2 Annotated Tags

Annotated tags contain metadata such as a message:

git tag -a v1.0.0 -m "Version 1.0.0"

Push tags to the remote repository:

git push origin v1.0.0

7: Git Stash

The git stash command allows you to save your changes without committing them, then apply them later.

To stash your changes:

git stash

To apply the stash:

git stash apply

8: Git Rebase vs Merge

8.1 Merge

Merging combines the history of two branches. It creates a new commit that combines the changes:

git merge branch-name

8.2 Rebase

Rebasing rewrites history by placing your changes on top of the changes from another branch:

git rebase main

9: Advanced Git Techniques

9.1 Git Bisect

Use git bisect to find a commit that introduced a bug:

git bisect start
git bisect bad
git bisect good <commit-id>

9.2 Cherry-Picking

Cherry-pick allows you to apply a specific commit to another branch:

git cherry-pick <commit-id>

Conclusion

Git is a powerful tool that streamlines version control, making it easier for developers to track changes, collaborate, and manage projects. Whether you're working alone or with a large team, mastering Git will significantly improve your workflow.