Essential Git Command Cheat Sheet for Developers

A comprehensive, English-language cheat sheet that explains Git terminology and provides ready-to-use command snippets for initializing repositories, configuring settings, managing files, committing changes, handling branches, tags, viewing history, synchronizing remotes, and undoing actions.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Essential Git Command Cheat Sheet for Developers

Glossary: Workspace (working directory), Index/Stage (staging area), Repository (local repository), Remote (remote repository).

1. Create a Repository

# Initialize a new Git repository in the current directory $ git init # Initialize a new repository in a specific directory $ git init [project-name] # Clone an existing repository with its full history $ git clone [url]

2. Configuration

Git configuration is stored in .gitconfig, which can be global (in the user’s home directory) or local (in the project directory).

# List current Git configuration $ git config --list # Edit the Git configuration file $ git config -e [--global] # Set user name for commits $ git config [--global] user.name "[name]" # Set user email for commits $ git config [--global] user.email "[email address]"

3. Add / Delete Files

# Add specific files to the staging area $ git add [file1] [file2] ... # Add an entire directory (including sub‑directories) to the staging area $ git add [dir] # Add all files in the current directory to the staging area $ git add . # Remove files from the working directory and stage the deletion $ git rm [file1] [file2] ... # Stop tracking a file but keep it in the working directory $ git rm --cached [file] # Rename a file and stage the rename $ git mv [file-original] [file-renamed]

4. Commit Changes

# Commit the staged changes with a message $ git commit -m [message] # Commit specific files directly $ git commit [file1] [file2] ... -m [message] # Commit all changes in the working directory $ git commit -a # Show diff information during commit $ git commit -v # Amend the previous commit (e.g., to change the message) $ git commit --amend -m [message] # Amend the previous commit and include new changes $ git commit --amend [file1] [file2] ...

5. Branch Management

# List all local branches $ git branch # List all remote branches $ git branch -r # List both local and remote branches $ git branch -a # Create a new branch (stay on current branch) $ git branch [branch-name] # Create and switch to a new branch $ git checkout -b [branch] # Create a branch pointing to a specific commit $ git branch [branch] [commit] # Create a tracking branch for a remote branch $ git branch --track [branch] [remote-branch] # Switch to an existing branch $ git checkout [branch-name] # Set upstream tracking for a branch $ git branch --set-upstream [branch] [remote-branch] # Merge a branch into the current branch $ git merge [branch] # Cherry‑pick a specific commit $ git cherry-pick [commit] # Delete a local branch $ git branch -d [branch-name] # Delete a remote branch $ git push origin --delete [branch-name] $ git branch -dr [remote/branch]

6. Tag Operations

# List all tags $ git tag # Create a tag on the current commit $ git tag [tag] # Create a tag on a specific commit $ git tag [tag] [commit] # Show tag details $ git show [tag] # Push a specific tag $ git push [remote] [tag] # Push all tags $ git push [remote] --tags # Create a branch from a tag $ git checkout -b [branch] [tag]

7. Inspect Repository

# Show changed files $ git status # Show commit history of the current branch $ git log # Show commit history with file change statistics $ git log --stat # Show history of a specific file (including renames) $ git log --follow [file] # Show diff for a specific file $ git log -p [file] # Show who modified a file and when $ git blame [file] # Show differences between staging area and working directory $ git diff # Show differences between staging area and last commit $ git diff --cached [file] # Show differences between working directory and HEAD $ git diff HEAD # Show differences between two branches $ git diff [first-branch]...[second-branch] # Show commit metadata and content changes $ git show [commit] # Show only changed file names for a commit $ git show --name-only [commit] # Show the content of a file at a specific commit $ git show [commit]:[filename] # Show recent commits on the current branch $ git reflog

8. Remote Synchronization

# Fetch all changes from a remote $ git fetch [remote] # List all configured remotes $ git remote -v # Show details of a specific remote $ git remote show [remote] # Add a new remote repository $ git remote add [shortname] [url] # Pull changes from a remote branch and merge $ git pull [remote] [branch] # Push a local branch to a remote $ git push [remote] [branch] # Force‑push the current branch $ git push [remote] --force # Push all branches $ git push [remote] --all

9. Undo Operations

# Restore a specific file from the staging area to the working directory $ git checkout [file] # Restore a file from a specific commit $ git checkout [commit] [file] # Restore all files to the state of the previous commit $ git checkout . # Reset a file in the staging area to match the last commit (working directory unchanged) $ git reset [file] # Reset both staging area and working directory to the last commit $ git reset --hard # Reset the current branch pointer to a specific commit (staging area reset, working directory unchanged) $ git reset [commit] # Reset HEAD to a specific commit and make the working directory match $ git reset --hard [commit] # Reset HEAD to a specific commit but keep working directory changes $ git reset --keep [commit] # Revert a specific commit (creates a new commit that undoes the changes) $ git revert [commit]

10. Miscellaneous

# Create a compressed archive suitable for distribution $ git archive
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Gitcommand-lineVersion ControlBranchcommit
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.