Help
Display help message for a specific git subcommand:
Use one of the following commands:
git <verb> --help git help <verb> man git-<verb>Display quick help message for a specific git subcommand
git <verb> -h
Repository Setup
Initialize a new repository.
git init <directory>- Omit
<directory>to initialize in the current directory.
- Omit
Initialize a new repository in the current directory and commit all files.
git init git add . git commit -m "Initial commit"Clone a repository.
git clone <repository-url> <directory>- Omit
<directory>to clone into a directory with the same name as the remote repository, inside the current directory.
- Omit
Configuration
Specify configuration scope:
git config --local git config --global git config --systemList all configurations:
git config --list--show-origin: Show origin (file path) of each configuration.--show-scope: Show scope of each configuration.--name-only: Show only names of configurations (without values).
Get value of a specific configuration:
git config --get <key>Set value of a specific configuration:
git config <key> <value>Remove a specific configuration:
git config --unset <key>Common global settings.
Set global user name:
git config --global user.name <your_name>Set global user email:
git config --global user.email <your_email>Set global editor:
git config --global core.editor <editor>
Branches
List branches.
git branchList only local branches by default.
-ror--remote: Show only remote branches.-a | --all: Show all branches (local and remote).-vor--verbose: Show more information about branches.-vvor--verbose --verbose: Show more information about branches and their upstream branches.--list <pattern>: Show branches that match the<pattern>.
Create a new branch from the current branch:
git branch <branch_name>Switch to a branch:
git checkout <branch_name>git switch <branch_name> # Git 2.23+ | ExperimentalCreate a new branch and switch to it:
-bor--branch.git checkout -b <branch_name>Force create a new branch, replacing an existing branch:
git checkout -B <branch_name>Rename a branch:
-mor--move.git branch -m <old_branch_name> <new_branch_name>Delete a branch:
-dor--delete. Deleted only if it is merged into its upstream branch, orHEADif no upstream branch is set.git branch -d <branch_name>Force delete a branch:
git branch -D <branch_name>Delete a remote branch:
git push <remote> --delete <branch_name>Set upstream branch:
-uor--set-upstream-to.git branch -u <upstream_branch>Remove upstream branch:
git branch --unset-upstream
Status and Information
Show status of working directory and staging area:
git status-sor--short: Show status in short format.-bor--branch: Show status along with branch information in short format.-u<mode>or--untracked-files=<mode>: Show/hide untracked files.no: Do not show untracked files.normal: Show untracked files and directories.all: Also shows individual files in untracked directories.
-vor--verbose: Show diff of staged changes.-vvor--verbose --verbose: Show diff of staged and unstaged changes.<files>: Show status of a specific file.<directories>: Show status of files in a specific directory.<pattern>: Show status of files that match the<pattern>.
Short format characters:
: UnmodifiedM: ModifiedT: File type changed (regular file, symbolic link or submodule)A: AddedD: DeletedR: RenamedC: CopiedU: Updated but unmerged?: Untracked!: Ignored
Show changes.
In working directory (unstaged changes):
git diffIn staging area (staged changes):
--stagedor--cachedgit diff --stagedAll changes (both staged and unstaged):
git diff HEADBetween two commits:
git diff <first-commit> <second-commit>Options for
git diff:--stat: Show statistics.--name-only: Show only names of changed files.--name-status: Show names and status of changed files.<files>: Show status of a specific file.<directories>: Show status of files in a specific directory.<pattern>: Show status of files that match the<pattern>.
Open a visual diff tool:
git difftoolShow author and date of last change for each line in a file:
git blame <file>Show changes in a specific file in the last commit:
git show <file>Show changes in a specific commit and its parent:
git show <commit>Show content of a file in a specific commit:
git show <commit>:<file>Show full change history:
git logShow full change history of a file.
Show commits that changed a file:
git log --follow -- <file>Show commits and changes that changed a file:
git log -p -- <file>List ignored files:
git check-ignore <pathname>*: List all ignored files at the root level.**: List all ignored files recursively.<pathname>: List ignored files in a specific directory.-vor--verbose: Show ignored files along with the rule that ignores them.-nor--non-matching: Show files that are not ignored. Use with-vto differentiate between ignored and non-ignored files.
Changes
Add changes from working directory to staging area:
git add <files>.: Add new and modified files in the current directory and its subdirectories. Does not add deleted files.-Aor--all: Add all changes, including untracked files and deleted files, in the working directory.-uor--update: Add modified and deleted files, but not untracked files.<files>: Add specific files.<directories>: Add all files in the specified directories.<pattern>: Add files that match the<pattern>.
Reset changes in the staging area and keep them in the working directory:
git reset<files>: Reset changes in specific files.<directories>: Reset changes in all files in the specified directories.<pattern>: Reset changes in files that match the<pattern>.
Reset all changes in the staging area and working directory to the last commit:
git reset --hard
Commits
Commit changes in the staging area:
git commit-m <message>or--message=<message>: Add a message to the commit. If not provided, opens the default editor to add a message.-aor--all: Add all changes to the commit.-vor--verbose: Show changes in the commit message.-qor--quiet: Do not show commit summary after commit.--amend: Amend the last commit.
Delete last commit:
git reset HEAD~1Moves the current branch HEAD to the previous commit.
--soft: Do not alter uncommitted changes in staging area and working directory. Add changes from previous commit to the working directory.--mixed: Move all staged changes to working directory. Add changes from previous commit to the working directory.--hard: Discard all changes including uncommitted changes in staging area and working directory.
Push
Push changes to a remote repository:
git push <remote> <branch>- Omit
<branch>to push changes to the current branch. - Omit
<remote>to push changes to the default remote repository. -nor--dry-run: Show what would be pushed, without pushing.--forceor-f: Force push.-uor--set-upstream: Set upstream branch.
- Omit
Fetch
Fetch changes from a remote repository:
git fetch <remote>- If
<remote>is omitted, uses upstream of the current branch, else usesorigin. --all: Fetch changes from all remotes.--dry-run: Show what would be fetched.--prune: Remove remote tracking branches that no longer exist on the remote.-tor--tags: Fetch all tags.
- If
Pull
Pull changes from a remote repository:
git pull <remote> <branch>- Omit
<branch>to pull changes from the upstream branch. - Omit
<remote>to pull changes from the default remote repository. --rebase: Rebase changes instead of merging.--ff-only: Allow only fast-forward merges.--no-rebase: Merge changes instead of rebasing.
- Omit
Merge
Merge changes from a
<feature_A>into the<development>branch:git checkout <development> git merge <feature_A> git push origin <development> # Delete the feature branch--no-ff: Create a merge commit even if fast-forward is possible.--ff-only: Allow only fast-forward merges.--squash: Merge changes into a single commit.
Rebase
Rebase changes from a
<feature_A>onto the<development>branch:git checkout <feature_A> git rebase <development> git checkout <development> git merge <feature_A> git branch -d <feature_A> git push origin <development>Interactive rebase:
-ior--interactive.git rebase -i <commit><commit>: Start rebase from a specific commit.rorreword: Change commit message.eoredit: Change commit.sorsquash: Combine commit with previous commit.forfixup: Combine commit with previous commit without keeping the commit message.dordrop: Remove commit.porpick: Keep commit as is.
Continue a rebase after resolving conflicts:
git rebase --continueSkip a commit during rebase:
git rebase --skipAbort a rebase:
git rebase --abort
Remote Repositories
List remote repositories:
git remote-vor--verbose: Show URLs of remote repositories.
Add a remote repository:
git remote add <name> <url>Remove a remote repository:
git remote remove <name>Rename a remote repository:
git remote rename <old_name> <new_name>Show information about a remote repository:
git remote show <name>Get URLs of a remote repository.
git remote get-url <name>--push: Get push URL.
Set URLs of a remote repository.
git remote set-url <name> <url>--push: Set push URL.
Tags
List tags:
git tag-l <pattern>or--list=<pattern>: Show tags that match the<pattern>.--sort=<key>: Sort tags by<key>.refname: Sort by tag name.version:refnameorv:refname: Sort by version number, when tag name is a version number.taggerdate: Sort by tagger date. (annotated tags only)committerdate: Sort by tag commit date of the referenced commit.creatordate: Sort by tag creation date of the tag object. (annotated tags only)- Prefix with
-for reverse order. - Use tab key for autocompletion.
--merged: Show tags that are merged into the current branch.--no-merged: Show tags that are not merged into the current branch.--contains <commit>: Show tags that contain a specific commit.-aor--annotate: Show annotated tags.
Create a lightweight tag:
git tag <tag_name><commit>: Tag a specific commit.
Create an annotated tag:
-aor--annotate.git tag -a <tag_name>-m <message>or--message=<message>: Add a message to the tag. If not provided, opens the default editor to add a message.<commit>: Tag a specific commit.
Push tags to a remote repository:
git push <remote> <tag>--tags: Push all tags. Omit<tag>to push all tags.
Delete a tag:
Local tag:
git tag -d <tag_name>Remote tag:
git push <remote> --delete <tag_name>Show information about a tag:
git show <tag_name>
Stash
List stashes:
git stash listStash changes:
Staged and unstaged changes:
git stash push-uor--include-untracked: Stash staged changes, unstaged changes and untracked files.-aor--all: Stash staged changes, unstaged changes, untracked files and ignored files.-Sor--staged: Stash only staged changes.-kor--keep-index: Keep staged changes after stashing.-m <message>or--message=<message>: Add a message to the stash.
Apply stash and keep it in the stash list.
git stash applystash@{n}: Apply a specific stash.
Apply stash and remove it from the stash list.
git stash popstash@{n}: Apply a specific stash.
Show changes in a stash:
git stash showstash@{n}: Show changes in a specific stash.
Delete stash:
git stash dropstash@{n}: Delete a specific stash.
Clear all stashes:
git stash clear
Ignore Files
Create a
.gitignorefile:touch .gitignoreAdd files and directories to ignore:
echo <pattern> >> .gitignore# <comment>: Add comments. Use\#for patterns that start with#.\: Include a space at the end of a line.!: Do not ignore a file or directory, ignored by a previous pattern. Use\!for patterns that start with!. Files inside ignored directories are always ignored.- Patterns with
/at beginning or in middle are relative to the directory level of the particular.gitignorefile. Otherwise, they can match at any level below the.gitignorefile. - Pattern ending with
/matches directories only. *: Matches anything except/.?: Matches any single character, except/.[a-zA-Z]: Matches any character in the range.- Leading
**/: Matches in all directories. - Trailing
/**: Matches everything inside a directory. /**/: Zero or more directories in a path.
