Git Command Cheat Sheet

Getting Started

  • Configure Git (first-time setup):
  git config --global user.name "Your Name"
  git config --global user.email "you@example.com"

Sets your Git identity for commits.

  • Initialize a new repository:
  git init

Creates an empty Git repository in your current directory.

  • Clone an existing repository:
  git clone <repository-url>

Copies a remote repository to your local machine.

Basic Workflow

  • Check repository status:
  git status

Displays changes, staged files, and untracked files.

  • Stage changes:
  git add <file>
  git add .      # Stages all changes in the current directory

Adds file(s) to the staging area before committing.

  • Commit changes:
  git commit -m "Descriptive commit message"

Records the staged snapshot in the project history.

  • Push changes to a remote repository:
  git push origin <branch>

Uploads your commits to the remote repository branch.

  • Pull changes from a remote repository:
  git pull origin <branch>

Fetches and integrates changes from the remote branch into your current branch.

Branching and Merging

  • Create a new branch:
  git branch <branch-name>

Creates a branch without switching to it.

  • Switch to a branch:
  git checkout <branch-name>

Switches your working directory to the specified branch.

  • Create and switch in one command:
  git checkout -b <branch-name>

Creates a new branch and immediately switches to it.

  • Merge a branch into the current branch:
  git merge <branch-name>

Merges changes from the specified branch into your current branch.

  • Delete a branch:
  git branch -d <branch-name>

Deletes the specified branch if it has been fully merged.

Undoing Changes

  • Unstage a file:
  git reset <file>

Removes file(s) from the staging area but keeps the changes.

  • Revert changes in working directory:
  git checkout -- <file>

Discards local changes in the specified file.

  • Undo the last commit (keep changes in working directory):
  git reset --soft HEAD~1

Moves HEAD to the previous commit, preserving your changes.

  • Undo the last commit (discard changes):
  git reset --hard HEAD~1

Moves HEAD to the previous commit and discards changes.

Advanced Commands

  • View commit history:
  git log

Lists all commits in the current branch.

  • View a condensed commit history:
  git log --oneline --graph --decorate

Provides a compact, visual representation of the commit history.

  • Stash changes:
  git stash

Temporarily saves uncommitted changes to clean your working directory.

  • Apply stashed changes:
  git stash pop

Restores the most recent stash and removes it from the stash list.

  • Tag a commit:
  git tag -a v1.0 -m "Version 1.0 release"

Creates an annotated tag for a specific commit.

  • Show differences between commits or changes:
  git diff

Displays changes between your working directory and the index or between commits.

Remote Management

  • List remote repositories:
  git remote -v

Shows all remotes with their URLs.

  • Add a new remote:
  git remote add <name> <url>

Adds a remote repository with a given name.

  • Remove a remote:
  git remote remove <name>

Removes the specified remote.