This repository is used to record some git commands which are frequently used.
If you like this tips, don't forget to give a star! 🌟
- Setup and Config
- Getting and Creating Projects
- Basic Snap Shotting
- Branching and Merging
- Sharing and Updating Projects
- Patching
- Debugging
- Administration
Set and get git global or repository settings
# Set global user settings
$ git config --global user.name xxx
$ git config --global user.email xxx@mail.com
# Set repository user settings
$ git config user.name xxx
$ git config user.email xxx@mail.com
# Remove settings
$ git config --unset user.name
# Get settings
$ git config --global --list
$ git config --list
# Use alias
$ git config --global alias.br branch
# Avoid line ending problem under cross platform development
$ git config --global core.autocrlf true
Useful Alias
alias.br=branch
alias.chp=cherry-pick
alias.fp=fetch --prune
alias.cm=commit
alias.co=checkout
alias.st=status
alias.lg=log --oneline --graph
alias.la=log --graph --all --pretty=format:"%C(auto)%h -%d %s%Creset %Cblue(%cr)%Creset %C(dim white)<%an>%Creset"
Create an empty git repository or reinitialize an existing one
$ git init
Clone a repository into a new directory
$ git clone <directory>
Show the working tree status
$ git status
Add file contents to the index
$ git add <file_path>
# Add all un-staged files in the index
$ git add .
Record changes to repository
$ git commit
$ git commit -m <commit_message>
# Add and commit all unstaged files with commit message
$ git commit -am <commit_message>
Show changes between commits, commit and working tree, etc.
$ git diff <file_path>
$ git diff HEAD~1
$ git diff <commit-1> <commit-2>
# Compare the difference between index changes and last commit
$ git diff --cached <file_path>
Restore working tree files
$ git restore <file_path>
# Restore staged files
$ git restore --staged <file_path>
Reset current HEAD to specified state
$ git reset [--soft | --mixed | --hard] <commit>
List, create, or delete branches
# List all branches in your repo, and which branch you're currently in
$ git branch
# Create a branch
$ git branch <branch_name>
# Delete the branch
$ git branch -d <branch_name>
# Delete the branch (FORCE)
$ git branch -D <branch_name>
# Change the branch name
$ git branch -m <older_branch_name> <new_branch_name>
Switch branches or restore working tree files
# Switch to another branch
$ git checkout <branch_name>
# Create a new branch & switch to it
$ git checkout -b <branch_name>
# Cancel the changed file
$ git checkout -- <file>
Join two or more histories together
$ git merge <branch>
# Merge without fast-forward
$ git merge --no-ff <branch>
Show commit logs
$ git log
# Specify date
$ git log --before=<date> --after=<date>
# Specify author
$ git log --author=<author_name>
Stash the changes in a dirty working directory away
$ git stash
# List out current stash
$ git stash list
# Push the files wanna stash
$ git stash push
# Output the stashed file
$ git stash apply
# Output specific stashed file
$ git stash apply <stash>
# Remove a single stashed state & apply it on top of current working tree state
$ git stash pop
# Remove a single stash entry from the list of stash entries
$ git stash drop <stash>
Create, list, delete or verify a tag object signed with GPG
$ git tag <tag_name>
# Delete a tag
$ git tag -d <tag_name>
# Delete all local tags
$ git tag -l | xargs git tag -d
Download objects and refs from another repository
$ git fetch
# Fetch all remote
$ git fetch --all
# Before fetching, remove any remote-tracking references that no longer exist on the remote
$ git fetch --prune
# Fetch & rebase branch without checkout
$ git fetch origin <remote_branch>:<local_branch>
Update remote refs along with associated objects
$ git push
# Add upstream reference
$ git push [-u | --set-upstream] <remote_name> <repository_url>
Manage set of tracked repositories
# Show remote url after name
$ git remote -v
# Add remote
$ git remote add <remote_name></remote_name> <repository_url>
# Remove remote
$ git remote remove <remote_name> <repository_url>
# Change remote URL
$ git remote set-url <remote_name> <repository_url>
# Add a submodules
$ git submodule add <repository> <path>
# Occasionally update the submodule to a new version
$ git -C <path> checkout <new-version>
$ git add <path>
$ git commit -m "update submodule to new version"
# Check submodules status
$ git submodule status
# Cloning or pulling a repository containing submodules
$ git submodule init
$ git submodule update
# Shorthand
$ git submodule update --init --recursive
# Remove submodules
$ git rm <path>
Apply the changes introduced by some existing commits
$ git cherry-pick <commit_hash_1> <commit_hash_2> ...
# Pick without commit
$ git cherry-pick <commit_hash_1> --no-commit
Reapply commits on top of another base tip
$ git rebase [-i | --interactive] <new_base>
Revert some existing commits
$ git revert
Print lines matching a pattern
# Search with line number
$ git grep -n <search_text>
Manage & track reflog information
$ git reflog
Open a Git repository browser
$ gitk
# Show all references (branches, tags, etc.)
$ gitk --all