The Most Used Git Commands: A Categorized Guide


1. Repository Setup & Creation
These commands are used to start a new project or get a copy of an existing one.
git init
: Create an empty Git repository or reinitialize an existing one.# Initializes a new Git repository in the current folder. git init
git clone [url]
: Clone a repository into a new directory.# Creates a local copy of a remote repository. git clone https://github.com/git/git.git
git config
: Get and set repository or global options.# Sets your user name and email for your commits. git config --global user.name "John Doe" git config --global user.email "John.doe@example.com"
git remote
: Manage the set of repositories whose branches you track.# Adds a new remote repository named 'origin'. git remote add origin https://github.com/user/new-project.git
2. Basic Workflow & Staging
This is the fundamental loop of a Git workflow: adding, committing, and checking status.
git status
: Show the working tree status.# Displays modified, staged, and untracked files. git status
git add [file]
: Add file contents to the index (the staging area).# Stages a specific file for the next commit. git add index.html # Stages all changed and new files in the current directory. git add .
git commit
: Record changes to the repository.# Commits the staged changes with a descriptive message. git commit -m "Add initial homepage structure"
git mv [old-file] [new-file]
: Move or rename a file.# Renames a file and stages the change. git mv README.md README.txt
git rm [file]
: Remove a file from the working tree and the index.# Removes a file from your project and stages the deletion. git rm old-file.js
3. Branching & Merging
Branching allows for parallel lines of development.
git branch
: List, create, or delete branches.# Lists all local branches. git branch # Creates a new branch named 'feature/user-profile'. git branch feature/user-profile
git checkout
: Switch branches or restore working tree files.# Switches to an existing branch. git checkout feature/user-profile
git checkout -b [new-branch-name]
: Create and switch to a new branch.# Creates and switches to a new branch in a single command. git checkout -b feature/login-flow
git merge [branch-name]
: Join two or more development histories together.# Merges the changes from a feature branch into the current branch. git merge feature/user-profile
git mergetool
: Run a merge conflict resolution tool.# Opens a tool to help you resolve conflicts that occurred during a merge. git mergetool
git branch -d [branch-name]
: Delete a local branch.# Deletes a local branch after it has been merged. git branch -d feature/login-flow
git push origin --delete [branch-name]
: Delete a remote branch.# Deletes a branch on the remote repository. git push origin --delete feature/user-profile
4. Sharing & Collaboration (Remote Operations)
These commands are used to interact with remote repositories.
git fetch
: Download objects and refs from another repository.# Downloads new commits from the remote but does not merge them. git fetch origin
git pull
: Fetch from and integrate with another repository or a local branch.# Downloads and merges changes from the remote into your current branch. git pull origin main
git push
: Update remote refs along with associated objects.# Pushes your local commits to the remote repository. git push origin main
git remote add origin [url]
: Add a remote repository.# Adds a remote repository to your local project. git remote add origin https://github.com/user/new-project.git
git remote -v
: List all remote connections.# Displays the names and URLs of your remote repositories. git remote -v
5. Inspecting & Comparing
These commands are crucial for reviewing history and changes.
git log
: Show commit logs.# Shows the commit history. git log # Shows a condensed, one-line history with a graph. git log --oneline --graph
git diff
: Show changes between commits, commit and working tree, etc.# Shows unstaged changes in your working directory. git diff # Shows changes that are staged but not yet committed. git diff --staged
git show
: Show various types of objects (commits, blobs, trees).# Shows the changes and metadata of a specific commit. git show 7b3e0b4
git bisect
: Find the commit that introduced a bug using binary search.# A powerful tool to find a bug by automatically checking out commits in a range. git bisect start git bisect bad # This commit has the bug git bisect good 7b3e0b4 # This commit is good
git blame [file]
: Show who last modified each line of a file.# Displays the commit and author for each line of code in a file. git blame index.html
6. Undoing & Rewriting History
These commands are for correcting mistakes. Some are very powerful and can be destructive.
git reset
: Reset current HEAD to the specified state.# Unstages a file without changing the working directory. git reset index.html # A destructive command! Resets your branch to a commit, deleting subsequent history and files. git reset --hard 7b3e0b4
git revert
: Revert some existing commits.# Creates a new commit that undoes the changes of a previous commit. git revert 7b3e0b4
git rebase
: Reapply commits on top of another base tip.# Moves the commits from your branch on top of another branch (e.g., main). git checkout feature/user-profile git rebase main
git stash
: Stash the changes in a dirty working directory away.# Temporarily saves your uncommitted changes. git stash # Applies the last stashed changes back to your working directory. git stash pop
git reflog
: Manage reflog information.# Shows a log of all branch updates. A safety net for recovering lost commits. git reflog
git cherry-pick
: Apply the changes introduced by some existing commits.# Applies the changes from a specific commit onto your current branch. git cherry-pick 7b3e0b4
7. Other Commands
git tag
: Create, list, delete or verify a tag.# Creates a lightweight tag for a specific point in history, often a release version. git tag v1.0.0
git worktree
: Manage multiple working trees attached to the same repository.# Creates a new working directory for a specific branch without cloning the entire repository again. git worktree add ../hotfix-branch hotfix/login-bug
git clean
: Remove untracked files from the working tree.# A dangerous command! Use --dry-run first to see what will be deleted. git clean -n # Deletes untracked files and directories. git clean -df
Subscribe to my newsletter
Read articles from Divakar Chakali directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
