Full Guide to Git with Tutorial

Introduction

Git is a distributed version control system used for tracking changes in source code during software development. It allows multiple developers to collaborate efficiently while keeping a history of modifications.

Installation

Windows

  1. Download Git from git-scm.com.

  2. Run the installer and follow the default settings.

  3. Verify installation by running:

    git --version

macOS

  1. Install Git using Homebrew:

    brew install git
  2. Verify installation:

    git --version

Linux

  1. Install Git using package manager:

    sudo apt install git  # Debian/Ubuntu sudo yum install git  # CentOS/RHEL sudo pacman -S git    # Arch Linux
  2. Verify installation:

    git --version

Git Configuration

  1. Set your user details:

    git config --global user.name "Your Name" git config --global user.email "you@example.com"
  2. Check configuration:

    git config --list

Basic Git Commands

Initializing a Repository

git init

This command initializes a new Git repository in the current directory.

Cloning a Repository

git clone <repository_url>

Clones an existing repository to your local machine.

Checking the Status

git status

Shows the current state of the working directory and staging area.

Adding Files to Staging

git add <filename>

Adds a specific file to the staging area.

git add .

Adds all changes to the staging area.

Committing Changes

git commit -m "Commit message"

Records changes to the repository with a descriptive message.

Pushing Changes to Remote Repository

git push origin <branch_name>

Uploads local commits to a remote repository.

Pulling Changes from Remote Repository

git pull origin <branch_name>

Fetches and merges updates from a remote repository.

Working with Branches

Creating a New Branch

git branch <new_branch_name>

Creates a new branch.

Switching Branches

git checkout <branch_name>

Switches to the specified branch.

git switch <branch_name>

Newer alternative to git checkout.

Merging Branches

git merge <branch_name>

Merges the specified branch into the current branch.

Deleting a Branch

git branch -d <branch_name>

Deletes a local branch.

Undoing Changes

Undo Last Commit (Keep Changes)

git reset --soft HEAD~1

Moves the last commit back to the staging area.

Undo Last Commit (Discard Changes)

git reset --hard HEAD~1

Deletes the last commit permanently.

Reverting a Commit

git revert <commit_id>

Creates a new commit that undoes the specified commit.

Working with Remote Repositories

Adding a Remote Repository

git remote add origin <repository_url>

Links the local repository to a remote repository.

Viewing Remote Repositories

git remote -v

Displays the remote repositories linked to your local repository.

Fetching Changes

git fetch origin

Downloads updates from a remote repository but does not merge them.

Git Stash

Saving Work in Progress

git stash

Temporarily saves changes without committing them.

Viewing Stashed Changes

git stash list

Lists all stashed changes.

Applying Stashed Changes

git stash apply

Applies the most recent stash.

Deleting Stashed Changes

git stash drop

Removes the most recent stash.

Git Logs and History

Viewing Commit History

git log

Shows a history of commits.

Viewing a Compact Log

git log --oneline --graph --all

Displays a simplified commit history.

Advanced Git Commands

Rebasing

git rebase <branch_name>

Moves your branch to the latest commit of another branch.

Squashing Commits

git rebase -i HEAD~3

Allows you to combine multiple commits into one.

Cherry-Picking a Commit

git cherry-pick <commit_id>

Applies a specific commit from one branch to another.

Git Hooks

Git hooks are scripts that run at different stages of the Git workflow.

Example of a pre-commit hook (.git/hooks/pre-commit):

#!/bin/sh echo "Running pre-commit hook"

Make the script executable:

chmod +x .git/hooks/pre-commit

Conclusion

This guide covers the essential and advanced Git commands. Git is a powerful tool that enables efficient collaboration, version tracking, and code management. The more you practice, the better you'll become at using Git effectively.