No Login Data Private Local Save

Git Cheatsheet - Online Common Commands Reference

18
0
0
0

Git Cheatsheet

Quick reference for the most essential Git commands โ€” search, copy & go

50 commands All categories
Setup & Config
git config --global user.name "[name]"

Set the author name for all commits on this machine.

Example git config --global user.name "John Doe"
Setup & Config
git config --global user.email "[email]"

Set the email address for all commits on this machine.

Example git config --global user.email "john@example.com"
Setup & Config
git init

Initialize a new Git repository in the current directory.

Example cd my-project && git init
Setup & Config
git clone [url]

Clone a remote repository to your local machine.

Example git clone https://github.com/user/repo.git
Setup & Config
git config --list

List all Git configuration settings for the current repository.

Example git config --list --show-origin
Basic
git add [file]

Stage a specific file for the next commit.

Example git add index.html
Basic
git add .

Stage all changed files in the current directory for the next commit.

Example git add .
Basic
git commit -m "[message]"

Commit staged changes with a descriptive message.

Example git commit -m "Fix login bug"
Basic
git status

Show the working tree status โ€” staged, unstaged, and untracked files.

Example git status -s (short format)
Basic
git diff

Show unstaged differences between the working tree and the index.

Example git diff HEAD (all changes including staged)
Basic
git log

Display the commit history of the current branch.

Example git log --oneline -10
Basic
git rm [file]

Remove a file from the working tree and stage the removal.

Example git rm --cached file.log (keep locally)
Basic
git mv [old] [new]

Move or rename a file and stage the change.

Example git mv old-name.js new-name.js
Branching
git branch

List all local branches. The current branch is highlighted with an asterisk.

Example git branch -a (list all including remote)
Branching
git branch [branch-name]

Create a new branch at the current commit.

Example git branch feature-login
Branching
git branch -d [branch]

Delete a local branch (only if already merged).

Example git branch -D feature-old (force delete)
Branching
git checkout [branch]

Switch to an existing branch.

Example git checkout main
Branching
git checkout -b [branch]

Create a new branch and switch to it immediately.

Example git checkout -b feature-dark-mode
Branching
git switch [branch]

Switch to an existing branch (modern alternative to checkout).

Example git switch main
Branching
git switch -c [branch]

Create a new branch and switch to it (modern alternative to checkout -b).

Example git switch -c feature-api
Remote
git remote -v

List all remote connections with their URLs.

Example git remote -v
Remote
git remote add [name] [url]

Add a new remote repository connection.

Example git remote add origin https://github.com/user/repo.git
Remote
git fetch [remote]

Download objects and refs from a remote without merging.

Example git fetch origin
Remote
git pull [remote] [branch]

Fetch from a remote and merge into the current branch.

Example git pull origin main
Remote
git push [remote] [branch]

Push local commits to a remote branch.

Example git push origin feature-login
Remote
git push -u origin [branch]

Push and set the upstream tracking reference (first push for a new branch).

Example git push -u origin feature-api
Remote
git remote remove [name]

Remove a remote connection from the repository.

Example git remote remove upstream
Stashing
git stash

Temporarily save all uncommitted changes and revert to a clean working tree.

Example git stash save "WIP: refactoring auth"
Stashing
git stash pop

Restore the most recently stashed changes and remove them from the stash list.

Example git stash pop stash@{0}
Stashing
git stash list

List all stashed changesets.

Example git stash list --stat
Stashing
git stash apply

Apply stashed changes without removing them from the stash list.

Example git stash apply stash@{1}
Stashing
git stash drop

Remove a specific stash from the stash list.

Example git stash drop stash@{0}
Stashing
git stash clear

Remove all stashed entries. This action is irreversible!

Example git stash clear
Inspect & Log
git log --oneline

Display commit history in a compact one-line-per-commit format.

Example git log --oneline -20
Inspect & Log
git log --graph --all --decorate --oneline

Visualize the full commit tree with branch and tag references.

Example git log --graph --all --decorate --oneline
Inspect & Log
git show [commit]

Show detailed information about a specific commit.

Example git show HEAD~2
Inspect & Log
git blame [file]

Show who last modified each line of a file and when.

Example git blame src/app.js -L 10,30
Inspect & Log
git reflog

Show a log of all reference updates (HEAD movements). Useful for recovering lost commits.

Example git reflog -10
Inspect & Log
git diff [branch1]...[branch2]

Show differences between two branches.

Example git diff main...feature-login
Undo Changes
git reset [file]

Unstage a file while keeping its changes in the working directory.

Example git reset index.html
Undo Changes
git reset --hard [commit]

Reset HEAD to a specific commit and discard all changes. Use with caution!

Example git reset --hard HEAD~1
Undo Changes
git reset --soft HEAD~1

Undo the last commit but keep all changes staged.

Example git reset --soft HEAD~1
Undo Changes
git revert [commit]

Create a new commit that reverses the changes of a specified commit.

Example git revert abc1234
Undo Changes
git restore [file]

Discard uncommitted changes in a file (modern alternative to checkout --).

Example git restore app.js
Undo Changes
git restore --staged [file]

Unstage a file without losing its changes (modern alternative to reset).

Example git restore --staged index.html
Undo Changes
git clean -fd

Remove all untracked files and directories. Use with caution!

Example git clean -fdn (dry run first!)
Tags
git tag

List all tags in the repository.

Example git tag -l "v2.*"
Tags
git tag [tag-name]

Create a lightweight tag at the current commit.

Example git tag v1.0.0
Tags
git tag -a [tag] -m "[message]"

Create an annotated tag with a message (recommended for releases).

Example git tag -a v2.0.0 -m "Release version 2.0.0"
Tags
git push origin [tag]

Push a specific tag to the remote repository.

Example git push origin v1.0.0
Tags
git push --tags

Push all local tags to the remote repository.

Example git push origin --tags
Tags
git tag -d [tag]

Delete a local tag.

Example git tag -d v1.0.0 && git push origin :refs/tags/v1.0.0
Merge & Rebase
git merge [branch]

Merge a branch into the current branch.

Example git merge feature-login
Merge & Rebase
git merge --abort

Abort the current merge process and return to the pre-merge state.

Example git merge --abort
Merge & Rebase
git rebase [branch]

Reapply commits from the current branch on top of another branch.

Example git rebase main
Merge & Rebase
git rebase --continue

Continue the rebase process after resolving conflicts.

Example git rebase --continue
Merge & Rebase
git rebase --abort

Abort the rebase process and return to the original branch state.

Example git rebase --abort
Merge & Rebase
git cherry-pick [commit]

Apply the changes from a specific commit to the current branch.

Example git cherry-pick abc1234

Frequently Asked Questions

Git is a distributed version control system that tracks changes in your source code during software development. It allows multiple developers to work on the same project simultaneously without overwriting each other's changes. With Git, you can revert to previous versions, create branches for experimentation, and collaborate efficiently through remote repositories. It's fast, lightweight, and the industry standard for version control.

Git is the version control system that runs locally on your machine to track code changes. GitHub is a cloud-based hosting service that stores Git repositories online and provides collaboration tools like pull requests, issue tracking, and CI/CD pipelines. You can use Git without GitHub, but GitHub makes it easier to share code and collaborate with others. Alternatives include GitLab, Bitbucket, and Gitea.

It depends on what you want to keep:
โ€ข git reset --soft HEAD~1 โ€” Undo the commit but keep all changes staged.
โ€ข git reset HEAD~1 โ€” Undo the commit and unstage changes (keep them in working directory).
โ€ข git reset --hard HEAD~1 โ€” Completely discard the commit and all changes (use with caution!).
โ€ข git revert HEAD โ€” Create a new commit that reverses the last commit (safer for shared branches).

When a merge conflict occurs, Git marks the conflicting sections in the affected files with <<<<<<<, =======, and >>>>>>>. To resolve:
1. Open the conflicted file(s) in your editor.
2. Choose which changes to keep (or combine them).
3. Remove the conflict markers.
4. Stage the resolved file with git add [file].
5. Complete the merge with git commit (or git merge --continue).
To abort a merge entirely, use git merge --abort.

git fetch downloads new data from the remote repository but does not integrate it into your working files. It's safe and lets you inspect changes before merging. git pull is essentially git fetch followed by git merge โ€” it downloads and immediately merges the remote changes into your current branch. Use git fetch when you want to review changes first; use git pull when you're ready to integrate.

git stash temporarily saves uncommitted changes and reverts your working directory to a clean state. Use it when you need to switch branches but aren't ready to commit your current work. Later, restore your changes with git stash pop or git stash apply. Stashes are stored in a stack, and you can have multiple stashes. Use git stash list to see them all.

git rebase moves or reapplies commits from one branch onto another, creating a cleaner, linear commit history. It's preferred over merging when you want to keep the history tidy and avoid unnecessary merge commits. However, avoid rebasing commits that have already been pushed to a shared repository โ€” it rewrites history and can cause problems for collaborators. Use interactive rebase (git rebase -i) to squash, reorder, or edit commits.

To delete a local branch: git branch -d [branch-name] (safe, only if merged) or git branch -D [branch-name] (force delete).
To delete a remote branch: git push origin --delete [branch-name] or git push origin :[branch-name].
Always make sure the branch changes have been merged or are no longer needed before deleting.

A .gitignore file tells Git which files or directories to ignore in a project. Create a .gitignore file in your repository root and list patterns for files to exclude โ€” such as build outputs, dependency folders (node_modules/), environment files (.env), and OS-specific files (.DS_Store). You can find templates for various languages at github.com/github/gitignore. Files already tracked by Git won't be affected; use git rm --cached [file] to untrack them.

A "detached HEAD" state occurs when you check out a specific commit (instead of a branch). In this state, any new commits you make won't belong to any branch and may be lost when you switch back. To save your work, create a new branch from the detached HEAD: git checkout -b [new-branch-name]. This is a common situation when exploring old commits or tags.