Essential Git Commands Every Developer Should Know
This guide provides a comprehensive overview of common Git commands, covering SSH key creation, user configuration, repository initialization, file staging, committing, branch and tag management, remote synchronization, undo operations, and .gitignore patterns, enabling developers to efficiently manage version control workflows.
Common Git Commands
Create SSH Key
ssh-keygen -t rsa -C "[email protected]"Configure User Information
git config --global user.name "Your Name"
git config --global user.email "[email protected]"Repository Operations
Create a new Git repository in the current directory
git initInitialize a new repository in a specific directory
git init [project-name]Clone a repository and its full history
git clone [url]Add / Remove Files
Add specified files to the staging area
git add [file1] [file2] ...Add a directory (including subdirectories) to the staging area
git add [dir]Add all files in the current directory to the staging area
git add .Interactively select changes to stage
git add -pRemove files from the working tree and stage the deletion
git rm [file1] [file2] ...Stop tracking a file while keeping it in the working tree
git rm --cached [file]Rename a file and stage the rename
git mv [file-original] [file-renamed]Commit Changes
Commit staged changes with a message
git commit -m [message]Commit all changes in the working tree directly to the repository
git commit -aShow diff information during commit
git commit -vAmend the previous commit (useful for fixing the commit message)
git commit --amend -m [message]Amend the previous commit and include new changes
git commit --amend [file1] [file2] ...View Information
Show changed files
git statusShow commit history of the current branch
git logShow commit history with file change statistics
git log --statSearch commit history by keyword
git log -S [keyword]Show commits containing a specific keyword in their messages
git log [tag] HEAD --grep featureShow the history of a specific file, following renames
git log --follow [file]Show diffs for a specific file
git log -p [file]Show the last five commits
git log -5 --pretty --onelineList contributors sorted by number of commits
git shortlog -snShow who modified a file and when
git blame [file]Show differences between the working tree and the index
git diffShow differences between the index and the last commit
git diff --cached [file]Show differences between the working tree and the latest commit on the current branch
git diff HEADShow differences between two branches
git diff [first-branch]...[second-branch]Branch Management
List all local branches
git branchList all remote branches
git branch -rList all local and remote branches
git branch -aCreate a new branch (stays 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]Set up a tracking relationship with a remote branch
git branch --track [branch] [remote-branch]Switch to a branch and update the working tree
git checkout [branch-name]Switch back to the previous branch
git checkout -Set upstream 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]Tag Management
List all tags
git tagCreate a tag on the current commit
git tag [tag]Create a tag on a specific commit
git tag [tag] [commit]Delete a local tag
git tag -d [tag]Delete a remote tag
git push origin :refs/tags/[tagName]Show tag information
git show [tag]Push a specific tag
git push [remote] [tag]Push all tags
git push [remote] --tagsCreate a branch from a tag
git checkout -b [branch] [tag]Remote Synchronization
Fetch all changes from a remote
git fetch [remote]Show all configured remotes
git remote -vShow 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]Force pull allowing unrelated histories
git pull origin master --allow-unrelated-historiesPush a local branch to a remote
git push [remote] [branch]Force push the current branch
git push [remote] --forcePush all branches
git push [remote] --allUndo Operations
Restore a file from the index to the working tree
git checkout [file]Restore a file from a specific commit
git checkout [commit] [file]Restore all files from the index
git checkout .Reset a file in the index to the last commit (working tree unchanged)
git reset [file]Reset index and working tree to the last commit
git reset --hardReset current branch to a specific commit (index reset)
git reset [commit]Hard reset current branch to a specific commit (index and working tree reset)
git reset --hard [commit]Keep local changes while resetting to a commit
git reset --keep [commit]Revert a commit by creating a new commit that undoes its changes
git revert [commit]Stash uncommitted changes and later reapply them
git stash
git stash pop.gitignore Configuration
Pattern syntax:
Leading slash (/) denotes a directory. Asterisk (*) matches multiple characters. Question mark (?) matches a single character. Square brackets [] define a character set. Exclamation mark (!) negates a pattern (track the file).
Rules are evaluated top‑to‑bottom; earlier broader rules can override later ones.
Examples:
(1) fd1/* – ignore everything under any fd1 directory.
(2) /fd1/* – ignore only the fd1 directory at the repository root.
(3)
*
! .gitignore
! /fw/bin/
! /fw/sf/– ignore all files except the .gitignore file and the specified directories.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
