Essential Git Commands Every Software
Developer Should Master for Version Control

Time Square
1

Setup and Config Commands

  1. git config Set user information, editor, and other settings.
    1. To set global username:
      git config --global userName "Your Name"
    2. To set global email:
      git config --global userEmail "your.email@example.com"
    3. To list all configuration settings:
      git config --list
    4. To set vim as the default editor:
      git config --global core.editor "vim"
    5. To set VSCode as the default editor:
      git config --global core.editor "code --wait"
  2. git help Access help documentation for Git commands.
    1. To get help for a specific command:
      git help <command>
    2. To help with merge command, for example:
      git help merge
    3. To list all available Git commands:
      git help -a

Cloning and Creating Projects Commands

  1. git init Initialize a new Git repository.
    1. To create a new repository:
      git init <directory-name>
    2. To create a bare repository (for servers):
      git init --bare
  2. git clone Clone a remote repository to the local machine.
    1. To clone a repository:
      git clone <url> <directory>
    2. To clone only the latest commit:
      git clone --depth=1 <url>

Basic Snapshot Commands

  1. git add Stage changes to be committed.
    1. To add a specific file:
      git add <file>
    2. To add all files:
      git add .
    3. To add changes interactively in patches:
      git add -p
  2. git status Show the working tree status.
    1. To show the status of the working tree:
      git status
    2. To show the status in short format:
      git status -s
    3. To show ignored files:
      git status --ignored
  3. git diff Show changes between commits, branches and working tree, etc.
    1. To show unstaged changes:
      git diff
    2. To show changes changes staged for commit:
      git diff --staged
    3. To compare between two branches:
      git diff <branch1> <branch2>
  4. git commit Record changes to the repository.
    1. To commit all staged changes with a message:
      git commit -m "message"
    2. To modify the last commit:
      git commit --amend
    3. To amend the last commit without changing the message:
      git commit --no-edit
  5. git reset Reset current HEAD to a specific state.
    1. To unstage a file:
      git reset <file>
    2. To keep changes in the working directory:
      git reset --soft <commit>
    3. To discard changes in the working directory:
      git reset --hard <commit>

Branching and Merging Commands

  1. git branch List, create, delete, or rename branches.
    1. To list all branches:
      git branch
    2. To create a new branch:
      git branch <branch-name>
    3. To delete a branch:
      git branch -d <branch-name>
    4. To rename a branch:
      git branch -m <old-branch-name> <new-branch-name>
    5. To show the current branch:
      git branch --show-current
  2. git checkout Switch branches or restore working tree files.
    1. To switch to a branch:
      git checkout <branch-name>
    2. To create and switch to a new branch:
      git checkout -b <branch-name>
    3. To discard changes in the working directory:
      git checkout -- <file>
  3. git merge Merge changes from one branch into another.
    1. To merge a branch into the current branch:
      git merge <branch-name>
    2. To merge changes into the current branch but do not commit immediately:
      git merge --squash <branch-name>
    3. To create a merge commit even if a fast-forward merge is possible:
      git merge --no-ff
  4. git rebase Merge changes from one branch into another.
    1. To rebase the current branch onto another branch:
      git rebase <branch-name>
    2. To rebase interactively to edit commit history:
      git rebase --interactive
    3. To the rebase and reset to the original branch:
      git rebase --abort
  5. git stash Save changes temporarily without committing.
    1. To save changes with a message:
      git stash save "message"
    2. To apply changes from the stash and remove it from the stash list:
      git stash pop
    3. To list all stashes:
      git stash list

Sharing and Updating Projects

  1. git fetch Download objects and refs from a remote repository.
    1. To fetch changes from a remote repository:
      git fetch origin
    2. To fetch all changes from all remotes:
      git fetch --all
  2. git pull Fetch and merge changes from a remote repository.
    1. To pull changes from a specific branch:
      git pull origin <branch-name>
    2. To rebase the local branch with the upstream branch:
      git pull --rebase
  3. git push Push local changes to a remote repository.
    1. To push the current branch to origin:
      git push origin <branch-name>
    2. To force push changes, overwriting history:
      git push --force
  4. git remote Manage remote repository connections.
    1. To add a remote repository:
      git remote add <name> <url>
    2. To list all remote repositories/URLs:
      git remote -v
    3. To remove a remote repository:
      git remote remove <name>
  5. git submodule Manage submodules in a Git repository.
    1. To add a submodule:
      git submodule add <repository>
    2. To clone and initialize submodules:
      git submodule update --init
    3. To show the status of submodules:
      git submodule status

Inspection and Comparison Commands

  1. git log Show the commit history.
    1. To show the commit history:
      git log
    2. To show the commit history in one line:
      git log --oneline
    3. To show a graphical representation of the commit history:
      git log --graph
    4. To show the changes introduced in each commit:
      git log -p
  2. git show Show details of a commit or object.
    1. To show the details of a commit:
      git show <commit>
    2. To show the details of a commit:
      git show HEAD
  3. git diff Show changes between commits, branches, or working tree.
    1. To show unstaged changes:
      git diff
    2. To show changes staged for commit:
      git diff --staged
    3. To compare between two branches:
      git diff <branch1> <branch2>
    4. To show only the names of changed files:
      git diff --name-only

Patching Commands

  1. git cherry-pick Apply changes from a specific commit.
    1. To apply a specific commit to the current branch:
      git cherry-pick <commit>
    2. To abort an ongoing cherry-pick operation:
      git cherry-pick --abort
  2. git apply Apply a patch to the working directory.
    1. To apply from a patch file:
      git apply <patch-file>
    2. To check if a patch can be applied cleanly:
      git apply --check <patch-file>

Debugging Commands

  1. git bisect Find the commit that introduced a bug.
    1. To start the bisect process:
      git bisect start
    2. To mark a commit as good:
      git bisect good <commit>
    3. To mark a commit as bad:
      git bisect bad <commit>
  2. git blame Show who made changes to each line of a file.
    1. To show changes by line in a file:
      git blame <file>
    2. To limit the blame to a range of lines:
      git blame -L <start>,<end> <file>
  3. git grep Search for patterns in tracked files.
    1. To search for a pattern in the current branch:
      git grep "pattern"
    2. To search for a pattern with line numbers for a match:
      git grep -n "pattern"