Setup
git config --global user.name "Your Name"git config --global user.email "you@example.com"git config --global init.defaultBranch maingit config --global pull.rebase false # merge on pull (true = rebase)git config --list --show-origin # what's set, and whereStart
git init # new repository heregit clone git@github.com:acme/app.git # copy an existing onegit clone --depth 1 URL # only the latest commitgit remote add origin URL # connect to a remotegit remote -v # which remotes?Every day
git status # what changed?git diff # changes not staged yetgit diff --staged # changes about to be committedgit add file.txt # stage a filegit add -p # stage part of a file, piece by piecegit commit -m "Message" # commit what's stagedgit pull # fetch and mergegit push # send your commitsgit push -u origin my-branch # first push of a new branchUndo
git restore file.txt # discard changes in a file (they're gone)git restore --staged file.txt # unstage, and keep the changesgit commit --amend # fix the last commit (message or content)git reset --soft HEAD~1 # undo the last commit, keep it stagedgit reset HEAD~1 # undo the last commit, keep changes unstagedgit reset --hard HEAD~1 # undo the last commit and DELETE its changesgit revert abc1234 # new commit that undoes an old one (safe if pushed)git clean -nd # list untracked files that would be removedgit clean -fd # remove themWarning
reset --hard, restore and clean -f delete uncommitted work for good, as Git never saved it. On commits you’ve already pushed and others may have, use revert, not reset.
Recover
git reflog # everywhere HEAD has been, even after a resetgit reset --hard HEAD@{1} # go back to where you just weregit branch rescue abc1234 # put a branch on a "lost" commitgit checkout abc1234 -- file # get one file back from a commitgit log --diff-filter=D -- file # in which commit was this file deleted?Branches
git branch # local branchesgit branch -a # with the remote onesgit switch my-branch # go to a branchgit switch -c new-branch # create it and go theregit branch -d my-branch # delete, if it's mergedgit branch -D my-branch # delete, even if it isn'tgit branch -m old new # renamegit push origin --delete old # delete on the remotegit fetch --prune # forget remote branches that are goneMerge and rebase
git merge feature # bring feature into this branchgit merge --abort # give up on a merge with conflictsgit rebase main # replay this branch on top of maingit rebase --continue # after fixing a conflictgit rebase --abort # back to where you startedgit cherry-pick abc1234 # copy one commit heregit checkout --theirs file # in a conflict, take their versiongit checkout --ours file # in a conflict, take oursIn a rebase, “ours” and “theirs” swap places: “ours” is the branch you’re replaying onto.
Stash
git stash # set changes asidegit stash -u # with untracked filesgit stash list # what's theregit stash pop # bring back the latest, and drop itgit stash apply stash@{1} # bring back one, and keep itgit stash drop stash@{1} # delete oneLook around
git log --oneline --graph --all -n 20 # the shape of historygit log -p file # every change to a filegit log --author="Ana" --since="2 weeks ago"git log -S"functionName" # commits that added or removed this textgit show abc1234 # one commitgit blame file # who last changed each linegit diff main...feature # what feature adds since it branchedRemotes
git fetch origin # get updates, change nothinggit pull --rebase # replay your commits on topgit push --force-with-lease # overwrite, unless the remote has movedgit remote set-url origin URL # change the addressgit tag v1.2.3 # tag this commitgit push origin v1.2.3 # send a tag