Fundamentals 12 min read

Master Essential Git Commands for Local Repositories and Remote Collaboration

This guide provides a comprehensive collection of Git commands covering repository initialization, user configuration, status checks, staging, committing, branching, checkout, merging, remote operations, tag management, diffing, stash handling, and repository maintenance for effective version control.

JavaScript
JavaScript
JavaScript
Master Essential Git Commands for Local Repositories and Remote Collaboration

Initialize a local Git repository (create new repository) git init Configure user name git config --global user.name "xxx" Configure email git config --global user.email "[email protected]" Enable automatic coloring for git status and related commands

git config --global color.ui true
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto

Clone a remote repository git clone git+ssh://[email protected]/VT.git Show current version status (whether there are modifications) git status Add a specific file (xyz) to the index git add xyz Add all changed files in the current directory to the index git add . Commit changes git commit -m 'xxx' Amend the previous commit (useful for iterative edits) git commit --amend -m 'xxx' Combine add and commit into a single step git commit -am 'xxx' Remove a file from the index git rm xxx Recursive removal git rm -r * Show commit log git log Show a single line of log (use -n for n lines)

git log -1
git log -5

Show commit log with changed files

git log --stat
git log -p -m

Show details of a specific commit

git show dfb02e6e4f2f7b573337763e5c0013802e392818

Show a commit using only the first few characters of its ID git show dfb02 Show the HEAD commit git show HEAD Show the parent of HEAD (previous version) or earlier ancestors

git show HEAD^
# HEAD^^ for two versions back, HEAD^5 for five versions back

List existing tags git tag Add a tag v2.0 git tag -a v2.0 -m 'xxx' Show the log for tag v2.0 git show v2.0 Show the log for tag v2.0 (without the trailing zero) git log v2.0 Show all changes not yet added to the index git diff Show changes that are staged but not yet committed git diff --cached Compare with the previous version git diff HEAD^ Compare the current branch with the lib directory of HEAD git diff HEAD -- ./lib Show differences between remote master and local master git diff origin/master..master Show only file names of differences (no content) git diff origin/master..master --stat Add a remote definition (used for push/pull/fetch)

git remote add origin git+ssh://[email protected]/VT.git

Show local branches git branch Show branches containing a specific commit (e.g., 50089) git branch --contains 50089 Show all branches git branch -a Show all remote branches git branch -r Show branches merged into the current branch git branch --merged Show branches not merged into the current branch git branch --no-merged Rename a local branch git branch -m master master_copy Create a new branch master_copy from the current branch and check it out git checkout -b master_copy Full form of the above command git checkout -b master master_copy Checkout an existing branch features/performance git checkout features/performance Checkout a remote branch hotfixes/BJVEP933 and create a local tracking branch git checkout --track hotfixes/BJVEP933 Checkout version v2.0 git checkout v2.0 Create a new local branch devel from remote develop and check it out git checkout -b devel origin/develop Checkout the README file from HEAD (useful for fixing errors) git checkout -- README Merge remote master into the current branch git merge origin/master Cherry-pick a specific commit (ff44785404a8e) git cherry-pick ff44785404a8e Push the current branch to remote master git push origin master Delete the remote branch hotfixes/BJVEP933 git push origin :hotfixes/BJVEP933 Push all tags to the remote repository git push --tags Fetch all remote branches without updating local branches git fetch Fetch all remote branches and prune deleted ones git fetch --prune Pull remote master and merge into the current branch git pull origin master Rename file README to README2 git mv README README2 Reset current version to HEAD (commonly used after a failed merge) git reset --hard HEAD Rebase (placeholder command shown) git rebase Delete branch hotfixes/BJVEP933 (already merged elsewhere) git branch -d hotfixes/BJVEP933 Force delete branch hotfixes/BJVEP933 git branch -D hotfixes/BJVEP933 List files in the Git index git ls-files Show the history graph of the current branch git show-branch Show the history graph of all branches git show-branch --all Show file changes associated with each commit git whatchanged Revert a specific commit (dfb02e6e4f2f7b573337763e5c0013802e392818)

git revert dfb02e6e4f2f7b573337763e5c0013802e392818

Internal command: show a specific Git object git ls-tree HEAD Internal command: show the SHA‑1 hash of a ref (e.g., v2.0) git rev-parse v2.0 Show all commits, including orphaned nodes

git reflog
git show HEAD@{5}

Show the state of the master branch as of yesterday git show master@{yesterday} Display a concise commit graph git log --pretty=format:'%h %s' --graph Show a specific earlier commit relative to HEAD git show HEAD~3 Show raw commit data for a given hash git show -s --pretty=raw 2be7fcb476 Stash current changes, reverting to HEAD git stash List all stashes git stash list Show details of the first stash git stash show -p stash@{0} Apply the first stash git stash apply stash@{0} Search for text "delete from" in the repository git grep "delete from" Search with multiple patterns (example) git grep -e '#define' --and -e SORT_DIRENT Run garbage collection and integrity check

git gc
git fsck
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.

Gitmergecommand-lineVersion ControlRepositorypushbranching
JavaScript
Written by

JavaScript

Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.

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.