← Back to DevOpsCommands

Git

Git Commands Cheat Sheet

Practical Git commands for source control, branching, commits, collaboration, DevOps workflows and troubleshooting.

17 categories108+ commands

Showing 108 commands.

Git Setup and Configuration

git --version

Display the installed Git version.

git config --global user.name "Your Name"

Set the global Git username used for commits.

git config --global user.email "you@example.com"

Set the global email address used for commits.

git config --global --list

Display global Git configuration settings.

git config --list

Display Git configuration settings currently in effect.

git config --global init.defaultBranch main

Set main as the default branch name for newly initialized repositories.

Repository Initialization

git init

Initialize a new Git repository in the current directory.

git status

Show the current repository state and changed files.

git status --short

Display a compact summary of repository changes.

git add .

Stage all changes in the current repository.

git add <file>

Stage a specific file.

git commit -m "message"

Create a commit containing the staged changes.

Clone and Remote Repositories

git clone https://github.com/user/repo.git

Clone a remote Git repository to the local machine.

git remote -v

Display configured remote repository URLs.

git remote add origin <URL>

Add a remote repository named origin.

git remote get-url origin

Display the URL configured for the origin remote.

git fetch origin

Download objects and references from the origin without changing the current branch.

git fetch --all

Fetch updates from all configured remotes.

Status and Inspection

git status

Show staged, unstaged and untracked changes.

git status --short

Show a compact two-column status of changed files.

git diff

Show unstaged changes in tracked files.

git diff --cached

Show changes currently staged for the next commit.

git show

Display details about a commit.

git show <commit>

Display a specific commit and its changes.

git branch

List local branches.

git branch -a

List local and remote-tracking branches.

Add and Commit

git add <file>

Stage one specific file.

git add .

Stage changes under the current directory.

git add -A

Stage all additions, modifications and deletions in the repository.

git commit -m "Add feature"

Create a commit with a message.

git commit --amend

Modify the most recent commit.

git commit --amend --no-edit

Update the latest commit without changing its commit message.

Branches

git branch

List local branches.

git branch feature/login

Create a new branch without switching to it.

git switch feature/login

Switch to an existing branch.

git switch -c feature/login

Create and switch to a new branch.

git checkout feature/login

Switch to an existing branch using the older checkout command.

git branch -d feature/login

Delete a local branch after it has been merged.

git branch -D feature/login

Force-delete a local branch. Use carefully.

git branch -m new-name

Rename the current branch.

Merge and Rebase

git merge feature/login

Merge a branch into the current branch.

git merge --no-ff feature/login

Create a merge commit even when a fast-forward merge is possible.

git rebase main

Replay the current branch commits on top of main.

git rebase -i HEAD~3

Interactively edit, reorder, squash or drop recent commits.

git rebase --continue

Continue a rebase after resolving conflicts.

git rebase --abort

Cancel an in-progress rebase and return to the previous state.

git merge --abort

Cancel an in-progress merge when supported by the current repository state.

Push and Pull

git push origin main

Push the local main branch to the origin remote.

git push

Push the current branch to its configured upstream branch.

git push -u origin feature/login

Push a new branch and configure its upstream remote branch.

git pull

Fetch and integrate changes from the configured upstream branch.

git pull --rebase

Fetch and rebase local commits on top of the upstream branch.

git push origin --delete feature/login

Delete a branch from the remote repository.

Stash

git stash

Temporarily save uncommitted changes and clean the working tree.

git stash push -m "work in progress"

Create a named stash containing current changes.

git stash list

List saved stashes.

git stash pop

Apply the latest stash and remove it from the stash list.

git stash apply

Apply the latest stash without removing it.

git stash drop

Delete a stash entry.

git stash clear

Delete all stashes. Use carefully.

History and Logs

git log

Display commit history.

git log --oneline

Display a compact one-line commit history.

git log --oneline --decorate --graph

Display a compact graphical branch and commit history.

git log --oneline -10

Show the ten most recent commits in compact format.

git log --author="Name"

Filter commit history by author.

git log --since="1 week ago"

Show commits created within a specified time period.

git reflog

Show updates to local branch references, useful for recovering previous states.

Undo Changes

git restore <file>

Discard unstaged changes in a tracked file.

git restore --staged <file>

Remove a file from the staging area without deleting its working-tree changes.

git reset HEAD <file>

Unstage a file using the reset command.

git revert <commit>

Create a new commit that reverses the changes introduced by another commit.

git reset --soft HEAD~1

Move HEAD back one commit while keeping changes staged.

git reset --mixed HEAD~1

Move HEAD back one commit while keeping changes in the working tree but unstaged.

git reset --hard HEAD~1

Move HEAD back one commit and discard working-tree changes. Use with extreme care.

Tags and Releases

git tag

List local Git tags.

git tag v1.0.0

Create a lightweight tag for the current commit.

git tag -a v1.0.0 -m "Release 1.0.0"

Create an annotated release tag.

git show v1.0.0

Display information about a tag.

git push origin v1.0.0

Push a specific tag to the remote repository.

git push origin --tags

Push all local tags to the remote repository.

Diff and Comparison

git diff

Show unstaged changes.

git diff --cached

Show staged changes.

git diff main..feature

Compare changes between two branches.

git diff HEAD~1 HEAD

Compare the latest commit with its parent.

git diff --stat

Show a summary of changed files and line counts.

git diff --name-only

List files changed between revisions.

Cherry-pick

git cherry-pick <commit>

Apply the changes introduced by a specific commit to the current branch.

git cherry-pick --no-commit <commit>

Apply a commit's changes without automatically creating a commit.

git cherry-pick --continue

Continue a cherry-pick after resolving conflicts.

git cherry-pick --abort

Cancel an in-progress cherry-pick operation.

Remote Tracking and Branch Management

git branch -r

List remote-tracking branches.

git branch -vv

Show local branches together with upstream tracking information.

git fetch origin

Download the latest remote references without modifying the working branch.

git remote show origin

Display detailed information about a remote repository.

git push -u origin main

Push main and establish its upstream branch.

Git Troubleshooting

git status

Start troubleshooting by checking the repository state.

git log --oneline --decorate --graph -10

Inspect recent commits and branch relationships.

git reflog

Find previous HEAD and branch states when commits appear to be missing.

git remote -v

Verify the configured remote URLs.

git branch -vv

Check branch tracking and upstream configuration.

git fetch --all --prune

Fetch remote updates and remove stale remote-tracking references.

git fsck --lost-found

Inspect unreachable Git objects when investigating repository recovery.

DevOps Git Workflow

git status --short

Check exactly what changed before staging files.

git add <file>

Stage only the intended file or files.

git commit -m "Describe change"

Create a focused commit describing the change.

git pull --rebase origin main

Update the local branch while keeping local commits on top of the remote branch.

git push origin main

Push the tested local changes to the main branch.

git log --oneline --decorate --graph -10

Verify recent repository history after pushing changes.

Recommended DevOps Git Workflow

# Check repository state
git status --short

# Review changes
git diff

# Stage the intended files
git add <file>

# Review staged changes
git diff --cached

# Commit
git commit -m "Describe the change"

# Update from remote
git pull --rebase origin main

# Push
git push origin main

# Verify history
git log --oneline --decorate --graph -10

DevOpsCommands Tip

Prefer small, focused commits that describe one logical change. Before pushing, review the status and staged diff so unrelated files are not accidentally included.