Skip to content
AkOps Labs
Playgrounds

Git

Save your work, travel back in time, and collaborate like a pro.

14 levels · 75 lessons · about 2 hours

Git commands cheat sheet

Every command in the Git Playground, grouped by level. Practise each one above in the interactive terminal.

Show all 75 commands

Level 1: Your First Repo (Beginner)

Start a repository and make your first commit.

Start a repository
git init

git init turns the current folder into a Git repository by creating a hidden .git folder that stores all history.

Check the status
git status

git status shows which files are new, changed or ready to commit. Run it all the time.

Stage files
git add .

Before committing, you stage changes with git add. A dot (.) means everything in this folder.

Commit
git commit -m "first commit"

A commit is a saved snapshot with a message. -m lets you write the message inline.

See the history
git log --oneline

git log lists commits, newest first. --oneline gives one short line per commit.

Level 2: Branches (Beginner)

Work on features safely without breaking main.

Create & switch branch
git switch -c feature/login

A branch is a separate line of work. git switch -c creates a branch and moves to it.

List branches
git branch

git branch lists your branches. The * marks the one you're on.

See what changed
git diff

git diff shows line-by-line changes that are not staged yet.

Go back to main
git switch main

git switch BRANCH moves you to an existing branch.

Merge a branch
git merge feature/login

git merge brings another branch's commits into the branch you're on.

Level 3: Teamwork with Remotes (Intermediate)

Push to GitHub and stay in sync with your team.

Clone a repo
git clone https://github.com/akopslabs/demo.git

git clone downloads a full copy of a remote repository, history included.

Connect a remote
git remote add origin https://github.com/you/app.git

A remote is a named link to a repo on GitHub/GitLab. By convention the main one is called origin.

Push your branch
git push -u origin main

git push uploads your commits. -u remembers the link so next time plain git push works.

Get teammates' changes
git pull

git pull downloads new commits from the remote and merges them into your branch.

Fetch without merging
git fetch

git fetch downloads remote changes but doesn't touch your files - safe for a quick look.

Level 4: Undo & Fix Mistakes (Intermediate)

Recover from mistakes without panic.

Unstage a file
git restore --staged config.js

git restore --staged takes a file out of the staging area but keeps your edits.

Discard local changes
git restore config.js

git restore FILE throws away uncommitted edits to that file. They are gone for good.

Park work for later
git stash

git stash saves your uncommitted changes and cleans the folder, so you can switch tasks.

Fix the last commit message
git commit --amend -m "add login page"

git commit --amend replaces the last commit. Only do this before you push.

Safely undo a pushed commit
git revert 7a8b9c0

git revert creates a new commit that reverses an old one. It's safe on shared branches because history isn't rewritten.

Level 5: Pro Git Workflow (Intermediate)

Keep history clean, ship releases and rescue lost work.

See the whole picture
git log --oneline --graph --all

--graph draws branches as lines and --all includes every branch, not only the current one.

Rebase on main
git rebase main

rebase replays your branch's commits on top of the latest main, so history stays a straight line.

Copy one commit
git cherry-pick 4f2e1a9

cherry-pick copies a single commit from another branch onto the current one, e.g. a hotfix.

Tag a release
git tag -a v1.0.0 -m "first release"

A tag is a permanent name for a commit, like v1.0.0. -a makes an annotated tag with a message.

Push the tag
git push origin v1.0.0

Tags are not pushed with normal git push. Push them by name (or all with --tags).

Rescue lost work
git reflog

reflog lists everywhere HEAD has been, even after a bad reset. Find the commit, then git reset --hard to it.

Level 6: Pull Requests (Advanced)

Open, review and merge pull requests from the terminal with the GitHub CLI.

Open a pull request
gh pr create --fill

In a team you never push straight to main. You push a branch and open a pull request (PR) so others can review it. gh is GitHub's command line tool. --fill uses your commit messages as the title and description.

List open pull requests
gh pr list

gh pr list shows the PRs waiting in the repository.

Test someone's PR locally
gh pr checkout 39

To review properly, run the code. gh pr checkout downloads the PR's branch and switches to it.

Approve a PR
gh pr review 39 --approve

After testing, you approve so it can be merged. Branch protection usually needs at least one approval.

Squash and merge
gh pr merge 39 --squash

--squash combines all the PR's commits into one clean commit on main. Many teams use it to keep history readable.

Level 7: History Detective (Advanced)

Find who changed what, when and why, and track down the commit that broke something.

Who wrote this line?
git blame app.js

git blame shows, for every line of a file, the last commit and person that changed it. Useful to find context, not to blame people.

Look inside a commit
git show 4f2e1a9

git show displays one commit: its message, author and the exact lines it changed.

History of one file
git log -p app.js

git log -p FILE shows every commit that touched a file, with the changes.

Search commit messages
git log --oneline --grep=login

--grep finds commits whose message contains a word. Great for finding when a fix went in.

Find the commit that broke it
git bisect start

git bisect does a binary search through history. You mark a bad commit and a good one, Git checks out the middle, and you test until it finds the guilty commit.

Mark the bad commit
git bisect bad

Tell bisect the current commit has the bug. Then mark a good one and it starts searching.

Level 8: Clean History & Safe Pushes (Advanced)

Tidy your commits before a review and push safely when history changes.

Undo the last commit, keep the work
git reset --soft HEAD~1

git reset --soft HEAD~1 removes the last commit but keeps all its changes staged. Use it when you committed too early. Only on commits you have not pushed.

Tidy several commits
git rebase -i HEAD~3

An interactive rebase lets you squash, reword or reorder your last commits before you open a PR. HEAD~3 means the last three commits.

Push rewritten history safely
git push --force-with-lease

After a rebase, a normal push is refused. --force-with-lease only overwrites the remote if nobody else pushed in the meantime. Never use plain --force on shared branches.

Preview a clean-up
git clean -nd

git clean deletes untracked files. -n only shows what would be deleted, so you can check first.

Work on two branches at once
git worktree add ../hotfix -b hotfix/login

A worktree is a second folder linked to the same repo, on a different branch. Fix a production bug without stashing your feature work.

Level 9: Merge Conflicts (Advanced)

Resolve a merge conflict calmly: find it, choose the right code and finish the merge.

Hit a conflict
git merge feature/pay

A conflict happens when two branches changed the same lines. Git stops and asks you to choose.

See what's conflicted
git status

git status lists the files that still need fixing under 'Unmerged paths'.

List only conflicted files
git diff --name-only --diff-filter=U

--diff-filter=U shows just the unmerged files. Useful when a merge touches many files.

Keep their version
git checkout --theirs config.js

Inside the file Git marks both versions with <<<<<<< and >>>>>>>. If one side is simply right, --theirs (or --ours) takes that whole version.

Mark it resolved
git add config.js

Adding the file tells Git the conflict is fixed.

Finish the merge
git commit --no-edit

--no-edit keeps Git's default merge message.

Level 10: Stash & Partial Commits (Advanced)

Park unfinished work and commit only the changes that belong together.

Stash with a name
git stash push -m "wip login"

Name your stashes, so a week later you still know what they are.

List your stashes
git stash list

Stashes are a stack: the newest is stash@{0}.

Look inside a stash
git stash show -p stash@{0}

show -p shows the full diff saved in a stash, before you apply it.

Bring it back
git stash pop

pop applies the newest stash and removes it from the list.

Stage part of a file
git add -p

add -p shows each change (hunk) and asks y/n. You commit only the related lines, and keep commits small and clean.

Delete an old stash
git stash drop stash@{0}

Clean up stashes you no longer need.

Level 11: Config & .gitignore (Expert)

Set up Git properly on a new machine and keep secrets and junk out of the repo.

Set your name
git config --global user.name "Alex Johnson"

Every commit records a name and email. --global sets it for all your repos.

Set your email
git config --global user.email alex.johnson@email.com

Use the same email as your GitHub account, so commits link to your profile.

Default branch main
git config --global init.defaultBranch main

New repos then start on main instead of master.

Ignore node_modules
echo "node_modules/" >> .gitignore

.gitignore lists files Git must never track: dependencies, build output, secrets.

Why is it ignored?
git check-ignore -v .env

check-ignore -v tells you which line in which .gitignore ignores a file.

Untrack a committed file
git rm --cached .env

If .env was committed before it was ignored, --cached removes it from Git but keeps it on your disk.

Level 12: Releases & Changelogs (Expert)

Tag versions, see what changed between releases and publish a release.

List the tags
git tag

Tags mark release points like v1.0.0.

Where are we?
git describe --tags

describe names the current commit relative to the last tag: v1.0.1-4-g7c2d9e1 means 4 commits after v1.0.1.

What changed since v1.0.1?
git log v1.0.1..HEAD --oneline

A range A..HEAD shows the commits that are in HEAD but not in A: your changelog.

Who contributed?
git shortlog -sn

shortlog -sn counts commits per author.

Publish a release
gh release create v1.1.0 --generate-notes

gh release creates a GitHub release from a tag. --generate-notes writes the notes from merged PRs.

Level 13: Branch Hygiene (Expert)

Keep a repo tidy: find merged branches, delete them locally and remotely, and prune.

See all branches
git branch -a

-a shows local branches and the remote ones (remotes/origin/...).

Which are merged?
git branch --merged main

--merged main lists branches whose work is already in main: safe to delete.

Delete a local branch
git branch -d feature/login

-d only deletes if the branch is merged. -D forces it.

Delete it on GitHub
git push origin --delete feature/login

Push with --delete removes the branch from the remote.

Prune old remote branches
git fetch --prune

--prune removes your local copies of branches that were deleted on the remote.

Level 14: Secrets & Signed Commits (Expert)

Find leaked secrets, remove them from history and sign your commits.

Search history for a key
git log -S "AKIA" --oneline

-S finds commits that added or removed a string. AKIA is how AWS access keys start.

Scan the repo for secrets
gitleaks detect --source .

gitleaks scans files and history for passwords, tokens and keys. Teams run it in CI on every push.

Remove a file from all history
git filter-repo --path config/aws.env --invert-paths

git filter-repo rewrites history without the file. Then rotate the key anyway: once pushed, assume it's leaked.

Sign a commit
git commit -S -m "signed release"

-S signs the commit with your GPG or SSH key, so GitHub shows it as Verified.

Check the signature
git log --show-signature -1

--show-signature verifies who signed the commit.