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 --system
List 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 branch
List only local branches by default.
-r
or--remote
: Show only remote branches.-a | --all
: Show all branches (local and remote).-v
or--verbose
: Show more information about branches.-vv
or--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+ | Experimental
Create a new branch and switch to it:
-b
or--branch
.git checkout -b <branch_name>
Force create a new branch, replacing an existing branch:
git checkout -B <branch_name>
Rename a branch:
-m
or--move
.git branch -m <old_branch_name> <new_branch_name>
Delete a branch:
-d
or--delete
. Deleted only if it is merged into its upstream branch, orHEAD
if 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:
-u
or--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
-s
or--short
: Show status in short format.-b
or--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.
-v
or--verbose
: Show diff of staged changes.-vv
or--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:
M
: 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 diff
In staging area (staged changes):
--staged
or--cached
git diff --staged
All changes (both staged and unstaged):
git diff HEAD
Between 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 difftool
Show 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 log
Show 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.-v
or--verbose
: Show ignored files along with the rule that ignores them.-n
or--non-matching
: Show files that are not ignored. Use with-v
to 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.-A
or--all
: Add all changes, including untracked files and deleted files, in the working directory.-u
or--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.-a
or--all
: Add all changes to the commit.-v
or--verbose
: Show changes in the commit message.-q
or--quiet
: Do not show commit summary after commit.--amend
: Amend the last commit.
Delete last commit:
git reset HEAD~1
Moves 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. -n
or--dry-run
: Show what would be pushed, without pushing.--force
or-f
: Force push.-u
or--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.-t
or--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:
-i
or--interactive
.git rebase -i <commit>
<commit>
: Start rebase from a specific commit.r
orreword
: Change commit message.e
oredit
: Change commit.s
orsquash
: Combine commit with previous commit.f
orfixup
: Combine commit with previous commit without keeping the commit message.d
ordrop
: Remove commit.p
orpick
: Keep commit as is.
Continue a rebase after resolving conflicts:
git rebase --continue
Skip a commit during rebase:
git rebase --skip
Abort a rebase:
git rebase --abort
Remote Repositories
List remote repositories:
git remote
-v
or--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:refname
orv: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.-a
or--annotate
: Show annotated tags.
Create a lightweight tag:
git tag <tag_name>
<commit>
: Tag a specific commit.
Create an annotated tag:
-a
or--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 list
Stash changes:
Staged and unstaged changes:
git stash push
-u
or--include-untracked
: Stash staged changes, unstaged changes and untracked files.-a
or--all
: Stash staged changes, unstaged changes, untracked files and ignored files.-S
or--staged
: Stash only staged changes.-k
or--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 apply
stash@{n}
: Apply a specific stash.
Apply stash and remove it from the stash list.
git stash pop
stash@{n}
: Apply a specific stash.
Show changes in a stash:
git stash show
stash@{n}
: Show changes in a specific stash.
Delete stash:
git stash drop
stash@{n}
: Delete a specific stash.
Clear all stashes:
git stash clear
Ignore Files
Create a
.gitignore
file:touch .gitignore
Add 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.gitignore
file. Otherwise, they can match at any level below the.gitignore
file. - 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.