Skip to content

Instantly share code, notes, and snippets.

@smarteist
Last active May 9, 2025 12:08
Show Gist options
  • Select an option

  • Save smarteist/d5d26bb62bc9bbf42e82ab61bd2d94cf to your computer and use it in GitHub Desktop.

Select an option

Save smarteist/d5d26bb62bc9bbf42e82ab61bd2d94cf to your computer and use it in GitHub Desktop.
git essential commands

Git Command Cheat Sheet

Current Git Version

git --version

Displays the installed Git version.

Git Global User Configuration

git config --global user.name "FIRST_NAME LAST_NAME"
git config --global user.email "MY_NAME@example.com"

Sets your global username and email for commits.

Initialize a Project

git init

Creates a new Git repository.

Uninitialize a Project

rm -rf .git

Removes the Git repository from the project.

Git Files State

git status

Shows the current status of files in the repository.

Commands Log

git log

Displays the commit history.

Clone a Project

git clone URL.git

Clones a repository from a remote URL.

Add Files to Stage

git add .
git add somefile.java

Stages all files or a specific file for commit. Show or add modifed files only:

git ls-files -m 
git ls-files -m | xargs git add

Remove Files from Tracked List

git rm --cached <file>
git rm -r --cached <directory>

Untracks a file or directory without deleting it.

Commit a Change

git commit -m 'initial commit'

Commits staged changes with a message.

Add Branch

git checkout -b dev

Creates and switches to a new branch named "dev".

Remove Branch

git branch -d dev

Deletes the "dev" branch.

Switch to Master Branch

git checkout master

Switches to the master branch.

Merge with Current Branch

git merge otherbranch

Merges changes from "otherbranch" into the current branch.

Add Remote URL

git remote add origin "URL.git"

Adds a remote repository with the alias "origin".

Current Push and Fetch Remotes

git remote -v

Lists all remote repositories.

Force Push to Remote

git push -uf origin master

Pushes changes to the remote repository, forcing updates.

Pull and Update Project Changes

git pull origin master

Fetches and merges changes from the remote master branch.

Fix Conflict Hard Reset (Backup First)

git fetch origin
git reset --mixed origin/master

Resets the current branch to match the remote master.

Reset Options

--hard

Resets everything to the specified commit, discarding all local changes.

--mixed

Resets the index but keeps local changes intact.

--soft

Keeps the index and working directory unchanged; changes are staged for commit.

--merge

Resets the index and affected files from a failed merge, keeping other changes.

File Permissions

To prevent issues with file permissions across platforms:

git config core.fileMode false
git config --global core.fileMode false

Disables tracking of file mode changes in Git.

Git Stash

List Stashes

git stash list

Displays a list of all stashed changes.

Show Stash Details

git stash show [<stash>]

Shows a summary of changes in the specified stash. If no stash is specified, it shows the latest stash.

Drop a Stash

git stash drop [<stash>]

Removes the specified stash from the stash list. If no stash is specified, it drops the latest stash.

Pop a Stash

git stash pop [<stash>]

Applies the changes from the specified stash and removes it from the stash list. If no stash is specified, it pops the latest stash.

Apply a Stash

git stash apply [<stash>]

Applies the changes from the specified stash without removing it from the stash list. If no stash is specified, it applies the latest stash.

Create a Branch from Stash

git stash branch <branchname> [<stash>]

Creates a new branch from the specified stash and applies the changes. If no stash is specified, it uses the latest stash.

Stash Changes

git stash

Stashes the current changes in the working directory and index.

Save Stash with a Message

git stash save "message"

Stashes the current changes with an optional message for identification.

Clear All Stashes

git stash clear

Removes all stashed changes from the stash list.

Create a Stash without Modifying the Working Directory

git stash create [<message>]

Creates a stash without applying it to the working directory. Optionally, you can provide a message.

Store a Stash

git stash store [-m <message>] <stash>

Stores a stash with an optional message. You can specify which stash to store.

Sure! Here are the Git Worktree commands with detailed explanations:

Git Worktree Commands

Add a Worktree

git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]] [--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]

Creates a new worktree at the specified path for the given branch or commit. Options include:

  • -f: Force the addition of the worktree.
  • --detach: Create a detached HEAD worktree.
  • --checkout: Checkout the specified commit or branch.
  • --lock [--reason <string>]: Lock the worktree to prevent changes, with an optional reason.
  • --orphan: Create an orphan branch.
  • -b <new-branch>: Create a new branch and switch to it.
  • -B <new-branch>: Create or reset a branch to the specified commit.

List Worktrees

git worktree list [-v | --porcelain [-z]]

Displays all active worktrees associated with the repository. Options include:

  • -v: Verbose output.
  • --porcelain: Machine-readable output.
  • -z: Use null character as delimiter for paths.

Lock a Worktree

git worktree lock [--reason <string>] <worktree>

Locks the specified worktree to prevent changes. Optionally, provide a reason for locking.

Move a Worktree

git worktree move <worktree> <new-path>

Moves the specified worktree to a new path.

Prune Worktrees

git worktree prune [-n] [-v] [--expire <expire>]

Removes references to worktrees that no longer exist. Options include:

  • -n: Dry run, show what would be pruned without actually doing it.
  • -v: Verbose output.
  • --expire <expire>: Specify an expiration time for pruning.

Remove a Worktree

git worktree remove [-f] <worktree>

Removes the specified worktree. Use -f to force removal if there are uncommitted changes.

Repair Worktrees

git worktree repair [<path>…​]

Repairs the specified worktree(s) if they are in a broken state.

Unlock a Worktree

git worktree unlock <worktree>

Unlocks the specified worktree, allowing changes to be made again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment