Interactive Git Tutorial

Creating and Switching to a Branch

Branches in Git allow you to develop features isolated from each other. The main branch is your default branch.

Lesson Context

When you create a new branch, Git creates a new pointer to the same commit you're currently on. The Git graph will show two branches pointing to the same commit. When you switch branches, the HEAD pointer moves to the tip of the new branch.

Task: Create a new branch called "feature" from "main" and switch to it.

Git Graph

Initial commitHEAD

Blue: main branch

Green: feature branch

HEAD: Current working branch

Git Terminal

$

Git Cheat Sheet

Basic Commands

  • git init - Initialize a new Git repository
  • git clone [url] - Clone a repository
  • git add [file] - Add a file to staging area
  • git commit -m "[message]" - Commit changes
  • git status - Check repository status
  • git log - View commit history

Branching and Merging

  • git branch - List branches
  • git branch [branch-name] - Create a new branch
  • git checkout [branch-name] - Switch to a branch
  • git merge [branch] - Merge a branch into the active branch
  • git branch -d [branch-name] - Delete a branch

Remote Repositories

  • git remote add [name] [url] - Add a remote repository
  • git push [remote] [branch] - Push to remote repository
  • git pull [remote] [branch] - Pull from remote repository
  • git fetch [remote] - Fetch changes from remote

Undoing Changes

  • git revert [commit] - Revert a commit
  • git reset [file] - Unstage a file
  • git checkout -- [file] - Discard changes to a file