Git Basics
The Git commands you use daily: record changes, look at history, branch and merge, share with a remote, and undo mistakes.
Set up
git config --global user.name "Your Name"Your name on every commit
git config --global user.email "you@example.com"Your email on every commit
git config --global init.defaultBranch mainName new repositories main
git initStart a repository in the current folder
git clone https://github.com/org/repo.gitCopy a remote repository to your machine
Record changes
Working folder -> staging area -> commit.
git status -sbShort status with the branch
git add file.txt | git add -pStage a file, or choose pieces of a file to stage
git commit -m "Fix login redirect"Save the staged changes as a commit
git diff | git diff --stagedSee unstaged changes, or what is about to be committed
git restore file.txtThrow away unstaged changes in a file
git restore --staged file.txtUnstage a file but keep the changes
git rm --cached secret.envStop tracking a file but keep it on disk
Look at history
git log --oneline --graph --allA compact picture of all branches
git log -p -- path/fileThe changes made to one file over time
git show abc1234What one commit changed
git blame file.pyWho last changed each line
git log --author="Ann" --since="2 weeks ago"Filter the history
git log -S"needle"Find commits that added or removed a piece of text
Branches
git switch -c feature/loginCreate a branch and move to it
git switch mainMove to another branch
git branch -vvList branches with their upstream and last commit
git merge feature/loginBring a branch into the current one
git rebase mainReplay your commits on top of the latest main
git branch -d feature/loginDelete a branch that is already merged
git cherry-pick abc1234Copy one commit onto the current branch
Remotes
git remote -vShow the remotes
git fetch --pruneDownload new work and forget deleted branches, without merging
git pull --rebaseFetch and replay your commits on top
git push -u origin feature/loginPublish a branch and remember it as the upstream
git push --force-with-leaseOverwrite a remote branch, but only if nobody else pushed
Undo things
git commit --amendChange the last commit (message or content) before you share it
git reset --soft HEAD~1Undo the last commit, keep the changes staged
git reset --hard HEAD~1Undo the last commit and delete its changes
git revert abc1234Add a new commit that undoes an old one (safe for shared history)
git stash push -u -m "wip"Put work in progress aside, including new files
git stash popBring the stashed work back
git reflogEvery place HEAD has been: the way back from a bad reset
Conflicts and tags
git merge --abort | git rebase --abortGive up on a merge or rebase that went wrong
git checkout --ours file | --theirs fileTake one side of a conflicted file
git add file && git rebase --continueFinish a rebase step after fixing a conflict
git tag -a v1.2.0 -m "Release 1.2.0"Mark a release
git push origin v1.2.0Publish a tag
echo "node_modules/" >> .gitignoreNever track a folder or file pattern
Nothing matches that search.
Cheatsheet for the Git Basics path · Deboistech · All cheatsheets