Nanyi
Date: December 9, 2015
I use Git every day, but I can't remember many commands.
In general, the daily use of just remember 6 commands, you can. But skilled use, I am afraid to remember 60~100 a command.
Here's a list of common Git commands I've compiled. Several special nouns are translated as follows.
- Workspace: Work Area
- Index/stage: Staging Area
- Repository: Warehouse area (or local warehouse)
- Remote: Repository
First, create a new code base
# 在当前目录新建一个Git代码库$ git init# 新建一个目录,将其初始化为Git代码库$ git init [project-name]# 下载一个项目和它的整个代码历史$ git clone [url]
Second, the configuration
Git's settings file is .gitconfig
, it can be in the user's home directory (global configuration), or under the project directory (project configuration).
# 显示当前的Git配置$ git config --list# 编辑Git配置文件$ git config -e [--global]# 设置提交代码时的用户信息$ git config [--global] user.name "[name]"$ git config [--global] user.email "[email address]"
Third, add/delete files
# Add the specified file to staging area $ git add[File1][File2]...# Add the specified directory to staging area, including subdirectories $ git add[dir]# Add all files of the current directory to staging area $ git add.# before adding each change, you will be asked to confirm# for multiple changes to the same file, you can implement the sub-commit $ git add- p# to delete the workspace file and put this deletion into staging area $ git rm [file1] [file2] . . # Stop tracking the specified file, but the file will remain in the workspace $ git rm --cached [File]# Renaming files and put this name into staging area $ git mv [File- Original] [file-renamed]
Iv. Code Submission
# Commit staging Area to warehouse area $ git commit-M[Message]# Commit Staging area file to warehouse area $ git commit[File1][File2]...-M[Message]# commit workspace changes since last commit, go directly to warehouse area $ git commit-a # commit when displaying all diff information $ git commit -v# Use a new commit, Override last Commit # If the code does not have any new changes, it is used to overwrite the commit information of the last commit $ git commit --amend- m [message]# Redo the last commit, and includes a new change to the specified file $ git commit --amend [file1] [file2] . .
V. Branches
# List all local branches $ git branch# list all remote branches $ git branch-R# list all local branches and remote branches $ git branch-A# Create a new branch, but still stay on the current branch $ git branch[Branch-name]# Create a new branch and switch to the branch $ git checkout-B[Branch]# Create a new branch, point to specify commit$ git branch[Branch][Commit]# Create a new branch to 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 specified branch to current branch $ git merge [branch]# Select a commit, merge into current branch $ git cherry-pick [Commit]# Delete Branch $ Git branch- d [branch-name]# Remove remote branch $ Git push origin --delete [branch-name]$ Git branch -dr [remote/branch]
Six, label
# list all tag$ git tags# 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 info $ git show [tag< Span class= "token punctuation" >]# submit specified tag$ git push [ Remote [tag]< Span class= "token comment" ># submit all tag$ git push [remote --tags# create a new branch, Point to a tag$ git checkout -b [branch" [tag
Vii. Viewing information
# show changed files $ git status# shows the version history of the current branch $ git log# Show commit history, and file $ git log for each commit change--stat# Search Commit history, according to keyword $ git log-S[keyword]# shows all changes after a commit, each of which occupies one line of git log[Tag] HEAD--pretty=format:%s# shows all changes after a commit, and its "submit description" must match the search criteria $ git log[Tag] HEAD--grep feature# shows the version history of a file, including file name rename $ git log--follow[File]$ git whatchanged[File]# Displays each time diff$ git log associated with the specified file-P[File]# Show last 5 commit $ git log-5--pretty--oneline# Show all submitted users, sort by commits $ git shortlog-sn# shows what the specified file is and at what time has the git blame been modified.[File]# Show staging area and Workspace differences $ git diff# Show staging area and previous commit differences $ git diff--cached[File]# shows the difference between the workspace and the current branch of the latest commit $ git diff HEAD# show the difference between two commits $ git diff[First-branch]...[Second-branch]# shows how many lines of code you wrote today $ git diff --shortstat "@{0 day ago}"# Show metadata and content changes for a commit $ git show [commi T]# shows a file that has been changed by a commit $ git show --name-only [Commit]# shows a commit when a file's contents $ git show [ Commit]:[filename]# shows the last few commits of the current branch $ git reflog
Eight, remote synchronization
# Download all changes to the remote repository $ git fetch[Remote]# Show all remote warehouses $ git remote-V# Show information for a remote repository $ git remote show[Remote]# Add a new remote repository and name $ git remote add[ShortName] [url]# Retrieve remote repository changes and merge with local branch $ git pull [remote] [branch]# upload locally specified branch to remote warehouse $ git push [ remote] [branch]# forcibly pushes the current branch to the remote repository, even if there is a conflict $ git push [remote] --force# Push all branches to remote repository $ git push [remote] --all
Ix. Revocation
# Restore staging area of the specified file to the workspace $ git checkout[File]# Restore the specified file of a commit to staging area and workspace $ git checkout[Commit][File]# Recover all files from staging area to workspace $ git checkout.# resets the specified file for staging area, consistent with the last commit, but the workspace is unchanged $ git reset[File]# Reset Staging area with workspace, consistent with last commit $ git reset--hard# Resets the current branch pointer to the specified commit while resetting the staging area, but the workspace is unchanged $ git reset [commit] # resetsthe current branch's head to the specified commit, while resetting the staging area and workspace , consistent with the specified commit $ git reset --hard [Commit]# Resets the current head to the specified commit, but keeps the staging area and workspace unchanged $ git reset --keep [ Commit]# Create a new commit to undo the specified commit# All changes to the latter will be offset by the former and applied to the current branch $ git revert [commit]# Temporarily remove uncommitted changes and later move into git stash$ git stash pop
X. Other
# 生成一个可供发布的压缩包$ git archive
List of common Git commands