Skip to content

Git does not store diffs; it stores snapshots. Every commit is a full tree of your project at that moment. Branches are not distinct copies of files, they are just lightweight, movable pointers to a specific commit. When you merge, Git is simply combining the histories of two pointers. Understanding this makes “detached HEAD” and rebasing make sense.

TaskCommand
Initializegit init
Clone repogit clone <url>
Add remotegit remote add origin <url>

Before you start tracking files, initialize a repository or clone an existing one. Cloning automatically sets up a remote called origin.

Terminal window
git init
git add .
git commit -m "Initial commit"

Gotcha: Cloning into an existing non-empty directory is not allowed by default.

TaskCommand
Stage filegit add <file>
Stage allgit add .
Commitgit commit -m "msg"

Git has a two-step commit process. You first move changes from your working directory to the staging area (index), and then commit the staged changes to the repository history.

Terminal window
git add index.js
git commit -m "Update index"

Tip: Use git commit -a -m "msg" to stage and commit all modified (but not new) files in one step.

TaskCommand
List branchesgit branch
Create & switchgit checkout -b <name>
Switch branchgit switch <name>

Branches allow you to work on features isolated from the main codebase. Creating a branch is nearly instantaneous because it just creates a new pointer.

Terminal window
git checkout -b feature/auth
git switch main

Note: git switch is the newer, preferred command for changing branches over git checkout.

TaskCommand
Merge branchgit merge <branch>
Abort mergegit merge --abort

To combine changes from one branch into another, switch to the destination branch and merge the source branch. Git will try to automatically resolve changes, but you may need to manually fix conflicts.

Terminal window
git switch main
git merge feature/auth

Warning: Always ensure your working directory is clean before starting a merge to prevent losing uncommitted work.

TaskCommand
Unstage filegit restore --staged <f>
Discard changesgit restore <file>
Amend commitgit commit --amend

Git provides several ways to undo mistakes depending on where they occurred. You can easily discard uncommitted changes or add forgotten files to the very last commit.

Terminal window
git add forgotten_file.txt
git commit --amend --no-edit

Gotcha: Never amend commits that have already been pushed to a shared remote branch.

TaskCommand
Fetch changesgit fetch
Pull & mergegit pull
Push changesgit push origin <b-name>

To share your work or get updates from others, you interact with remote repositories. fetch gets the changes without merging, while pull fetches and merges in one step.

Terminal window
git push -u origin feature/auth

Tip: The -u flag sets the upstream tracking, so future pushes only require git push.

TaskCommand
View loggit log
One-line loggit log --oneline
View changesgit status

You can inspect the history of commits and the current state of your working directory. The log shows commit hashes, authors, dates, and messages.

Terminal window
git log --oneline -n 5
  • Pro Git Book — The definitive, free guide to Git internals and advanced usage.
Verified 2026-07-25 against Git Documentation