git config --global user.name "Your Name"Set the name attached to your commits.
Git commands are the instructions you type in a terminal to control Git: git, then a command like commit, then options. This page lists the 107 you'll actually use, grouped by task, each with a one-line explanation you can copy.
git status → git add . → git commit -m "msg" → git pull → git push, plus git switch -c for new branches and git log --oneline to read history.
Tell Git who you are and how it should behave. Most of these run once per machine.
git config --global user.name "Your Name"Set the name attached to your commits.
git config --global user.email "you@example.com"Set the email attached to your commits.
git config --global init.defaultBranch mainMake new repos start on a branch called main.
git config --global core.editor "code --wait"Use VS Code as the editor for commit messages.
git config --list --show-originShow every setting Git is using and the file it came from.
git config --global alias.st statusCreate a shortcut, so git st runs git status.
git config --global pull.rebase trueMake git pull rebase instead of merge by default.
git versionShow which version of Git is installed.
$ git version
git version 2.56.1git help <command>Open the manual page for any command.
git help -aList every Git command installed on your machine.
Create a new repo or copy an existing one.
git initTurn the current folder into a Git repository. Full guide →
$ git init
Initialized empty Git repository in /projects/app/.git/git init <folder>Create a new folder and make it a repository.
git clone <url>Download a full copy of a remote repo, history included.
$ git clone https://github.com/user/app.gitgit clone <url> <folder>Clone into a folder name of your choice.
git clone --depth 1 <url>Clone only the latest snapshot. Much faster for big repos.
git clone --branch <name> <url>Clone and check out a specific branch right away.
The daily loop: check what changed, stage it, save it.
git statusSee which files are changed, staged or untracked.
$ git status
On branch main
Changes not staged for commit:
modified: index.htmlgit status -sShort, one-line-per-file status.
git add <file>Stage one file for the next commit.
git add .Stage every change in the current folder and below.
git add -AStage all changes in the whole repo, deletions included.
git add -pPick which pieces of a file to stage, hunk by hunk.
git commit -m "message"Save staged changes as a new commit.
$ git commit -m "Add login form"
[main 3f2a1c9] Add login form
1 file changed, 24 insertions(+)git commit -am "message"Stage all tracked files and commit in one step.
git commit --amendFix the last commit's message or add forgotten files to it. Full guide →
git commit --amend --no-editAdd staged changes to the last commit, keep its message.
git rm <file>Delete a file and stage the deletion.
git rm --cached <file>Stop tracking a file but keep it on disk.
git mv <old> <new>Rename or move a file and stage the change.
Work on features in isolation, then bring them back together.
git branchList local branches. The current one has a *.
git branch -aList local and remote branches.
git branch <name>Create a new branch but stay where you are.
git switch <name>Move to another branch.
git switch -c <name>Create a new branch and switch to it.
$ git switch -c feature/login
Switched to a new branch 'feature/login'git switch -Jump back to the branch you were on before.
git checkout <name>Older way to switch branches. Still works everywhere.
git checkout -b <name>Older way to create and switch to a new branch.
git branch -m <new-name>Rename the current branch.
git merge <branch>Merge another branch into the one you're on.
git merge --no-ff <branch>Merge and always create a merge commit.
git merge --abortCancel a merge that has conflicts and go back.
git rebase <branch>Replay your commits on top of another branch.
git rebase -i HEAD~3Edit, squash, reorder or drop your last 3 commits.
git rebase --continueCarry on after fixing a rebase conflict.
git rebase --abortCancel a rebase and go back to how things were.
git branch -d <name>Delete a branch that's already merged.
git branch -D <name>Force-delete a branch, merged or not.
Sync your work with GitHub, GitLab, Bitbucket or any server.
git remote -vShow which remotes are connected and their URLs.
git remote add origin <url>Connect your local repo to a remote one.
git remote set-url origin <url>Change the URL of an existing remote.
git remote remove <name>Disconnect a remote.
git fetchDownload new commits from the remote without changing your files. Full guide →
git fetch --pruneFetch and clean up branches deleted on the remote.
git pullFetch and merge remote changes into your current branch. Full guide →
git pull --rebaseFetch and rebase instead of merging. Keeps history straight. Full guide →
git pushUpload your commits to the remote.
git push -u origin <branch>Push a new branch and set it to track the remote.
$ git push -u origin feature/logingit push origin --delete <branch>Delete a branch on the remote.
git push --force-with-leaseForce-push, but refuse if someone else pushed first.
Read history and see exactly what changed.
git logShow the commit history of the current branch.
git log --onelineOne line per commit.
git log --oneline --graph --allCompact, visual history of every branch.
$ git log --oneline --graph --all
* 3f2a1c9 (HEAD -> main) Add login form
* 9b8d7e1 Initial commitgit log -p <file>Show every change ever made to one file.
git log --author="name"Show only commits by one person.
git log -S "text"Find commits that added or removed some text.
git diffShow unstaged changes.
git diff --stagedShow what's staged and about to be committed.
git diff <branch1>..<branch2>Compare two branches.
git show <commit>Show one commit: message, author and changes.
git blame <file>See who last changed each line of a file.
git shortlog -snCount commits per author.
git grep "text"Search tracked files for some text. Faster than plain grep.
git describe --tagsName the current commit relative to the nearest tag.
Every mistake has a fix. Pick the one that matches where you are.
git restore <file>Throw away unstaged changes to a file.
git restore --staged <file>Unstage a file but keep the edits.
git restore --source=<commit> <file>Bring back a file as it was in an older commit.
git reset --soft HEAD~1Undo the last commit, keep its changes staged. Full guide →
git reset HEAD~1Undo the last commit, keep its changes unstaged. Full guide →
git reset --hard HEAD~1Delete the last commit and its changes. Full guide →
git reset --hard origin/mainMake your branch exactly match the remote. Local work is lost. Full guide →
git revert <commit>Make a new commit that reverses an old one. Safe on shared branches.
git reflogList everywhere HEAD has been. Your rescue rope for 'lost' commits. Full guide →
git clean -nPreview which untracked files would be deleted.
git clean -fdDelete untracked files and folders.
Park unfinished work, mark releases, copy single commits.
git stashSave uncommitted changes and clean the working tree. Full guide →
git stash -uStash untracked files too. Full guide →
git stash push -m "note"Stash with a label so you can find it later.
git stash listShow every saved stash. Full guide →
git stash popBring back the latest stash and remove it from the list. Full guide →
git stash apply stash@{1}Apply a specific stash but keep it in the list.
git stash dropDelete the latest stash.
git tagList all tags.
git tag -a v1.0.0 -m "Release"Create an annotated release tag.
git push --tagsUpload your tags to the remote.
git cherry-pick <commit>Copy one commit from another branch onto this one.
Less common, but they save hours when you need them.
git bisect startStart a binary search to find the commit that introduced a bug.
$ git bisect start
$ git bisect bad
$ git bisect good v1.0.0git worktree add <path> <branch>Check out a second branch in another folder at the same time.
git submodule add <url>Include another repository inside this one.
git submodule update --init --recursiveDownload all submodules after cloning.
git sparse-checkout set <folder>Only check out part of a huge repo.
git archive -o app.zip HEADExport the current snapshot as a zip file, without .git.
git format-patch -1 <commit>Turn a commit into a patch file you can email.
git apply <file.patch>Apply a patch file to your working tree.
git rerereRemember how you fixed a conflict and reuse the fix next time.
git gcClean up and compress the repository database.
git fsckCheck the repository for corruption.
git maintenance startSchedule background tasks that keep big repos fast.
No command matches those filters.
Every Git command follows the same pattern:
git <command> [options] [arguments]
| Part | Example | What it does |
|---|---|---|
| Command | commit | The action Git should take. |
| Short option | -m | One dash, one letter. Can often be combined, like -am. |
| Long option | --amend | Two dashes, a full word. Easier to read in scripts. |
| Argument | "Fix typo", main, index.html | What the command acts on: a message, branch, file or commit. |
Commits can be referred to by their hash (3f2a1c9), a branch name (main), or relative to HEAD, your current position: HEAD~1 means "one commit before this one". That's why so many undo fixes use HEAD~1.
git --version; if Git isn't installed, macOS offers to install it.sudo apt install git.Run commands from inside your project folder. Git looks for the hidden .git folder to know which repository you mean. New to all this? Read what Git is and how it works first.
Git commands are instructions you type in a terminal to control Git, the version control system. Each one starts with git, followed by a command name such as commit or push, then options and arguments.
Git ships with roughly 150 commands, which you can list with git help -a. About 40 are everyday "porcelain" commands; the rest are low-level "plumbing" commands used by scripts and by Git itself.
git init, git clone, git status, git add, git commit, git push and git pull. Those seven cover creating a repository, saving changes and syncing them with a remote.
In a terminal. On macOS and Linux, use the built-in Terminal app. On Windows, use Git Bash (installed with Git for Windows), PowerShell or Command Prompt. Code editors like VS Code also have a built-in terminal.
Run git help <command> for the full manual page, or git <command> -h for a short summary of its options.
No. Git is the version control tool that runs on your computer. GitHub is a website that hosts Git repositories and adds features like pull requests. The same Git commands work with GitHub, GitLab and Bitbucket.