Create a new repository
git initCreates an empty Git repository in the current folder, stored in a hidden .git folder. No files are tracked until you add and commit them.
git initFree · Customize it · Download it · Print it
We used git-sim to build an animated Git cheat sheet you can customize with just the commands you need.
(because only you know which commands you always forget...)
A repository is a project folder with its history in a hidden .git folder. Make a new one, or clone one that already exists, history and all.
git initCreates an empty Git repository in the current folder, stored in a hidden .git folder. No files are tracked until you add and commit them.
git initgit clone <url>Downloads the repository at <url> with its full history into a new folder, checks out its default branch, and adds the source as the remote origin.
git clone https://example.com/your_project.gitgit clone --depth <N> <url>Clones only the <N> latest commits of the default branch, leaving out the older history.
git clone --depth 3 https://example.com/your_project.gitgit clone -b <branch> <url>Clones the repository and checks out <branch> instead of the remote's default branch.
git clone -b feature https://example.com/your_project.gitSettings made with --global apply to all your repositories on this machine and are saved in ~/.gitconfig. Leave out --global to set something for the current repository only.
git config --global user.name "<name>"Sets the name Git records as the author and committer of each new commit.
git config user.name "Jacob Stopak"git config --global user.email "<email>"Sets the email address Git records for the author and committer of each new commit.
git config --global user.email "jacob@initialcommit.io"git config --global --unset user.emailgit config --global init.defaultBranch <name>Sets the name git init gives the first branch of a new repository.
git config --global init.defaultBranch maingit config --global core.editor "<editor>"Sets the editor Git opens for commit messages and interactive rebases. For VS Code, use code --wait so Git waits until the file is closed.
git config --global core.editor "code --wait"git config --listPrints every setting from the system, global, and repository config files. When a setting is in more than one file, the repository value overrides the global value, and the global value overrides the system value.
git config --listgit config --system <setting> <value>Sets <setting> to <value> in the system config file, which applies to every user and repository on the machine. A global or repository value for the same setting overrides it.
git config --system core.autocrlf truegit config --global alias.<name> <command>Creates a Git command named <name> that runs <command>. For example, alias.st status makes git st run git status.
git config --global alias.st statusgit config --global pull.rebase trueMakes git pull rebase your local commits onto the fetched commits instead of creating a merge commit.
git config --global pull.rebase truegit config --global fetch.prune trueMakes git fetch delete remote-tracking branches, like origin/<branch>, whose branches no longer exist on the remote.
git config --global fetch.prune truegit config --global core.autocrlf trueConverts LF line endings to CRLF when files are checked out, and back to LF when they're committed.
git config --global core.autocrlf truegit config --global credential.helper <helper>Stores your username and password or token with <helper>, like manager on Windows or osxkeychain on macOS, so Git doesn't ask for them each time.
git config --global credential.helper cacheCommitting takes two steps: stage the changes you want, then commit them as one snapshot.
git statusShows the active branch, staged changes, unstaged changes, and untracked files. During a merge, rebase, or cherry-pick, it also shows that operation's state and the next step.
git statusgit add <file>Stages the current changes to <file>, so they're included in the next commit.
git add app.py notes.txtgit add .Stages every new, modified, and deleted file in the current folder and its subfolders.
git add .git add -AStages every new, modified, and deleted file in the repository, whichever folder it's run from.
git add -Agit commit -m "<message>"Creates a new commit from the staged changes with the message <message>, and moves the active branch to it.
git commit -m "Describe the project in the README"git reset --soft HEAD~1git commit -am "<message>"Stages the changes to all tracked files and commits them with the message <message>. Untracked files aren't included.
git commit -a -m "Tweak the app and the header"git reset --soft HEAD~1Delete, rename, untrack, and ignore files, and clear out the ones Git doesn't track.
git rm <file>Deletes <file> from the working directory and stages the deletion.
git rm login.htmlgit restore --staged --worktree <file>git rm --cached <file>Removes <file> from the staging area, so the next commit stops tracking it. The file stays in the working directory.
git rm --cached .envgit add <file>git mv <old> <new>Renames or moves <old> to <new> and stages the change.
git mv app.py main.pygit mv <new> <old>git check-ignore -v <file>Prints the ignore file, line number, and pattern that ignore <file>. Prints nothing if the file isn't ignored.
git check-ignore -v debug.log build/app.js app.pygit clean -fDeletes the untracked files in the working directory. Add -d to delete untracked folders too.
git clean -fgit clean -n first to list what would be deleted.git clean -fdxDeletes all untracked files and folders, including ignored ones like build output and .env files.
git clean -fdxgit clean -ndx first to list what would be deleted.Every commit keeps its author, date, and changes. These commands list them, filter them, and search through them. None of them change anything.
git logLists the commits in the active branch's history, newest first, with each commit's hash, author, date, and message.
git loggit log --allLists the commits in the history of every branch and tag, not only the active branch.
git log --allgit log --oneline --graph --allPrints the history of all branches as a text graph, one line per commit, with branch and tag names next to their commits.
git log --oneline --graph --allgit log -p <file>Lists the commits that changed <file>, each with the lines it added and removed.
git log -p app.pygit log --follow <file>Lists the commits that changed <file>, including the ones from before it was renamed.
git log --follow main.pygit log -S "<text>"Lists the commits that added or removed <text>, found by checking where the number of times <text> appears changes.
git log -S "logging"git log --author "<name>"Lists the commits whose author name or email matches <name>.
git log --author "Ada"git log --since "<date>" --until "<date>"Lists the commits made between the two dates. Exact dates like 2024-05-01 and relative ones like 2 weeks ago both work.
git log --since "2024-05-01 11:00 +0000" --until "2024-05-01 13:00 +0000"git show <commit>Shows a commit's hash, author, date, and message, followed by the changes it made.
git show HEAD~1git blame <file>Prints each line of <file> with the commit, author, and date that last changed it.
git blame app.pygit shortlog -snLists each author who made commits on the active branch, with their number of commits, highest first.
git shortlog -sngit describeNames the current commit after the most recent annotated tag in its history, like v1.0-2-g1a2b3c4: the tag v1.0, 2 commits since it, and g followed by the commit's short hash.
git describegit grep -n "<text>"Searches every tracked file for <text> and prints each matching line with its file name and line number.
git grep -n logginggit bisect start <bad> <good>Starts a binary search between the <bad> and <good> commits by checking out the commit halfway between them. Test each commit Git checks out and mark it with git bisect good or git bisect bad until Git reports the first bad commit.
git bisect start HEAD 4114b2cSee exactly which lines changed between your files, the staging area, commits, and branches. Nothing is changed.
git diffShows the changes in the working directory that aren't staged yet, line by line.
git diffgit diff --stagedShows the staged changes line by line, which is what the next commit will contain.
git diff --stagedgit diff <commit> <commit>Shows the changes between two commits. Branch and tag names work in place of commit hashes.
git diff HEAD~2 HEADgit diff <branch>..<branch>Shows the changes between the latest commits of the two branches.
git diff main..featuregit diff --statLists each file with unstaged changes and the number of lines added and removed in it.
git diff --statA branch is a movable pointer to a commit, and HEAD points to the active branch.
git branch <name>Creates a branch named <name> that points to the current commit. HEAD stays on the active branch.
git branch bugfix-headergit branch -aLists local branches, then remote-tracking branches like remotes/origin/main. A * marks the active branch.
git branch -agit branch -vvLists each local branch with its latest commit, its upstream branch, and how many commits it's ahead or behind, like [origin/main: ahead 1, behind 2].
git branch -vvgit branch --mergedLists the branches whose latest commit is already in the active branch's history.
git branch --mergedgit branch --no-mergedLists the branches that have commits not in the active branch's history.
git branch --no-mergedgit switch <branch>Points HEAD at <branch> and updates the staging area and working directory to match its latest commit.
git switch featuregit switch -Switches to the branch that was active before the current one.
git switch -git switch <branch>When no local branch named <branch> exists but origin/<branch> does, creates the local branch from it, sets it to track origin/<branch>, and switches to it.
git switch featuregit switch -c <name>Creates a branch named <name> at the current commit and switches to it.
git switch -c hotfixgit switch -c <name> <start>Creates a branch named <name> at <start>, which can be a commit hash, a tag, or a branch like origin/main, and switches to it.
git switch -c search origin/featuregit branch -m <old> <new>Renames the branch <old> to <new>. Its commits don't change.
git branch -m feature searchgit branch -m <new> <old>git branch -d <name>Deletes the branch <name>. Git refuses if the branch has commits that aren't merged into its upstream branch, or into the active branch if it has no upstream.
git branch -d docsgit branch <name> <hash>git branch -D <name>Deletes the branch <name> even if it has unmerged commits. Afterward, those commits are reachable only through the reflog.
git branch -D featuregit branch <name> <hash>git switch --detach <commit>Points HEAD directly at <commit> instead of at a branch, and updates the working directory to match. Create a branch before committing there.
git switch --detach 96c4fc2A merge brings another branch's work into yours and keeps the history as it happened.
git merge <branch>Merges <branch> into the active branch. When both have new commits, Git creates a merge commit with two parents.
git merge featuregit reset --hard ORIG_HEADgit merge <branch>When the active branch has no commits that <branch> lacks, Git moves the active branch forward to <branch>'s latest commit. No merge commit is created.
git merge maingit merge --no-ff <branch>Creates a merge commit even when a fast-forward is possible, so the merged branch stays visible in the history.
git merge --no-ff maingit reset --hard ORIG_HEADgit merge --squash <branch>Stages the combined changes from <branch> without committing or recording a merge. The next commit contains them as one commit.
git merge --squash featuregit merge --continueCreates the merge commit once the conflicts are resolved and the files are staged with git add.
git merge --continuegit merge --abortStops the merge and returns the branch, staging area, and working directory to their state before it started.
git merge --abortRun the merge againA rebase replays your commits on top of another branch, so the history reads as one line. A cherry-pick replays single commits the same way.
git rebase <branch>Replays the active branch's commits on top of <branch>, one at a time, as new commits with new hashes.
git rebase maingit reset --hard ORIG_HEADgit rebase -i <base>Opens an editable list of the commits after <base>. Change pick to reword, squash, or drop, or reorder the lines, and Git replays the commits as listed when you save and close the file.
git rebase -i maingit reset --hard ORIG_HEADgit rebase --onto <new-base> <old-base>Replays only the commits after <old-base> onto <new-base>. The commits before <old-base> stay where they are.
git rebase --onto main featuregit reset --hard ORIG_HEADgit rebase --continueAfter the conflicts are resolved and staged, commits the stopped commit and continues replaying the remaining commits.
git rebase --continuegit rebase --skipSkips the commit that caused the conflict and continues replaying the remaining commits.
git rebase --skipgit rebase --abortStops the rebase and returns the branch to the commit it pointed to before the rebase started.
git rebase --abortRun the rebase againgit cherry-pick <commit>Applies the changes from <commit> to the active branch as a new commit.
git cherry-pick fc19889git reset --hard HEAD~1git cherry-pick <from>..<to>Applies each commit after <from> up to and including <to>, in order, as new commits on the active branch.
git cherry-pick 96c4fc2..featureA remote is a name for the URL of another repository of the same project. A clone starts with one named origin.
git remote add <name> <url>Adds a remote named <name> for the repository at <url>, so you can fetch from it and push to it.
git remote add upstream ../your_project.gitgit remote -vLists each remote with its fetch URL and push URL.
git remote -vgit remote show <name>Shows the remote's URLs, its branches, which of them are tracked, and what git pull and git push do for each local branch.
git remote show origingit remote set-url <name> <url>Changes the URL of the remote <name> to <url>.
git remote set-url origin https://example.com/your_project.gitgit remote set-url <name> <old-url>git remote rename <old> <new>Renames the remote <old> to <new>, along with its remote-tracking branches and settings.
git remote rename origin upstreamgit remote rename <new> <old>git remote remove <name>Removes the remote <name> with its remote-tracking branches and settings. The remote repository itself isn't affected.
git remote remove origingit remote add <name> <url>git ls-remoteLists the remote's branches and tags and the commit each points to, without downloading anything.
git ls-remotegit branch -u origin/<branch>Sets origin/<branch> as the active branch's upstream, so plain git pull and git push use it.
git branch -u origin/featureFetch and pull bring a remote's new commits down, and push sends yours up.
git fetchDownloads new commits from the remote and updates its remote-tracking branches, like origin/main. Local branches and the working directory don't change.
git fetchgit fetch <remote> <branch>Downloads only <branch>'s new commits from <remote> and updates <remote>/<branch>. Local branches and the working directory don't change.
git fetch origin maingit fetch --allFetches from every remote, like origin and upstream, and updates all their remote-tracking branches.
git fetch --allgit fetch --pruneFetches, then deletes remote-tracking branches, like origin/<branch>, whose branches no longer exist on the remote.
git fetch --prunegit pullFetches the active branch's upstream and merges it into the active branch. When both have new commits, Git creates a merge commit.
git pullgit reset --hard ORIG_HEADgit pull origin <branch>Fetches <branch> from origin and merges it into the active branch, even if the active branch tracks a different branch.
git pull origin maingit reset --hard ORIG_HEADgit pull --rebaseFetches the active branch's upstream, then replays your local commits on top of it instead of merging.
git pull --rebasegit pushSends the active branch's new commits to its upstream branch on the remote.
git pushgit push -u origin <branch>Creates <branch> on origin, pushes its commits, and sets it as the local branch's upstream.
git push -u origin hotfixgit push --force-with-leaseOverwrites the remote branch with your local branch, but only if the remote branch hasn't changed since your last fetch.
git push --force-with-leasegit push --forceOverwrites the remote branch with your local branch, even if the remote has commits you don't have.
git push --force--force-with-lease instead.git push origin --delete <branch>Deletes <branch> on the remote. The local branch isn't affected.
git push origin --delete featuregit push origin <branch>A tag is a fixed pointer to a commit, usually for a release like v1.0. Unlike a branch it doesn't move, and it isn't sent to a remote until you push it.
git tag <name>Creates a lightweight tag named <name> that points to the current commit.
git tag v1.0git tag -a <name> -m "<message>"Creates an annotated tag named <name> on the current commit, storing the tagger, the date, and <message>.
git tag -a v1.0 -m "First release"git tag -d <name>git tag -lLists all tags in alphabetical order.
git tag -lgit tag -d <name>Deletes the local tag <name>. The commit it pointed to isn't affected.
git tag -d v1.0git push origin <tag>Pushes the tag <tag>, and any commits it needs, to the remote.
git push origin v1.1git push origin --delete <tag>git push --tagsPushes every local tag the remote doesn't have yet.
git push --tagsgit push origin --delete <tag>Deletes the tag <tag> on the remote. The local tag isn't affected.
git push origin --delete v1.0git push origin <tag>The stash saves changes you're not ready to commit and cleans the working directory, so you can switch branches and reapply the changes later.
git stashSaves the staged and unstaged changes to tracked files as a new stash entry, and resets the staging area and working directory to the last commit.
git stashgit stash popgit stash -uStashes untracked files as well as the changes to tracked files.
git stash -ugit stash popgit stash push -m "<message>"Stashes the changes as an entry with the message <message>, which git stash list shows.
git stash push -m "Try a grid header"git stash popgit stash push <file>Stashes only the changes to <file>, leaving all other changes in place.
git stash push styles.cssgit stash popgit stash listLists every stash entry, newest first, as stash@{0}, stash@{1}, and so on.
git stash listgit stash popApplies the latest stash entry to the working directory and removes it from the stash. If applying it conflicts, the entry is kept.
git stash popgit stash applyApplies the latest stash entry to the working directory and keeps it on the stash.
git stash applygit stash dropDeletes the latest stash entry, or the one named, like stash@{1}.
git stash dropgit stash apply <hash> restores it until Git deletes it.git stash show -pShows the changes in the latest stash entry, line by line. Name an entry to see another, like stash@{1}.
git stash show -pgit stash branch <name>Creates the branch <name> at the commit the latest stash entry was created on, applies the entry there, and drops it if it applied cleanly.
git stash branch try-hellogit stashgit stash clearDeletes every stash entry.
git stash cleargit stash list first.Most mistakes in Git can be undone, as long as you know which command undoes what. The badge on each card says what's at stake.
git restore --staged <file>Unstages the changes to <file>, so they're not in the next commit. The file in the working directory doesn't change.
git restore --staged README.mdgit restore <file>Discards the unstaged changes to <file>, restoring it from the staging area.
git restore app.pygit stash first if you're not sure.git restore .Discards the unstaged changes to every file in the current folder and its subfolders. Staged changes are kept.
git restore .git clean for those.git checkout -- .Discards the unstaged changes in the current folder, like git restore . does. The -- marks what follows as file paths, not a branch.
git checkout -- .git restore --source <commit> <file>Replaces <file> in the working directory with its version from <commit>. Commit the file to keep that version.
git restore --source HEAD~1 app.pygit restore <file><file> can't be recovered.git commit --amend -m "<message>"Replaces the last commit with a new commit that includes any staged changes and has the message <message>. Leave out -m to edit the existing message.
git commit --amend -m "Update dependencies and README"git reset --soft HEAD@{1}git reset --soft HEAD~<N>Moves the active branch back <N> commits (HEAD~1 undoes only the last one). Their changes stay staged.
git reset --soft HEAD~1git reset --soft ORIG_HEADgit reset HEAD~<N>Moves the active branch back <N> commits (HEAD~1 undoes only the last one) and unstages their changes. The working directory doesn't change.
git reset HEAD~1git reset ORIG_HEADgit reset --hard <commit>Moves the active branch to <commit> and resets the staging area and working directory to match it.
git reset --hard HEAD~2git reset --hard ORIG_HEADgit reset --hard origin/<branch>Moves the active branch to origin/<branch> and resets the staging area and working directory to match, discarding local commits and changes. Run git fetch first.
git reset --hard origin/maingit reset --hard ORIG_HEADgit revert <commit>Creates a new commit that reverses the changes from <commit>. Existing commits aren't rewritten, so it's safe on shared branches.
git revert HEADgit revert <the new commit>git revert -m 1 <merge>Creates a new commit that reverses the changes a merge brought in. -m 1 makes Git reverse them relative to the merge's first parent, the branch the merge was made on.
git revert -m 1 HEADgit revert <the revert commit>git revert --no-commit <from>..<to>Reverses each commit after <from> up to and including <to> and stages the result without committing, so one commit can undo them all.
git revert --no-commit HEAD~2..HEADgit revert --abortgit reflogLists every commit HEAD has pointed to, newest first, including commits that no branch points to anymore.
git refloggit reset --hard HEAD@{1}HEAD@{1} is the commit HEAD pointed to before its last move, so this resets the branch to where it was before the reset.
git reset --hard HEAD@{1}reset --hard, it discards uncommitted changes.git branch <name> <hash>Creates the branch <name> at <hash>, its last commit. Git prints the hash when it deletes a branch, and git reflog shows it too.
git branch feature 1117a34Nothing matches that search.
Build your own Git cheat sheet in 3 steps
The Animated Git Cheat SheetFree · Customize it · Download it · Print it
Git Cheat Sheet: 12 Essential Git Commands for Intermediate Users
In this article, we’ll discuss 12 essential Git commands for intermediate users.
Git cheat sheet: 12 essential Git commands for beginners
In this article, I will explain 12 essential Git commands that are especially important for a beginner.