Added sections for staging, stashing, inspecting, remote, tags, cherry-pick, and cleaning commands.
3.5 KiB
Configuration
Set the global config
git config --global user.name "[name]"
git config --global user.email "[email]"
Get started
Create a git repository
git init
Pull a git repository
git pull [url]
Clone an existing git repository
git clone [url]
Staging
Stage specific files
git add [file]
Stage all changes
git add .
Interactively stage parts of a file (hunks)
git add -p [file]
Unstage a file but keep changes
git restore --staged [file]
Commit
Commit all tracked changes
git commit -am "[commit message]"
Add new modifications to the last commit
git commit --amend --no-edit
Branches
Lists all local branches in your repository (use -a for local and remote branches)
git branch
Switch to an existing branch
git switch [branch name]
Create a new branch
git checkout -b [branch name]
Stashing
Save uncommitted changes for later
git stash
List all stashes
git stash list
Apply most recent stash and remove it from the list
git stash pop
Apply a specific stash without removing it
git stash apply stash@{2}
Inspecting
Show changes between working directory and staging
git diff
Show changes between staging and last commit
git diff --staged
Show commit history for a specific file
git log --follow [file]
Show who changed each line in a file
git blame [file]
Remote
Add a remote repository
git remote add origin [url]
List remote repositories
git remote -v
Push a branch and set upstream tracking
git push -u origin [branch-name]
Delete a remote branch
git push origin --delete [branch-name]
Tags
Create a tagged release
git tag -a v1.0.0 -m "Release v1.0.0"
Push tags to remote
git push origin --tags
I've made a mistake
Change last commit message
git commit --amend
Undo most recent commit and keep changes
git reset HEAD~1
Undo the N most recent commit and keep changes
git reset HEAD~N
Undo most recent commit and get rid of changes
git reset HEAD~1 --hard
Reset branch to remote state
git fetch origin
git reset --hard origin/[branch-name]
Revert a commit by creating a new undo commit (safe for shared branches)
git revert [commit-hash]
Discard all uncommitted changes in working directory
git restore .
Recover a deleted branch or lost commit
git reflog
git checkout -b [branch-name] [commit-hash]
Cherry-pick
Apply a specific commit from another branch
git cherry-pick [commit-hash]
Cleaning
Remove untracked files (dry run first)
git clean -n
Remove untracked files and directories
git clean -fd
Miscellaneous
Renaming the local master branch to main
git branch -m master main
Checking log graph
git log --graph
Checking log graph (merges only)
git log --graph --merges
Tracking down a bad commit using binary search
git bisect start
git bisect good 13c988d4f15e06bcdd0b0af290086a3079cdadb0
git bisect bad ca82a6dff817ec66f44342007202690a93763949
Pulling new changes into current branch from mainline
git checkout [branch-name]
git fetch origin [master-branch-name]
git rebase origin/[master-branch-name]