Detailed list of Git various operation commands



Hello, I'm a passerby. For more high-quality articles, see my personal blog: http://itsoku.com

Generally speaking, if you use git daily, you only need to remember the 6 commands in the figure below.

But if you use it proficiently, you may have to remember 60 to 100 commands.

3b38831ba1519f3f63b8616147527874.png

Below is a list of commonly used Git commands. The translations of several proper nouns are as follows:

Workspace: Workspace
Index / Stage: temporary storage area
Repository: warehouse area (or local warehouse)
Remote: remote warehouse

1. Create a new code base

# Create a new Git repository in the current directory
$ git init

# Create a new directory and initialize it as a Git repository
$ git init [project-name]

# Download a project and its entire code history
$ git clone [url]

Second, configuration

Git’s setting file is .gitconfig, which can be in the user’s home directory (global configuration) or in the project directory (project configuration).

# Display the current Git configuration
$ git config --list

# Edit the Git configuration file
$ git config -e [--global]

# Set user information when submitting code
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

3. Add/delete files

# Add the specified file to the temporary storage area
$ git add [file1] [file2] ?…

# Add the specified directory to the temporary storage area, including subdirectories
$ git add [dir]

# Add all files in the current directory to the temporary storage area
$ git add .

# Confirmation will be asked before adding each change
# For multiple changes in the same file, it can be submitted in batches
$ git add -p

# Delete the workspace file, and put this deletion into the temporary storage area
$ git rm [file1] [file2] ?…

# Stop tracking the specified file, but the file will remain in the workspace
$ git rm --cached [file]

# Rename the file and put this rename into the temporary storage area
$ git mv [file-original] [file-renamed]

Fourth, code submission

# Submit the temporary storage area to the warehouse area
$ git commit -m [message]

# Submit the specified file in the temporary storage area to the warehouse area
$ git commit [file1] [file2] ... -m [message]

# Submit the changes in the workspace since the last commit, directly to the warehouse area
$ git commit -a

# Display all diff information when submitting
$ git commit -v

# Use a new commit to replace the previous commit
# If there is no new change in the code, it is used to rewrite the submission information of the last commit
$ git commit --amend -m [message]

# Redo the last commit and include the new changes for the specified files
$ git commit --amend [file1] [file2] ...

5. Branch

# list all local branches
$ git branch

# list all remote branches
$ git branch -r

# List all local and remote branches
$ git branch -a

# Create a new branch, but still stay in the current branch
$ git branch [branch-name]

# Create a new branch and switch to it
$ git checkout -b [branch]

# Create a new branch pointing to the specified commit
$ git branch [branch] [commit]

# Create a new branch and establish a tracking relationship with the specified remote branch
$ git branch --track [branch] [remote-branch]

# Switch to the specified branch and update the workspace
$ git checkout [branch-name]

# switch to previous branch
$ git checkout -

# Establish a tracking relationship between the existing branch and the specified remote branch
$ git branch --set-upstream [branch] [remote-branch]

# Merge the specified branch into the current branch
$ git merge [branch]

# Select a commit and merge it into the current branch
$ git cherry-pick [commit]

# delete branch
$ git branch -d [branch-name]

# delete remote branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

6. Label

# list all tags
$ git tag

# Create a new tag in the current commit
$ git tag [tag]

# Create a new tag in the specified commit
$ git tag [tag] [commit]

# delete local tag
$ git tag -d [tag]

# delete remote tag
$ git push origin :refs/tags/[tagName]

# View tag information
$ git show [tag]

# Submit the specified tag
$ git push [remote] [tag]

# Submit all tags
$ git push [remote] --tags

# Create a new branch pointing to a tag
$ git checkout -b [branch] [tag]

7. View information

# show changed files
$ git status

# Display the version history of the current branch
$ git log

# Display the commit history, and the files changed by each commit
$ git log --stat

# Search commit history, according to keywords
$ git log -S [keyword]

# Display all changes after a commit, each commit occupies one line
$ git log [tag] HEAD --pretty=format:%s

# Display all changes after a commit, whose "commit description" must meet the search criteria
$ git log [tag] HEAD --grep feature

# Display the version history of a file, including file renaming
$ git log --follow [file]
$ git whatchanged [file]

# Display every diff related to the specified file
$ git log -p [file]

# show last 5 commits
$ git log -5 --pretty --oneline

# Display all submitted users, sorted by the number of submissions
$ git shortlog -sn

# Display who and when the specified file was modified
$ git blame [file]

# Display the difference between the staging area and the working area
$ git diff

# Display the difference between the staging area and the previous commit
$ git diff --cached [file]

# Display the difference between the workspace and the latest commit of the current branch
$ git diff HEAD

# show the diff between two commits
$ git diff [first-branch]...[second-branch]

# Show how many lines of code you wrote today
$ git diff --shortstat "@{0 day ago}"

# Display metadata and content changes for a commit
$ git show [commit]

# Display files that have changed in a commit
$ git show --name-only [commit]

# Display the content of a file when a commit is made
$ git show [commit]:[filename]

# Display the last few commits of the current branch
$ git reflog

VIII. Remote synchronization

# Download all changes from the remote repository
$ git fetch [remote]

# show all remote repositories
$ git remote -v

# Display information about a remote warehouse
$ git remote show [remote]

# Add a new remote warehouse and name it
$ git remote add [shortname] [url]

# Retrieve the changes from the remote repository and merge them with the local branch
$ git pull [remote] [branch]

# Upload the local specified branch to the remote warehouse
$ git push [remote] [branch]

# Forcibly push the current branch to the remote warehouse, even if there is a conflict
$ git push [remote] --force

# Push all branches to the remote repository
$ git push [remote] --all

9. Cancellation

# Restore the specified file in the temporary storage area to the work area
$ git checkout [file]

# Restore the specified file of a commit to the temporary storage area and work area
$ git checkout [commit] [file]

# Restore all files in the temporary storage area to the work area
$ git checkout .

# Reset the specified file in the temporary storage area to be consistent with the last commit, but the work area remains unchanged
$ git reset [file]

# Reset the staging area and work area to be consistent with the last commit
$ git reset --hard

# Reset the pointer of the current branch to the specified commit, and reset the temporary storage area, but the work area remains unchanged
$ git reset [commit]

# Reset the HEAD of the current branch to the specified commit, and reset the temporary storage area and work area to be consistent with the specified commit
$ git reset --hard [commit]

# Reset the current HEAD to the specified commit, but keep the staging area and work area unchanged
$ git reset --keep [commit]

# Create a new commit to undo the specified commit
# All changes in the latter will be offset by the former and applied to the current branch
$ git revert [commit]

# Temporarily remove uncommitted changes and move them in later
$ git stash
$ git stash pop

Ten, others

# Generate a compressed package ready for distribution
$ git archive

Source: https://blog.csdn.net/zjhred/article/details/104951936

defe36b002292ad78e82f5caa2a9eece.png

Click to read the original text and go directly to my personal blog

2b0317a036752b790dadb9ebc4b7e36a.jpeg You are Do you want to see