Table of Contents
Setup and Config Commands
-
git config
Set user information, editor, and other settings.
-
To set global username:
git config --global userName "Your Name" -
To set global email:
git config --global userEmail "your.email@example.com" -
To list all configuration settings:
git config --list -
To set vim as the default editor:
git config --global core.editor "vim" -
To set VSCode as the default editor:
git config --global core.editor "code --wait"
-
To set global username:
-
git help
Access help documentation for Git commands.
-
To get help for a specific command:
git help <command> -
To help with merge command, for example:
git help merge -
To list all available Git commands:
git help -a
-
To get help for a specific command:
Cloning and Creating Projects Commands
-
git init
Initialize a new Git repository.
-
To create a new repository:
git init <directory-name> -
To create a bare repository (for servers):
git init --bare
-
To create a new repository:
-
git clone
Clone a remote repository to the local machine.
-
To clone a repository:
git clone <url> <directory> -
To clone only the latest commit:
git clone --depth=1 <url>
-
To clone a repository:
Basic Snapshot Commands
-
git add
Stage changes to be committed.
-
To add a specific file:
git add <file> -
To add all files:
git add . -
To add changes interactively in patches:
git add -p
-
To add a specific file:
-
git status
Show the working tree status.
-
To show the status of the working tree:
git status -
To show the status in short format:
git status -s -
To show ignored files:
git status --ignored
-
To show the status of the working tree:
-
git diff
Show changes between commits, branches and working tree, etc.
-
To show unstaged changes:
git diff -
To show changes changes staged for commit:
git diff --staged -
To compare between two branches:
git diff <branch1> <branch2>
-
To show unstaged changes:
-
git commit
Record changes to the repository.
-
To commit all staged changes with a message:
git commit -m "message" -
To modify the last commit:
git commit --amend -
To amend the last commit without changing the message:
git commit --no-edit
-
To commit all staged changes with a message:
-
git reset
Reset current HEAD to a specific state.
-
To unstage a file:
git reset <file> -
To keep changes in the working directory:
git reset --soft <commit> -
To discard changes in the working directory:
git reset --hard <commit>
-
To unstage a file:
Branching and Merging Commands
-
git branch
List, create, delete, or rename branches.
-
To list all branches:
git branch -
To create a new branch:
git branch <branch-name> -
To delete a branch:
git branch -d <branch-name> -
To rename a branch:
git branch -m <old-branch-name> <new-branch-name> -
To show the current branch:
git branch --show-current
-
To list all branches:
-
git checkout
Switch branches or restore working tree files.
-
To switch to a branch:
git checkout <branch-name> -
To create and switch to a new branch:
git checkout -b <branch-name> -
To discard changes in the working directory:
git checkout -- <file>
-
To switch to a branch:
-
git merge
Merge changes from one branch into another.
-
To merge a branch into the current branch:
git merge <branch-name> -
To merge changes into the current branch but do not commit immediately:
git merge --squash <branch-name> -
To create a merge commit even if a fast-forward merge is possible:
git merge --no-ff
-
To merge a branch into the current branch:
-
git rebase
Merge changes from one branch into another.
-
To rebase the current branch onto another branch:
git rebase <branch-name> -
To rebase interactively to edit commit history:
git rebase --interactive -
To the rebase and reset to the original branch:
git rebase --abort
-
To rebase the current branch onto another branch:
-
git stash
Save changes temporarily without committing.
-
To save changes with a message:
git stash save "message" -
To apply changes from the stash and remove it from the stash list:
git stash pop -
To list all stashes:
git stash list
-
To save changes with a message:
Sharing and Updating Projects
-
git fetch
Download objects and refs from a remote repository.
-
To fetch changes from a remote repository:
git fetch origin -
To fetch all changes from all remotes:
git fetch --all
-
To fetch changes from a remote repository:
-
git pull
Fetch and merge changes from a remote repository.
-
To pull changes from a specific branch:
git pull origin <branch-name> -
To rebase the local branch with the upstream branch:
git pull --rebase
-
To pull changes from a specific branch:
-
git push
Push local changes to a remote repository.
-
To push the current branch to origin:
git push origin <branch-name> -
To force push changes, overwriting history:
git push --force
-
To push the current branch to origin:
-
git remote
Manage remote repository connections.
-
To add a remote repository:
git remote add <name> <url> -
To list all remote repositories/URLs:
git remote -v -
To remove a remote repository:
git remote remove <name>
-
To add a remote repository:
-
git submodule
Manage submodules in a Git repository.
-
To add a submodule:
git submodule add <repository> -
To clone and initialize submodules:
git submodule update --init -
To show the status of submodules:
git submodule status
-
To add a submodule:
Inspection and Comparison Commands
-
git log
Show the commit history.
-
To show the commit history:
git log -
To show the commit history in one line:
git log --oneline -
To show a graphical representation of the commit history:
git log --graph -
To show the changes introduced in each commit:
git log -p
-
To show the commit history:
-
git show
Show details of a commit or object.
-
To show the details of a commit:
git show <commit> -
To show the details of a commit:
git show HEAD
-
To show the details of a commit:
-
git diff
Show changes between commits, branches, or working tree.
-
To show unstaged changes:
git diff -
To show changes staged for commit:
git diff --staged -
To compare between two branches:
git diff <branch1> <branch2> -
To show only the names of changed files:
git diff --name-only
-
To show unstaged changes:
Patching Commands
-
git cherry-pick
Apply changes from a specific commit.
-
To apply a specific commit to the current branch:
git cherry-pick <commit> -
To abort an ongoing cherry-pick operation:
git cherry-pick --abort
-
To apply a specific commit to the current branch:
-
git apply
Apply a patch to the working directory.
-
To apply from a patch file:
git apply <patch-file> -
To check if a patch can be applied cleanly:
git apply --check <patch-file>
-
To apply from a patch file:
Debugging Commands
-
git bisect
Find the commit that introduced a bug.
-
To start the bisect process:
git bisect start -
To mark a commit as good:
git bisect good <commit> -
To mark a commit as bad:
git bisect bad <commit>
-
To start the bisect process:
-
git blame
Show who made changes to each line of a file.
-
To show changes by line in a file:
git blame <file> -
To limit the blame to a range of lines:
git blame -L <start>,<end> <file>
-
To show changes by line in a file:
-
git grep
Search for patterns in tracked files.
-
To search for a pattern in the current branch:
git grep "pattern" -
To search for a pattern with line numbers for a match:
git grep -n "pattern"
-
To search for a pattern in the current branch: