01Set up, once
git config --global user.name "Your Name"Sets the name recorded on every commit you make.
git config --global user.email "you@example.com"Sets the email recorded on every commit you make.
git clone <url>Copies a repository and its whole history into a new folder.
git initStarts a new repository in the current folder.
02See where you are
git statusShows your branch and which files are changed, staged or untracked.
git diffShows the changes you haven't staged yet, line by line.
git diff --stagedShows what is staged, which is what the next commit will contain.
git log --onelineLists commits one per line, newest first.
git log --oneline --graph --allDraws every branch's history as a text graph.
git show <commit>Shows one commit: its message, its author and its changes.
git blame <file>Shows which commit and author last changed each line.
03Save your work
git add <file>Stages that file's changes for the next commit.
git add -pWalks through your changes one chunk at a time and asks which to stage.
git add .Stages everything changed in the current folder and below, new files included.
git commit -m "message"Records what's staged as a new commit with that message.
git commit --amendReplaces your last commit with one that adds what's staged, and lets you reword the message. only before you push
git stashShelves uncommitted changes to tracked files and gives you a clean working tree.
git stash popPuts the most recent stash back and removes it from the stash list.
04Branch and merge
git branchLists local branches, with a star on the one you're on.
git switch <branch>Moves you to an existing branch.
git switch -c <branch>Creates a new branch from where you are and moves you onto it.
git merge <branch>Brings that branch's commits into the branch you're on.
git rebase mainReplays your branch's commits on top of your local main. They get new IDs.
git cherry-pick <commit>Copies one commit from anywhere onto your current branch.
git branch -d <branch>Deletes a local branch. Refuses if its work isn't merged.
05Sync with the remote
git remote -vLists the remotes and the URLs they point to.
git fetchDownloads new commits from the remote without touching your files.
git pullFetches, then brings the remote branch's new commits into yours.
git pull --rebaseFetches, then replays your unpushed commits on top of the remote's.
git pushSends your branch's new commits to its upstream branch.
git push -u origin <branch>The first push of a new branch. Sets its upstream, so a plain git push works from then on.
git push --force-with-leaseOverwrites the remote branch with yours, but refuses if someone has pushed since your last fetch. your own branches only
06Undo
git restore <file>Throws away your unstaged changes to that file. can't be undone
git restore --staged <file>Unstages the file and keeps your changes.
git revert <commit>Adds a new commit that reverses an earlier one. Safe on shared branches.
git reset --soft HEAD~1Removes the last commit and leaves its changes staged.
git reset --hard HEAD~1Removes the last commit and discards its changes, along with any other uncommitted changes to tracked files. uncommitted work is gone for good
git reflogLists where HEAD has pointed recently, so you can find a commit you thought was lost.
07When a merge or rebase conflicts
Edit each file between the <<<<<<< and >>>>>>> markers, git add it, then:
git merge --continueFinishes the merge once every conflict is fixed and staged.
git merge --abortGives up on the merge and puts everything back as it was.
git rebase --continueCarries on with the rebase after you fix and stage a conflict.
git rebase --abortGives up on the rebase and puts your branch back as it was.
08Find things
git grep "text"Searches the tracked files in the current folder and below for that text.
git log -S "text"Lists the commits that added or removed that text.
git log -- <file>Lists the commits that changed that file.
git log --author="name"Lists the commits made by that author.