Git tutorial
Git Cheat Sheet: Git commands explained simply
57 entries to learn Git or look up a command: what it does, a use case, its command-line variants, frequently asked questions and the steps to follow in Merathon. Warnings point out the actions that lose data or rewrite history.
To search by keyword, use the cheat sheet search on the home page.
The most common questions
- Undo the last commit without losing your changes
- Undo a commit already pushed
- Change the last commit's message
- Discard a file's changes
- Delete a local and remote branch
- Rename a branch
- Resolve a merge conflict
- Combine several commits into one
- Force a push without overwriting others' work
- Set up an SSH key for GitHub or GitLab
Getting started
- Create a repository
git initTurns a folder into a git repository: the history is stored in its .git subfolder. - Clone a repository
git clone <url>Downloads a remote repository with its whole history and links it to its "origin" remote. - Open an existing repository
cd <folder>A repository already on disk opens as is, in the current tab or a new tab.
Changes
- See the state of the working copy
git statusLists modified, added, deleted or untracked files, and those already staged for the next commit. - See a file's differences
git diff · git diff --stagedShows line by line what changed, compared to the index (unstaged) or the last commit (staged). - Stage files
git add <file> · git add -APlaces changes in the index: only what is staged will be part of the next commit. - Stage part of a file
git add -pStages only certain hunks or lines of a file, the rest staying unstaged. - Unstage
git restore --staged <file>Removes changes from the index without losing them: they become "unstaged" again. - Discard changes
git restore <file>Puts a file back in the state of the index: unstaged changes are discarded. - Ignore files
echo <pattern> >> .gitignoreFiles matching a .gitignore pattern no longer appear as untracked. - Commit
git commit -m <message>Records the content of the index as a new commit on the current branch. - Amend the last commit
git commit --amendReplaces the last commit with a new one: corrected message, files added or removed.
Branches
- Create a branch
git switch -c <branch>Creates a new line of development from a commit, without touching other branches. - Switch branches
git switch <branch>Puts the working copy in the state of another branch, which becomes the current branch. - Rename a branch
git branch -m <old> <new>Changes the name of a local branch without changing its commits. - Delete a branch
git branch -D <branch> · git push origin --delete <branch>Removes a local or remote branch; commits already merged elsewhere stay in the history. - Link a branch to a remote branch
git branch --set-upstream-to=origin/<branch>Sets the tracked remote branch: it is the one targeted by pull and push, and by the ahead / behind counter. - Go to an old commit
git switch --detach <commit>Puts the working copy in the state of a specific commit, without a branch ("detached HEAD").
Synchronize
- Add a remote
git remote add <name> <url>Declares a remote repository to exchange commits with. - Have one identity per folder
git config --global includeIf."gitdir:~/dev/pro/".path ~/.gitconfig-proAn includeIf rule applies a configuration file to all the repositories in a folder: same commit email and same SSH key (core.sshCommand) without setting anything repository by repository. For Merathon as for git on the command line, a repository's own setting takes precedence over the rule. - Set up an SSH key
ssh-keygen -t ed25519An SSH key replaces the token for repositories whose remote uses SSH (git@host:…). The private key stays on your machine; only the public key is registered on GitHub or GitLab. On first connection, the server fingerprint is checked then stored in ~/.ssh/known_hosts. - Get new commits without integrating them
git fetchDownloads the commits and branches of the remote without changing your branch or your files. - Integrate new commits from the remote
git pull · git pull --rebaseGets the remote commits and integrates them into the current branch, by merge or by rebase. - Publish your commits
git push · git push -u origin <branch>Sends the commits of the current branch to the remote; on the first push, the branch is created there. - Force a push
git push --forceReplaces the remote branch with your local version, even if their histories have diverged.
Integrate
- Merge a branch
git merge <branch>Integrates the commits of another branch into the current branch, with a merge commit if needed. - Rebase a branch
git rebase <branch>Replays the commits of the current branch on top of another, for a linear history. - Resolve a conflict
git add <file> (after editing)When two branches change the same lines, git stops and asks you which version to keep. - Abort a merge or a rebase
git merge --abort · git rebase --abortCancels the current operation and puts the branch back in its previous state. - Take a commit from another branch
git cherry-pick <commit>Copies the changes of a specific commit onto the current branch. - Undo a published commit
git revert <commit>Creates a new commit that undoes the changes of an old one, without rewriting history. - Open a pull request
gh pr create · glab mr createProposes the commits of a branch for review on GitHub (pull request) or GitLab (merge request). - Link an issue to a pull request
#12 · Closes #12 (in the description)#12 in the description creates a link to the issue; "Closes #12" also closes it on merge, on GitHub and GitLab alike.
Rewrite history
- Interactive rebase
git rebase -i <commit>Reorganizes a series of commits: reorder, rename, squash, edit or delete. - Group commits
git rebase -i (squash)Merges several commits into one, in place of the oldest. - Go back to an earlier commit
git reset --soft | --mixed | --hard <commit>Moves the current branch to another commit. Soft keeps everything staged, mixed keeps modified files, hard erases everything.
Set aside
- Set your changes aside
git stash push -u -m <message>Stores the current changes in a reserve and cleans up the working copy. - Resume a stash
git stash pop · git stash applyReapplies changes set aside; "pop" also removes them from the reserve. - Delete a stash
git stash dropRemoves a stash from the reserve without applying it.
Tags
- Create a tag
git tag -a <name> -m <message>Places a fixed label on a commit, usually to mark a version. - Publish a tag
git push origin <tag>Tags do not go along with an ordinary push: they are pushed explicitly. - Delete a tag
git tag -d <tag> · git push origin --delete <tag>Removes a tag locally, and separately on the remote.
Explore
- Browse history
git log --graph --allShows the commits and how branches split and join. - See the content of a commit
git show <commit>Shows the message, the author and the files changed by a commit. - See the commands runEach action in the application is logged with the equivalent git command.
- Everything else, on the command line
git blame · git bisect · git reflog · git clean …Commands without a dedicated button stay available in the integrated terminal, opened in the repository.
Worktrees
- Understand worktrees
git worktree listA worktree is an additional working copy of the same repository, in another folder and on another branch. They all share commits, branches, tags, remotes and stashes: a commit made in one is immediately visible in the others, without push or fetch. Each one keeps its own files, index and current branch. The original folder is the main worktree; in the others, .git is a simple file pointing to the main repository, with no second history. - Create a worktree on a new branch
git worktree add -b <branch> <folder>Creates the branch from the current commit, then checks it out in a new folder. - Check out an existing branch in a worktree
git worktree add <folder> <branch>Checks out an already created local branch in a new folder, without changing branch where you are. - Switch between worktrees
cd <folder>Each worktree is a separate folder: going there is enough, nothing is rewritten on disk, unlike a branch switch. - Find a branch's worktree
git worktree listA branch can only be checked out in one worktree at a time: otherwise, a commit in one would move the branch under the other's files. Elsewhere, it can therefore be neither checked out, nor advanced, nor deleted. - Delete a worktree
git worktree remove <folder>Deletes the folder and its reference in the repository. The branch and its commits remain: you can reopen it later, or delete it separately. - Clean up a missing worktree
git worktree pruneIf the folder was deleted or moved outside git, the repository keeps its reference and its branch is still considered checked out. This reference must be removed. - Move or lock a worktree
git worktree move <folder> <new-folder> ; git worktree lock <folder>move moves the folder while keeping the link with the repository. lock protects it from deletion and cleanup, for example if it is on an external drive; git worktree unlock removes the protection.
Submodules and LFS
- Initialize and update submodules
git submodule update --initGets the nested repositories at the commit recorded by the parent repository. - Track large files with Git LFS
git lfs track <pattern>Stores large files outside the history, which keeps only a pointer. - Fetch LFS files
git lfs pullDownloads the real content of the LFS files of the current revision.