Git Playground
Freely experiment with Git commands and learn from the cheat sheet
Git Cheat Sheet
Basics
Branches
Commit History
Remote Repositories
Undoing Changes
Advanced Commands
Basics
git init
git init
This command creates a new Git repository in your current directory. It creates a hidden .git directory that contains all Git metadata.
git status
git status
This lets you see the current status of your repository - which files have been changed, which are staged, etc.
git add <file> or git add .
git add index.html
With this command, you mark changes for the next commit. Use 'git add .' to mark all changes in the current directory.
git commit -m "<message>"
git commit -m "Fix bug in login form"
Creates a new commit with all staged changes. The message should be a short, precise description of what was changed.
git config [--global] <key> <value>
git config --global user.name "Your Name"
Sets configuration values for your user name, email, editor, and other preferences. Use --global to apply settings to all repositories.
git help <command>
git help commit
Shows detailed help information for any Git command. You can also use 'git <command> --help' for the same information.
Branches
git branch [name] [--delete]
git branch feature-login
Without parameters, this command lists all existing branches. With a name, it creates a new branch (but doesn't switch to it).
git checkout <branch> or git checkout -b <new-branch>
git checkout -b feature-login
Switches to another branch. With '-b', it creates a new branch and immediately switches to it.
git merge <branch>
git merge feature-login
Integrates changes from the specified branch into the current branch. This creates a merge commit if it's not a fast-forward situation.
git switch <branch> or git switch -c <new-branch>
git switch main
Modern alternative to 'git checkout' for switching branches. Use '-c' to create and switch to a new branch in one command.
git branch -d <branch>
git branch -d feature-login
Deletes the specified branch if it has been fully merged. Use '-D' instead of '-d' to force deletion even if not merged.
Commit History
git log [options]
git log --oneline --graph
Shows the commit history with details like author, date, and message. Many options available to customize the output format.
git diff [<commit>] [<commit>]
git diff HEAD~1 HEAD
Shows the differences between two commits, commit and working tree, etc. Without arguments, shows changes in working directory that aren't staged.
git show [<commit>]
git show HEAD
Shows information about a git object. For commits, shows the commit message and the differences it introduced.
git blame <file>
git blame index.html
Shows who made each change to a file, line by line, and in which commit. Useful for understanding the history of a specific file.
Remote Repositories
git clone <url>
git clone https://github.com/user/repo.git
Creates a local copy of a remote repository, including all branches and history.
git pull [remote] [branch]
git pull origin main
Combines 'git fetch' and 'git merge' to fetch changes from a remote branch and integrate them into your current branch.
git push [remote] [branch]
git push origin main
Sends your local commits to a remote repository. Others can then see and fetch your changes.
git remote add <name> <url>
git remote add origin https://github.com/user/repo.git
Lists, adds, or removes remote repositories. Use 'git remote -v' to see the URLs of your remotes.
git fetch [remote]
git fetch origin
Downloads all branches and commits from a remote repository without merging them into your local branches.
Undoing Changes
git restore <file> or git restore --staged <file>
git restore --staged index.html
Undoes changes to your working tree (with no options) or removes files from the staging area (with --staged).
git reset [--soft | --mixed | --hard] [commit]
git reset --hard HEAD~1
Resets your branch to a specific commit. --soft keeps changes in staging area, --mixed (default) unstages them, --hard discards all changes.
git revert <commit>
git revert HEAD
Creates a new commit that undoes the changes made by an earlier commit. Safer than reset for shared branches.
Advanced Commands
git rebase <base>
git rebase main
Transfers your changes onto the latest version of the base branch. This creates a cleaner history than merges.
git stash [pop]
git stash
Saves your uncommitted changes temporarily, allowing you to return to a clean working directory. Use 'pop' to reapply the stashed changes.
git tag [name] [commit]
git tag v1.0.0
Tags are references to specific points in Git history, typically used for marking version releases. Use with -a for annotated tags.
git cherry-pick <commit>
git cherry-pick abc123
Applies the changes from specific commits to your current branch. Useful for selectively bringing changes from one branch to another.
git bisect <subcommand>
git bisect start
Helps find which commit introduced a bug using binary search. Mark commits as 'good' or 'bad' to narrow down the problem.