GIT Command Reference Table with Description and Examples

1. Git Configuration

CommandDescriptionExample
git config --global user.name "Your Name"Set username globallygit config --global user.name "Pritesh Thamke"
git config --global user.email "you@example.com"Set email globallygit config --global user.email "pritesh@example.com"
git config --listView all config settingsgit config --list
git config --global core.editor nanoSet default editorgit config --global core.editor nano
Command NameDescriptionExample
git initInitializes a new Git repository in the current directory.git init Creates a .git folder in the current directory to start version control.
git config --global user.name "<name>"Sets the global username for commits.git config --global user.name "Pritesh thamke" Configures the username as “John Doe” for all repositories.
git config --global user.email "<email>"Sets the global email for commits.git config --global user.email "pritesh.cotocus@gmail.com" Sets the email for commit authorship.
git config --listLists all Git configuration settings.git config --list Displays all global and local config settings, e.g., user.name, user.email.
git config --global core.editor "<editor>"Sets the default text editor for Git (e.g., for commit messages).git config --global core.editor "vim" Sets Vim as the default editor.
git config --global color.ui autoEnables colored output for Git commands.git config --global color.ui auto Makes Git output (like status, diff) colorful.

2. Git Repository Initialization

CommandDescriptionExample
git initInitialize a new Git repogit init
git clone <url>Clone remote repogit clone https://github.com/user/repo.git
Command NameDescriptionExample
git clone <repo-url>Clones a remote repository to your local machine.git clone https://github.com/user/repo.git Downloads the repository to a local folder named repo.
git remote add <name> <url>Adds a remote repository to your local repository.git remote add origin https://github.com/user/repo.git Links the local repo to a remote named “origin”.
git remote -vLists all remote repositories and their URLs.git remote -v Shows the fetch and push URLs for remotes like “origin”.
git remote remove <name>Removes a remote repository from the local config.git remote remove origin Deletes the “origin” remote from the repository.
git remote rename <old-name> <new-name>Renames a remote repository.git remote rename origin upstream Renames the “origin” remote to “upstream”.

3. Staging and Snapshot

CommandDescriptionExample
git statusShow status of filesgit status
git add <file>Add file to staginggit add index.html
git add .Add all files to staginggit add .
git commit -m "message"Commit staged changesgit commit -m "Initial commit"
git commit -a -m "message"Commit all tracked filesgit commit -a -m "Updated config"
git restore <file>Discard changes to filegit restore index.html
git reset <file>Unstage filegit reset style.css
Command NameDescriptionExample
git add <file>Adds a specific file to the staging area.git add index.html Stages index.html for the next commit.
git add .Adds all modified and new files in the current directory to the staging area.git add . Stages all changes in the working directory.
git add -AStages all changes (including deletions) in the entire repository.git add -A Stages all modified, new, and deleted files.
git commit -m "<message>"Commits staged changes with a message.git commit -m "Add new feature" Creates a commit with the message “Add new feature”.
git commit -a -m "<message>"Stages and commits all modified tracked files in one step.git commit -a -m "Update styles" Stages and commits changes to tracked files.
git commit --amendModifies the most recent commit (e.g., to change the message or add files).git commit --amend -m "Updated message" Updates the last commit’s message.

4. Branching and Merging

CommandDescriptionExample
git branchList branchesgit branch
git branch <name>Create branchgit branch feature-x
git checkout <name>Switch branchgit checkout feature-x
git switch <name>Switch branch (modern way)git switch main
git merge <branch>Merge branch into currentgit merge feature-x
git branch -d <name>Delete branchgit branch -d feature-x
git checkout -b <name>Create and switchgit checkout -b bugfix
Command NameDescriptionExample
git branchLists all branches in the repository (current branch marked with *).git branch Shows branches like main, feature-x.
git branch <branch-name>Creates a new branch.git branch feature-x Creates a branch named feature-x.
git checkout <branch-name>Switches to the specified branch.git checkout feature-x Switches to the feature-x branch.
git checkout -b <branch-name>Creates and switches to a new branch in one step.git checkout -b feature-x Creates and switches to feature-x.
git branch -d <branch-name>Deletes a branch (only if merged).git branch -d feature-x Deletes the feature-x branch if merged.
git branch -D <branch-name>Force deletes a branch (even if unmerged).git branch -D feature-x Deletes feature-x regardless of merge status.
git branch -m <new-name>Renames the current branch.git branch -m new-feature Renames the current branch to new-feature.
Command NameDescriptionExample
git merge <branch-name>Merges the specified branch into the current branch.git merge feature-x Merges feature-x into the current branch (e.g., main).
git merge --abortAborts a merge in progress in case of conflicts.git merge --abort Cancels a conflicted merge and restores the pre-merge state.
git rebase <branch-name>Reapplies commits from the current branch onto another branch.git rebase main Reapplies current branch’s commits on top of main.
git rebase -i <commit-hash>Interactively rebase commits (e.g., squash, reorder).git rebase -i HEAD~3 Interactively rebases the last 3 commits.
git rebase --continueContinues a rebase after resolving conflicts.git rebase --continue Proceeds with rebase after fixing conflicts.
git rebase --abortAborts a rebase in progress.git rebase --abort Cancels a rebase and restores the original state.


5. Remote Repositories

CommandDescriptionExample
git remote -vShow remotesgit remote -v
git remote add origin <url>Add a remote repogit remote add origin https://github.com/user/repo.git
git push -u origin mainPush first timegit push -u origin main
git pushPush latest commitsgit push
git pullFetch + merge from remotegit pull origin main
git fetchDownload changes (no merge)git fetch origin
git remote remove originRemove a remotegit remote remove origin
Command NameDescriptionExample
git pushPushes local commits to the default remote branch.git push Pushes commits from the current branch to its remote counterpart.
git push origin <branch-name>Pushes commits to a specific remote branch.git push origin feature-x Pushes feature-x to the remote origin.
git push --forceForce pushes commits, overwriting remote history (use cautiously).git push --force Overwrites the remote branch with local changes.
git push --set-upstream origin <branch-name>Pushes a branch and sets it to track the remote branch.git push --set-upstream origin feature-x Pushes and tracks feature-x on origin.
git pullFetches and merges changes from the remote branch.git pull Pulls updates from the tracked remote branch.
git pull origin <branch-name>Pulls changes from a specific remote branch.git pull origin main Pulls updates from main on origin.
git fetchDownloads objects and refs from the remote without merging.git fetch Fetches updates from all remotes without merging.
git fetch origin <branch-name>Fetches a specific branch from the remote.git fetch origin main Fetches main from origin.

6. Stashing (Temporary Save)

CommandDescriptionExample
git stashSave changes temporarilygit stash
git stash listList all stashesgit stash list
git stash popApply and remove last stashgit stash pop
git stash applyApply last stash (keep it)git stash apply
Command NameDescriptionExample
git stashSaves modified and staged changes to a stack and resets the working directory.git stash Stashes current changes and cleans the working directory.
git stash push -m "<message>"Stashes changes with a custom message.git stash push -m "WIP feature" Stashes changes with the message “WIP feature”.
git stash listLists all stashed changes.git stash list Shows all stashes, e.g., stash@{0}: WIP feature.
git stash applyApplies the most recent stash without removing it.git stash apply Reapplies the latest stash to the working directory.
git stash apply stash@{n}Applies a specific stash by index.git stash apply stash@{1} Reapplies the stash at index 1.
git stash popApplies the most recent stash and removes it from the stack.git stash pop Applies and deletes the latest stash.
git stash drop stash@{n}Removes a specific stash from the stack.git stash drop stash@{0} Deletes the stash at index 0.
git stash clearRemoves all stashes from the stack.git stash clear Deletes all stashed changes.

7. Logs and History

CommandDescriptionExample
git logShow commit historygit log
git log --onelineOne-line summarygit log --oneline
git show <commit>Show commit detailsgit show f3e1a90
git blame <file>Show who changed each linegit blame app.py
Command NameDescriptionExample
git statusShows the current state of the working directory and staging area.git status Displays modified, staged, and untracked files.
git logShows the commit history of the current branch.git log Lists all commits with their details (hash, author, message).
git log --onelineDisplays commit history in a compact, one-line format.git log --oneline Shows each commit as a single line with hash and message.
git log --graphShows a graphical representation of the commit history.git log --graph Displays branch and merge history visually.
git log --author="<name>"Filters commits by author.git log --author="John" Shows commits made by “John”.
git diffShows changes between the working directory and the staging area.git diff Displays unstaged changes in tracked files.
git diff --stagedShows changes between the staging area and the last commit.git diff --staged Displays staged changes ready to be committed.
git show <commit-hash>Shows details and changes for a specific commit.git show abc123 Displays details of the commit with hash abc123.

8. Undo and Reset

CommandDescriptionExample
git reset HEAD~1Undo last commit (keep files)git reset HEAD~1
git reset --hard HEAD~1Completely remove last commitgit reset --hard HEAD~1
git revert <commit>Revert specific commitgit revert f3e1a90
Command NameDescriptionExample
git reset <file>Unstages a file but preserves its changes in the working directory.git reset index.html Removes index.html from the staging area.
git reset --hard <commit-hash>Resets the repository to a specific commit, discarding changes.git reset --hard abc123 Resets to commit abc123, discarding all later changes.
git revert <commit-hash>Creates a new commit that undoes the changes of a specific commit.git revert abc123 Creates a commit that reverses changes from abc123.
git checkout -- <file>Discards changes in a file, restoring it to the last committed state.git checkout -- index.html Reverts index.html to its last committed version.
git clean -fRemoves untracked files from the working directory.git clean -f Deletes untracked files and directories.
git clean -fdRemoves untracked files and directories (including folders).git clean -fd Deletes untracked files and directories recursively.

9. Git Diff Tools

CommandDescriptionExample
git diffSee unstaged changesgit diff
git diff --stagedSee staged changesgit diff --staged
git diff <branch1> <branch2>Compare branchesgit diff main dev

10. Tags (Releases/Versions)

CommandDescriptionExample
git tagList tagsgit tag
git tag v1.0Create taggit tag v1.0
git tag -a v1.1 -m "Release v1.1"Annotated taggit tag -a v1.1 -m "Stable release"
git push origin v1.1Push tag to remotegit push origin v1.1
Command NameDescriptionExample
git tag <tag-name>Creates a lightweight tag at the current commit.git tag v1.0 Creates a tag named v1.0 for the current commit.
git tag -a <tag-name> -m "<message>"Creates an annotated tag with a message.git tag -a v1.0 -m "Release 1.0" Creates an annotated tag with a message.
git tagLists all tags in the repository.git tag Shows tags like v1.0, v2.0.
git push origin <tag-name>Pushes a specific tag to the remote repository.git push origin v1.0 Pushes the v1.0 tag to origin.
git push origin --tagsPushes all tags to the remote repository.git push origin --tags Pushes all local tags to origin.
git tag -d <tag-name>Deletes a tag locally.git tag -d v1.0 Deletes the v1.0 tag locally.
git push origin --delete <tag-name>Deletes a tag from the remote repository.git push origin --delete v1.0 Deletes the v1.0 tag from origin.

11. Git Ignore

FileDescriptionExample
.gitignoreIgnore files/foldersAdd node_modules/ or *.log in .gitignore

12. Git Aliases (Optional)

CommandDescriptionExample
git config --global alias.co checkoutSet aliasgit co dev
git config --global alias.st statusgit st for statusgit st

13. Miscellaneous Commands

Additional useful Git commands for various purposes.

Command NameDescriptionExample
git blame <file>Shows who last modified each line of a file.git blame index.html Displays author and commit details for each line.
git bisect startStarts a binary search to find a bug-introducing commit.git bisect start Begins the bisect process.
git bisect good <commit-hash>Marks a commit as good during bisect.git bisect good abc123 Marks commit abc123 as good.
git bisect bad <commit-hash>Marks a commit as bad during bisect.git bisect bad def456 Marks commit def456 as bad.
git bisect resetEnds the bisect process and returns to the original state.git bisect reset Exits bisect mode.
git cherry-pick <commit-hash>Applies a specific commit to the current branch.git cherry-pick abc123 Applies the changes from commit abc123.
git archive --format=zip --output=<file>.zip <branch>Creates a zip archive of a branch or commit.git archive --format=zip --output=repo.zip main Creates a repo.zip of the main branch.

Git Quick Command Cheatsheet

Use CaseCommand
Initialize repogit init
Clone repogit clone URL
Check statusgit status
Stage filesgit add .
Commit changesgit commit -m "msg"
Push changesgit push origin branch
Pull changesgit pull
Create branchgit checkout -b branch
Merge branchesgit merge branch
View historygit log --oneline
Reset commitgit reset HEAD~1
View remotesgit remote -v

Leave a Comment