50 Git commands Cheatsheet
The Git commands you will use every day, grouped by what they do. Copy any command with one click, and see where it moves your files.
git statusShows which files you changed, which are staged and which Git is not tracking yet. It never changes anything.
Before every add and commit, so you know exactly what you are about to save.
Run it often. It is the safest command in Git.
git add .Puts every change in the current folder into the staging area, ready for the next commit.
Everything you changed belongs in the same commit.
Want only one file? Use git add <file> instead.
git commit -am "<message>"Stages changes to files Git already tracks and saves them as one commit with your message, in a single step.
Quick saves on files you have committed before.
Brand new files are skipped. Run git add on them first.
git push -u origin mainSends the commits on your local main branch to GitHub. The -u flag links main to origin/main.
You want your commits on GitHub. Add -u on the first push only.
After the first push, plain git push is enough.
git clone <url>Downloads a full copy of a repository, with its whole history, into a new folder on your computer.
You start working on a project that already exists on GitHub.
Copy the URL from the green Code button on the repository page.
git initCreates a hidden .git folder, so Git starts tracking this folder as a new repository.
Once, at the start of a brand new project.
Cloned projects already have a .git folder. Do not run it again there.
git log --onelineLists your commits, newest first, with a short ID and the message on each line.
You need a commit ID or want a quick look at recent history.
If the list is long, press q to leave it.
git branchLists your local branches. The one marked with * is the branch you are on.
Before you switch to or create a branch.
Add -a to see remote branches too.
git checkout <branch-name>Switches to another branch and updates the files in your working folder to match it.
You want to work on, or look at, a different branch.
Commit or stash your changes first. git switch <branch-name> does the same job.
git diffShows, line by line, what you changed in your files but have not staged yet.
Before git add, to review your own changes.
Already staged? Use git diff --staged.
Understanding Git: Adding the Files to Git
Git moves your changes through four places. Most commands just move files from one place to the next. Click a place to see the commands that work there.
All Git commands
Pick any command on the left to see what it does, where it moves your files and when to use it. The copy icon copies it straight away. Advanced commands sit at the end of the list.
git initCreates a hidden .git folder, so Git starts tracking this folder as a new repository.
Once, at the start of a brand new project.
Cloned projects already have a .git folder. Do not run it again there.
git add .Puts every change in the current folder into the staging area, ready for the next commit.
Everything you changed belongs in the same commit.
Want only one file? Use git add <file> instead.
git add <file>Puts the changes from one file into the staging area. Other changed files stay where they are.
You want to commit some of your changes, not all of them.
Stage several files at once: git add a.txt b.txt
git add -AStages every change in the whole repository: new, edited and deleted files, even outside the folder you are in.
You want everything in the next commit.
git add . covers only the current folder. -A covers the whole repository.
git clone <url>Downloads a full copy of a repository, with its whole history, into a new folder on your computer.
You start working on a project that already exists on GitHub.
Copy the URL from the green Code button on the repository page.
git commitSaves everything in the staging area as a new commit. Git opens your text editor so you can write the message.
You staged your changes and want to write a longer message.
Add -m "your message" to skip the editor.
git commit -aStages every change to files Git already tracks and commits it. Git opens your editor for the message.
Quick commits when you have not created any new files.
New, untracked files are not included. Run git add on them first.
git commit -am "<message>"Stages changes to files Git already tracks and saves them as one commit with your message, in a single step.
Quick saves on files you have committed before.
Brand new files are skipped. Run git add on them first.
git commit --amend -m "<message>"Replaces your last commit with a new one that has this message, plus anything you have staged.
You spotted a typo in the last message or forgot to add a file.
Only amend commits you have not pushed. It changes the commit ID.
git logLists every commit on the current branch, newest first, with the author, date and full message.
You want the full history of the branch.
Scroll with the arrow keys. Press q to leave.
git log --summaryShows the commit history plus a note of the files each commit created, deleted or renamed.
You want to find when a file was added or removed.
For the number of changed lines per file, try git log --stat.
git log --onelineLists your commits, newest first, with a short ID and the message on each line.
You need a commit ID or want a quick look at recent history.
If the list is long, press q to leave it.
git statusShows which files you changed, which are staged and which Git is not tracking yet. It never changes anything.
Before every add and commit, so you know exactly what you are about to save.
Run it often. It is the safest command in Git.
git showShows your latest commit: its message, author and the exact lines it changed.
You want to see what one commit did.
See an older commit with git show <commit-sha>.
git diffShows, line by line, what you changed in your files but have not staged yet.
Before git add, to review your own changes.
Already staged? Use git diff --staged.
git diff --color-wordsWorks like git diff, but highlights only the words that changed instead of whole lines.
Reviewing small edits in long lines, such as docs or Markdown.
Handy for blog posts and README files.
git diff --stagedShows the changes you have staged, which is exactly what your next commit will contain.
Right before git commit, as a final check.
git diff --cached does the same thing.
git mv <filename> <new-filename>Renames the file in your folder and stages the rename, in one step.
You want to rename a file Git already tracks.
It moves files too: git mv a.txt docs/a.txt
git rm <filename>Deletes the file from your folder and stages the deletion for the next commit.
A tracked file is no longer needed.
Want to keep the file on disk? Use git rm --cached <filename>.
git branchLists your local branches. The one marked with * is the branch you are on.
Before you switch to or create a branch.
Add -a to see remote branches too.
git branch <name>Creates a new branch that points at your current commit. You stay on the branch you are on.
You want to start new work without touching main.
git switch -c <name> creates it and switches to it in one step.
git branch -d <branch-name>Deletes a local branch. Git refuses if the branch has commits you have not merged yet.
A branch is merged and you are done with it.
-D forces the delete. Use it only if you are sure.
git checkout <branch-name>Switches to another branch and updates the files in your working folder to match it.
You want to work on, or look at, a different branch.
Commit or stash your changes first. git switch <branch-name> does the same job.
git switch <branch-name>Switches to another branch and updates your files to match. It only handles branches, so it is harder to misuse than checkout.
Everyday branch switching on Git 2.23 or newer.
git switch - jumps back to the branch you were on before.
git merge <branch-name>Brings the commits from another branch into the branch you are on.
Your feature branch is ready to join main.
Switch to the branch that should receive the changes first.
git merge --abortStops a merge that hit conflicts and puts your branch and files back the way they were before it.
A merge goes wrong and you want to start over.
Works only while the merge is still in progress.
git checkout -- <file>Throws away your unstaged changes to one file and restores its last staged or committed version.
You broke a file and want it back as it was.
Newer Git: git restore <file> does the same.
git checkout -- .Throws away every unstaged change in the current folder.
You want a clean slate and do not need your edits.
There is no undo. Run git stash first if you are unsure.
git reset HEAD <file>Takes a file out of the staging area. Your edits stay in the file.
You staged a file by mistake.
Newer Git: git restore --staged <file> does the same.
git revert <commit-sha>Makes a new commit that does the opposite of an earlier one. History is kept and nothing is deleted.
Undoing a commit that is already pushed and shared.
Safe on shared branches, unlike reset.
git clean -nLists the untracked files that git clean -f would delete. It deletes nothing.
Every time, before you run git clean -f.
Add -d to include untracked folders.
git clean -fPermanently deletes untracked files from your folder. They do not go to the bin.
Build output or junk files are cluttering your folder.
Run git clean -n first to preview.
git remote add origin <url>Saves the GitHub URL under the name origin, so your local repository knows where to push.
Once, after git init and before your first push.
Check it with git remote -v.
git branch -M mainRenames your current branch to main, even if a branch called main already exists.
Your repository started on master and you want main, like GitHub.
-m does the same but refuses to overwrite an existing branch.
git push -u origin mainSends the commits on your local main branch to GitHub. The -u flag links main to origin/main.
You want your commits on GitHub. Add -u on the first push only.
After the first push, plain git push is enough.
git fetchDownloads new commits from GitHub into your local repository, without touching your files or branches.
You want to see what others pushed before you merge it.
Then compare with git log HEAD..origin/main
git pullFetches new commits and merges them into your current branch, which updates your files.
At the start of the day, and before you push.
Commit or stash your local changes first to avoid conflicts.
git stashSaves your uncommitted changes in a hidden stash and resets your folder to the last commit.
You need to switch branches but are not ready to commit.
Add -u to stash untracked files too.
git stash listLists every stash you have saved, newest first, as stash@{0}, stash@{1} and so on.
You have stashed more than once and lost track.
See what is inside one: git stash show -p stash@{0}
git stash popPuts your latest stashed changes back into your folder and removes them from the stash list.
You are back on the branch and ready to carry on.
git stash apply restores the changes but keeps the stash, just in case.
git rebase <branch-name>Takes your commits and replays them on top of another branch, so history reads as one straight line.
Updating your feature branch with the latest main before a pull request.
Never rebase commits that other people have already pulled.
git rebase -i HEAD~<n>Opens a list of your last n commits where you can squash, reword, reorder or drop them.
Tidying up messy commits before you push.
Change pick to squash to fold a commit into the one above it.
git cherry-pick <commit-sha>Takes one commit from any branch and applies it as a new commit on the branch you are on.
You need one fix from another branch, not the whole branch.
Get the commit ID from git log --oneline.
git reset --soft HEAD~1Removes your last commit but keeps all of its changes staged, ready to commit again.
You committed too early or with the wrong message.
Use it only on commits you have not pushed.
git reset --hard <commit-sha>Moves your branch back to that commit and wipes every later commit and every uncommitted change.
You are certain you want to throw work away.
Lost something? git reflog can usually find it.
git reflogLists every commit HEAD has pointed to, including commits no branch shows any more.
After a bad reset or rebase, to find a lost commit.
Rescue a lost commit with git branch <name> <commit-sha>.
git push --force-with-leaseReplaces the remote branch with your local one, but stops if someone else pushed since your last fetch.
After a rebase or amend on a branch only you work on.
Safer than --force. Never force push to main.
git tag -a <tag> -m "<message>"Adds a named label with a message to the current commit, usually a release such as v1.0.0.
You ship a release.
Tags are not pushed by default. Use git push origin <tag>.
git blame <file>Shows each line of a file next to the commit, author and date that last changed it.
You want to know when or why a line changed.
Every file on GitHub has a Blame button too.
git bisect startStarts a binary search. You mark commits as good or bad and Git narrows down the one that added the bug.
Something broke and you do not know which commit did it.
Finish with git bisect reset to return to your branch.
<orange> Your own value<file> Replace the part in orange with your own value. ⚠ Careful Can delete work. Read before running.
Watch the video
Conclusion
Bookmark this page and come back whenever you need a command. In the next lesson you will create your first GitHub repository.
UP NEXTCreate GitHub Repo (GUI)Your vote helps us improve our docs.
All our docs are open source. Found a typo, want to add detail or have a better explanation? Anyone can contribute.
Comments
Powered by GitHub Discussions