Online admissions open for the SEO, PHP web development and networking & cybersecurity courses. Enquire for the next batch.

Git Commands Cheat Sheet

Cheat sheet · 97 entries Updated By the SkillNewton team

This cheat sheet lists the Git commands you will use every day, grouped by task: setting up, committing, branching, merging, working with remotes, reading history and undoing mistakes. It uses the modern git switch and git restore commands and shows the older git checkout equivalents. Keep it open while you work, and read the warnings on the reset and force-push rows before you run them on shared code.

How to use this cheat sheet

  1. Set up Git once. Run the configuration commands in the first section so every commit carries your name and email address.
  2. Find the task, not the command. Sections are grouped by what you want to do, such as undoing a change or updating from a remote. Scan the section titles first.
  3. Check before you change. Run git status and git log --oneline before any command that rewrites history, so you know exactly where you are.
  4. Practise on a test repository. Create a throwaway repository with git init and try merging, rebasing and resetting there before using them on real work.

Setup and starting a repository

git config --global user.name "Your Name"Set the name recorded on your commits.
git config --global user.email "[email protected]"Set the email recorded on your commits. Use the one linked to your GitHub account.
git config --global init.defaultBranch mainName the first branch main in new repositories.
git config --global core.autocrlf trueWindows only: convert line endings to CRLF on checkout and back to LF on commit. Use input on macOS and Linux.
git config --list --show-originShow every setting and the file it comes from.
git initTurn the current folder into a new Git repository.
git clone https://github.com/example/repo.gitCopy a remote repository, with its full history, into a new folder.
git clone --depth 1 https://github.com/example/repo.gitShallow clone: only the latest commit. Faster for large projects you only need to read or build.
git statusShow the current branch and which files are modified, staged or untracked.
git status -sShort status, one line per file.
git help commitOpen the manual for any command (here commit).

Staging and committing

git add index.phpStage one file for the next commit.
git add .Stage all changes in the current folder and below, including new files.
git add -pReview changes piece by piece and choose which parts to stage.
git commit -m "Add contact form validation"Commit the staged changes with a message.
git commit -am "Fix typo in footer"Stage all modified tracked files and commit in one step. New, untracked files are not included.
git commit --amendChange the last commit (message or content). Rewrites the commit, so only use it before pushing.
git rm old-page.phpDelete a file and stage the deletion.
git rm --cached config.local.phpStop tracking a file but keep it on disk.
git mv about.html about-us.htmlRename or move a file and stage the change.

Branching

git branchList local branches. The current one is marked with *.
git branch -aList local and remote-tracking branches.
git branch -vvList branches with their last commit and the remote branch each one tracks.
git switch -c feature/loginCreate a new branch and switch to it.
git switch mainSwitch to an existing branch.
git switch -Switch back to the previous branch.
git checkout -b feature/loginOlder equivalent of git switch -c; still widely used.
git checkout mainOlder equivalent of git switch main.
git branch -m new-nameRename the current branch.
git branch -d feature/loginDelete a branch that has been merged.
git branch -D feature/loginForce-delete a branch even if it is not merged. Unmerged commits become hard to find.

Merging and rebasing

git merge feature/loginMerge the named branch into the current branch.
git merge --no-ff feature/loginAlways create a merge commit, even when a fast-forward is possible, so the feature stays visible in history.
git merge --abortStop a merge that has conflicts and go back to the state before it started.
git rebase mainReplay the current branch's commits on top of main. Rewrites those commits.
git rebase --continueCarry on after fixing a conflict during a rebase (stage the fixed files first).
git rebase --abortCancel the rebase and return to where you started.
git rebase -i HEAD~3Interactive rebase of the last three commits: reword, squash, reorder or drop them. Only for unpushed commits.
git cherry-pick a1b2c3dCopy a single commit from another branch onto the current branch.
git switch main
git merge feature/login
# CONFLICT (content): Merge conflict in index.php
# 1. Open index.php and edit the block between <<<<<<<, ======= and >>>>>>>
# 2. Stage the fixed file and finish the merge:
git add index.php
git commit

Remotes: fetch, pull and push

git remote -vList remotes and their URLs.
git remote add origin https://github.com/example/repo.gitConnect a local repository to a remote called origin.
git remote set-url origin [email protected]:example/repo.gitChange a remote URL, for example from HTTPS to SSH.
git fetch originDownload new commits and branches without changing your files.
git fetch --pruneFetch and remove remote-tracking branches that were deleted on the remote.
git pullFetch and merge the tracked remote branch into the current branch.
git pull --rebaseFetch and rebase your local commits on top, avoiding extra merge commits.
git push -u origin feature/loginPush a new branch and set it to track the remote, so plain git push works next time.
git pushSend your commits to the tracked remote branch.
git push --force-with-leaseOverwrite the remote branch after a rebase, but refuse if someone else pushed in the meantime. Never force-push main.
git push origin --delete feature/loginDelete a branch on the remote.

Inspecting history and changes

git log --oneline --graph --allCompact history of every branch, drawn as a graph.
git log -p index.phpHistory of one file, with the changes in each commit.
git log --author="Priya"Commits by one author.
git log -S "sendInvoice"Find commits that added or removed this text.
git diffUnstaged changes in your working folder.
git diff --stagedChanges that are staged and will go into the next commit.
git diff main...feature/loginWhat the feature branch changed since it split from main.
git show a1b2c3dShow one commit: message, author and changes.
git blame index.phpShow who last changed each line, and in which commit.
git blame -L 10,20 index.phpBlame only lines 10 to 20.
git reflogEvery position HEAD has been at recently. Use it to find commits after a bad reset or rebase.

Undoing changes (read the warnings)

git restore index.phpDiscard unstaged edits to a file. Warning: those edits are lost for good.
git restore --staged index.phpUnstage a file but keep your edits.
git restore --source=HEAD~1 index.phpBring back a file as it was one commit ago.
git revert a1b2c3dCreate a new commit that reverses an earlier one. The safe way to undo pushed commits.
git reset --soft HEAD~1Undo the last commit but keep its changes staged. Rewrites history: local commits only.
git reset HEAD~1Same as --mixed (the default): undo the last commit and keep its changes unstaged.
git reset --hard HEAD~1Undo the last commit and delete its changes and any uncommitted work. Warning: destructive; check git status first.
git reset --hard origin/mainMake the current branch match the remote exactly, discarding all local commits and edits. Warning: destructive.
git clean -nDry run: list untracked files that would be deleted.
git clean -fdDelete untracked files and folders. Warning: they are not in Git, so they cannot be recovered.
# Which undo do I need?
git restore index.php            # throw away unstaged edits to a file
git restore --staged index.php   # unstage, keep the edits
git commit --amend               # fix the last commit (not yet pushed)
git revert a1b2c3d               # undo a pushed commit with a new commit
git reset --soft HEAD~1          # undo last commit, keep changes staged
git reset --hard HEAD~1          # undo last commit AND delete its changes
git reflog                       # find a "lost" commit after a bad reset

Stash: put work aside

git stash push -m "half-done header"Save uncommitted changes to tracked files and clean the working folder.
git stash -uStash untracked files as well.
git stash listList saved stashes, newest first as stash@{0}.
git stash show -p stash@{0}Show the changes inside a stash.
git stash apply stash@{0}Reapply a stash and keep it in the list.
git stash popReapply the latest stash and remove it from the list (it is kept if there is a conflict).
git stash drop stash@{0}Delete one stash.
git stash branch fix/header stash@{0}Create a branch from the commit where the stash was made and apply the stash there.

Tags and releases

git tagList tags.
git tag -a v1.2.0 -m "Release 1.2.0"Create an annotated tag (stores author, date and message). Use these for releases.
git tag v1.2.0Create a lightweight tag: just a name pointing at a commit.
git tag -a v1.1.0 9fceb02 -m "Release 1.1.0"Tag an older commit.
git show v1.2.0Show the tag details and the commit it points to.
git push origin v1.2.0Push one tag. Tags are not pushed by plain git push.
git push origin --tagsPush all local tags.
git tag -d v1.2.0Delete a local tag.
git push origin --delete v1.2.0Delete a tag on the remote.
git switch --detach v1.2.0Look at the code as it was at a tag (detached HEAD). Create a branch before making commits.

.gitignore

*.logIgnore every file ending in .log, in any folder.
/vendor/Ignore the vendor folder at the repository root only.
node_modules/Ignore any folder called node_modules, at any depth.
.env*Ignore .env, .env.local and similar files that hold secrets.
!.env.exampleException: keep this file tracked. The ! rule must come after the rule it overrides.
git check-ignore -v storage/app.logShow which .gitignore rule is ignoring a file.
git rm --cached .envStop tracking a file that was committed before it was ignored. It stays in old commits, so change any exposed passwords or keys.
git config --global core.excludesFile ~/.gitignore_globalUse a personal ignore file for every repository (good for editor and OS files).
# .gitignore for a typical PHP project
/vendor/
node_modules/
.env*
!.env.example
*.log
/writable/cache/
.DS_Store
Thumbs.db
.idea/
.vscode/

Frequently asked questions

What is the difference between git switch, git restore and git checkout?

git checkout does two jobs: changing branches and restoring files. Git 2.23 split these into git switch for branches and git restore for files, which are clearer and harder to misuse. Checkout still works, so you will see it in older tutorials.

Should I merge or rebase?

Merge keeps the full history and is always safe on shared branches. Rebase gives a cleaner, linear history but rewrites commits, so only rebase commits that you have not pushed or that nobody else has based work on.

How do I undo a commit that I have already pushed?

Use git revert <commit>. It creates a new commit that reverses the old one without rewriting history, so teammates are not affected. Avoid git reset on pushed commits.

Is Git the same as GitHub?

No. Git is the version control tool that runs on your computer. GitHub, GitLab and Bitbucket are hosting services for Git repositories that add pull requests, issues and access control.

Want to Learn This Properly?

The PHP Web Development: Foundation & Practical Implementation (Online) turns resources like this into practised skills, with projects on real websites.

WhatsApp Call Us Now

Free counselling

Send an Enquiry

Tell us which course you are interested in. We reply by email or phone with the next batch and how to enrol.

We reply by email or phone within working hours. No spam.