Menu

2. Git & GitHub

Next.js Master Roadmap - IT Technology

This chapter covers the essentials of Git and GitHub, including installation, repository creation, commit workflow, branching, merging, conflict resolution, remote repositories, pull requests, collaboration workflows, and GitHub Actions for CI/CD.

No MCQ questions available for this chapter.

2. Git & GitHub

Git Basics

Installation

Git is a distributed version‑control system that must be installed on your development machine before you can create or clone repositories. The installation method varies by operating system.

  • macOS: Use Homebrew: brew install git
  • Windows: Download the official installer from git-scm.com and run it.
  • Linux (Ubuntu/Debian): sudo apt-get install git
  • Linux (CentOS/RHEL): sudo yum install git

After installation, verify the version:

git --version

It is good practice to set your user identity so that every commit records who made it:

git config --global user.name "John Doe"
git config --global user.email "john@example.com"

Note: The --global flag writes these settings to your ~/.gitconfig file, affecting all repositories on the machine.

Repository Creation

There are two primary ways to obtain a Git repository:

  1. Initialize a new repository: Run git init inside a project folder. This creates a hidden .git directory that stores all metadata.
  2. Clone an existing repository: Use git clone <url> to copy a remote repo (including its full history) to your machine.

Example: Cloning the Next.js source:

git clone https://github.com/vercel/next.js.git

For server‑side sharing, you can create a bare repository (no working directory):

git init --bare

Bare repos are typically used as central remotes (e.g., on GitHub) because they only store the Git data.

Commit Workflow

Git follows a three‑stage architecture:

  • Working Directory: Files you edit.
  • Staging Area (Index): Snapshots of changes marked for the next commit.
  • Repository: Committed snapshots.

The basic commands are:

  • git add <file> – stages a file (or git add . to stage all changes).
  • git status – shows the state of the working directory and staging area.
  • git commit -m "message" – creates a commit from the staged snapshot.

Example:

git add .
git commit -m "feat: add user authentication"

Commit messages often follow a convention like <type>: <description> (e.g., feat:, fix:, docs:).

To modify the most recent commit:

git commit --amend -m "new message"

This replaces the tip of the current branch with a new commit that has the same parent(s).

Branching

Branches are lightweight pointers to commits, enabling parallel lines of development.

  • Create a branch: git branch <name>
  • Switch to a branch: git checkout <name> or the newer git switch <name>
  • Create and switch in one step: git checkout -b <name> or git switch -c <name>
  • List branches: git branch -a (shows local and remote)

Example: Starting a feature branch:

git checkout -b feature/payment-integration

By convention, the main line of development is named main (formerly master).

Merging

Merging integrates changes from one branch into another.

  • Fast‑forward merge: Occurs when the target branch has not diverged; Git simply moves the pointer forward.
  • Three‑way merge: When histories diverge, Git creates a merge commit that has two parents.

Command:

git merge <branch>

Example: Merging a feature into main:

git checkout main
git merge feature/payment-integration

To force a merge commit even when a fast‑forward is possible, use --no-ff:

git merge --no-ff feature/branch

This preserves the explicit branch topology in the history.

Resolving Conflicts

Conflicts arise when the same lines are modified in both branches being merged. Git marks the conflicting sections:

<<<<<<< HEAD
// your changes