Git Cheat Sheet

learngitbranching.js.org - git training simulator

Git Configuration

// Set username
git config --global user.name "Your Name"

// Set email
git config --global user.email "example@email.com"

// Check current configuration
git config --list

Getting Started

// Create new repository
git init

// Clone remote repository
git clone <repository URL>

Basic Commands

// Check repository status (new/modified files)
git status

// Add files to index (staging area)
git add <filename or .>

// Commit changes
git commit -m "Commit message"

// View commit history
git log
// Brief history:
git log --oneline --graph --decorate --all

// Send changes to remote repository
git push

// Get changes from remote repository
git pull

Working with Branches

// Create new branch and switch to it
git checkout -b <branch_name>

// Switch to existing branch
git checkout <branch_name>

// View list of local branches
git branch

// Delete branch locally
git branch -d <branch_name>

// Merge branch into current
git merge <branch_name>

Tip:

Before merge or rebase make sure you have no uncommitted changes to avoid conflicts.

Git Merge vs Git Rebase

// Merge
// merge creates merge commit and preserves branching history.
git checkout main
git merge feature

// Rewriting history (rebase)
// rebase "rewrites" commits on top of another branch, creating linear history.
git checkout feature
git rebase main

Git Merge

  • Preserving history: When merging with git merge, history of both branches is preserved. This means all forks and merges are visible in commit graph.
  • Merge commit: Additional merge commit is created that combines branch history.
  • Safety: Merge doesn't change already existing history, which is especially important when working with public branches where multiple developers may work on same branch.

Git Rebase

  • Linear history: git rebase rewrites history by moving commits of one branch to end of another. This results in cleaner, linear history without merge commits.
  • Changing history: Rebase changes commit history, which can lead to conflicts if changes are already published and used by other developers.
  • Convenience for preparation: Often used for "cleaning up" history before merging branches (e.g., before sending pull request) to avoid large number of merge commits.

Working with Remote Repositories

// Add new remote repository
git remote add <name> <URL>

// View list of remote repositories
git remote -v

// Fetch changes from remote repository (without merging)
git fetch <repo_name>

// Merge changes after fetch
git merge <branch_name> 

Undoing Changes

// Undo changes in working directory (before commit)
git checkout -- <filename>

// Remove file from index (but keep changes in working directory)
git reset <filename>

// "Reset" branch to specific commit
git reset --hard <commit_hash>

Warning:

git reset --hard command irreversibly deletes changes that aren't committed and not saved in other branches.

Working with Tags

// Create lightweight tag
git tag v1.0

// Create annotated tag
git tag -a v1.0 -m "Version 1.0"

// View list of tags
git tag

// Send tags to remote repository
git push --tags
// View difference between working directory and index
git diff

// View difference between index and last commit
git diff --staged

// View changes between two commits
git diff <commit1> <commit2>

// Search commit by message/author:
git log --grep="search in comments"
git log --author="Name"

Git Stash

If you need to urgently switch to another branch, but have uncommitted changes:

// Save changes to temporary storage
git stash

// View list of "stashes"
git stash list

// Restore last stash
git stash pop