Remove files from the working tree and from the index
git rm file.txt # Remove file
git rm --cached file.txt # Remove from index but keep
locally
git mv [old] [new]
Move or rename a file, a directory, or a symlink
git mv oldname.txt newname.txt
2
Branching
git branch
List, create, or delete branches
git branch # List local branches
git branch -a # List all branches (local and
remote)
git branch new-branch # Create new branch
git branch -d branch-name # Delete branch
git checkout [branch]
Switch branches or restore working tree files
git checkout main # Switch to main branch
git checkout -b new-branch # Create and switch to new
branch
git checkout file.txt # Discard changes to file
git switch [branch]
Switch branches (Git 2.23+)
git switch main # Switch to main branch
git switch -c new-branch # Create and switch to new
branch
git merge [branch]
Join two or more development histories together
git merge feature-branch # Merge feature-branch into current
branch
git merge --no-ff feature-branch # Merge with explicit merge
commit
git rebase [branch]
Reapply commits on top of another base tip
git rebase main # Rebase current branch onto main
git rebase -i HEAD~3 # Interactive rebase of last 3
commits
git cherry-pick [commit]
Apply the changes introduced by some existing commits
git cherry-pick abc1234 # Apply changes from commit
abc1234
git cherry-pick abc1234..def5678 # Apply range of
commits
git fetch # Fetch from default remote
git fetch origin # Fetch from specific remote
git fetch --prune # Remove remote-tracking branches that no
longer exist
git pull [remote] [branch]
Fetch from and integrate with another repository or local branch
git pull # Pull from default remote
git pull origin main # Pull specific branch
git pull --rebase # Pull with rebase instead of merge
git push [remote] [branch]
Update remote refs along with associated objects
git push # Push to default remote
git push origin main # Push specific branch
git push -u origin main # Push and set upstream
git push --force # Force push (use with caution!)
Use binary search to find the commit that introduced a bug
git bisect start
git bisect bad # Current version is bad
git bisect good abc1234 # Commit abc1234 is known to be
good
git bisect reset # End bisect session
git reflog
Manage reflog information
git reflog # Show reference logs
git reflog show main # Show reflog for main branch
git reset [commit]
Reset current HEAD to the specified state
git reset --soft HEAD~1 # Undo commit but keep changes
staged
git reset --mixed HEAD~1 # Undo commit and unstage changes
(default)
git reset --hard HEAD~1 # Discard commit and all changes
(careful!)