List of commonly used Git commands (excerpt)

Source: Internet
Author: User
Tags diff git commands

Source: Nanyi's Blog

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

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

# Create a new Git code base in the current directory

$ git init

# Create a new directory and initialize it to a git code base

$ git init [project-name]

# Download a project and its entire code history

$ 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).

# shows the current git configuration

$ git config--list

# edit git config file

$ git config-e [--global]

# Set user information when submitting code

$ git config [--global] user.name "[Name]"

$ git config [--global] user.email "[Email address]"

Third, add/delete files

# Add specified file to staging area

$ git add [file1] [file2] ...

# Add the specified directory to staging area, including subdirectories

$ git add [dir]

# Add all files from the current directory to staging area

$ git Add.

# 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]

# Rename the file and put this name in staging area

$ git mv [file-original] [file-renamed]

Iv. Code Submission

# Submit staging Area to warehouse area

$ git commit-m [message]

# submit the specified file of staging area to the warehouse area

$ git commit [file1] [file2] ...-m [message]

# Submit Workspace changes since the last commit, directly to the warehouse area

$ git commit-a

# Show all diff information when submitting

$ git commit-v

# Use a new commit instead of the 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 include 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 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 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]

# Establish a tracking relationship between the existing branch and the specified remote branch

$ git Branch--set-upstream [branch] [Remote-branch]

# merge the specified branches into the current branch

$ git merge [branch]

# Select a commit to merge into the 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 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]

# 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, point to a tag

$ git checkout-b [branch] [tag]

Vii. Viewing information

# Show files with changes

$ git status

# shows the version history of the current branch

$ git log

# shows the commit history, and the file that each commit changed

$ git log--stat

# Displays the version history of a file, including file renaming

$ git log--follow [file]

$ git whatchanged [file]

# Displays each diff associated with the specified file

$ git log-p [file]

# shows who the specified file was and what time it was modified

$ 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 where a commit has changed

$ git show--name-only [commit]

# Displays the contents of a file when a commit is displayed

$ 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

# Display information for a remote warehouse

$ git remote show [remote]

# Add a new remote repository and name

$ git remote add [shortname] [url]

# Retrieve changes to the remote repository and merge with the local branch

$ git pull [remote] [branch]

# Upload locally designated branch to remote repository

$ 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 the remote repository

$ git push [remote]--all

Ix. Revocation

# Restore Staging area specified file to workspace

$ git checkout [file]

# Restore the specified file of a commit to the workspace

$ git checkout [commit] [file]

# Restore all files from the previous commit to the workspace

$ git checkout.

# resets the specified file for staging area, consistent with the last commit, but the workspace remains the same

$ git reset [file]

# Reset Staging area to 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 does not change

$ git reset [commit]

# resets the head of the current branch 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 intact

$ git reset--keep [commit]

# Create a new commit to revoke the specified commit

# All changes to the latter will be offset by the former and applied to the current branch

$ git revert [commit]

X. Other

# Generate a compressed package that can be published

$ git Archive

List of commonly used Git commands (excerpt)

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.