Common git commands

Source: Internet
Author: User
Tags git commands

Original address: http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

 阮一峰日期: 2015年12月 9日

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

The settings file for Git is. Gitconfig, which 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
# 添加指定文件到暂存区$ git add [file1] [file2] ...# 添加指定目录到暂存区,包括子目录$ git add [dir]# 添加当前目录的所有文件到暂存区$ git add .# 删除工作区文件,并且将这次删除放入暂存区$ git rm [file1] [file2] ...# 停止追踪指定文件,但该文件会保留在工作区$ git rm --cached [file]# 改名文件,并且将这个改名放入暂存区$ git mv [file-original] [file-renamed]
Iv. Code Submission
# 提交暂存区到仓库区$ git commit -m [message]# 提交暂存区的指定文件到仓库区$ git commit [file1] [file2] ... -m [message]# 提交工作区自上次commit之后的变化,直接到仓库区$ git commit -a# 提交时显示所有diff信息$ git commit -v# 使用一次新的commit,替代上一次提交# 如果代码没有任何新变化,则用来改写上一次commit的提交信息$ git commit --amend -m [message]# 重做上一次commit,并包括指定文件的新变化$ 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 in the current branch $Git branch [Branch-name]# Create a new branch and switch to that branch $git checkout-b [branch]# Create a new branch, point to the specified commit$GIT branch [branch] [commit]# Create a new branch, establish a tracking relationship with the specified remote branch $ git branch--track [branch] [remote-branch]< Span class= "CO" ># switch to the specified branch and update the workspace $ git checkout [Branch-name] # 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] # Remove 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 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 info $ git show [tag]# commit specified tag$ g It push [remote] [tag]# commit all tag$ git push [remote]--tags# Create a new branch, point to a tag$ git checkout-b [branch] [t AG]                
Vii. Viewing information
# Show files with changes $git status# Displays the version history of the current branch $GitLog# shows the commit history, and the file that each commit changedGitLog--stat# shows all changes after a commit, with each commit occupying a single $GitLog [tag] HEAD--pretty=format:%s# shows all changes after a commit, and its "submit description" must match the search criteria $GitLog [tag] HEAD--grep feature# Displays the version history of a file, including the name of the file renamed $GitLog--follow [file]$git whatchanged [file]# Displays each diff$ associated with the specified fileGitlog-p [File]# shows what the specified file is and what time the person modified the $git blame [file]# Show Staging area and workspace differences $ git diff# shows the difference between staging area and the previous commit $ git diff--cached [file]# shows the difference between the workspace and the latest commit of the current branch $ git diff HEAD# shows the difference between two commits $ git diff [First-branch] ... [Second-branch] # Show metadata and content changes for a commit $ git show [commit]# shows a file that has changed during a commit $ git show--name-only [commit]# shows a commit The contents of a file $ git show [commit]:[filename]# shows the most recent 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]< c9># 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 repository $ git push [remote] [branch]# forcibly push the current branch to the remote repository, even if there is a conflict $ git push [remote]--force# pushes all branches to the far Process Warehouse $ git push [remote]--all             
Ix. Revocation
# Restore the specified file to the workspace $ staging Areagit checkout [file]# Restore the specified file of a commit to the workspace $git checkout [commit] [file]# Recover all files from last commit to workspace $git checkout. # Reset Staging area specified file, consistent with last commit, but workspace 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]# resets the 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 staging area and workspace unchanged $ git reset--keep [commit]# Create a new commit, All changes that are used to undo the specified commit# will be offset by the former and applied to the current branch $ git revert [commit]       
X. Other
# 生成一个可供发布的压缩包$ git archive

Finish

Common git commands

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.