Common git commands

Source: Internet
Author: User
Tags git commands

1. Create a code library

# Create a git code library in the current directory

$ Git init

# Create a directory and initialize it as a git code library

$ Git init [project-name]

# Download a project and its entire code history

$ Git clone [url]

Ii. Configuration

The GIT setting file is. gitconfig, which can be in the user's main directory (global configuration) or project directory (project configuration ).

# Display the current git Configuration

$ Git config -- list

# Edit the GIT configuration file

$ Git config-E [-Global]

# Configure the user when submitting code

$ Git config [-Global] user. Name "[name]"

3. Add/delete files

# Add a specified file to the temporary storage Zone

$ Git add [file1] [file2]...

# Add a specified directory to the temporary storage area, including subdirectories

$ Git add [dir]

# Add all files in the current directory to the temporary storage Zone

$ Git add.

# Confirmation is required before each change is added.

# Multiple changes to the same file can be submitted in multiple stages

$ Git add-P

# Delete the workspace file and put it in the temporary storage zone.

$ Git RM [file1] [file2]...

# Rename the file and place it in the temporary storage area

$ Git MV [file-original] [file-renamed]

4. Code submission

# Submit the temporary storage area to the warehouse area

$ Git commit-M [Message]

# Submit a specified file for the temporary storage area to the warehouse area

$ Git commit [file1] [file2]... -- M [Message]

# Submit the changes in the workspace since the previous commit and directly go to the warehouse

$ Git commit-

# Display all diff information when submitting

$ Git commit -- V

# Use a new commit instead of the previous commit

# If the Code does not change, it is used to rewrite the commit information of the previous commit.

$ Git commit -- amend-M [Message]

# Redo the previous commit and include new changes to the specified file

$ Git commit-amend [file1] [file2]...

V. Branch

# List all local branches

$ Git Branch

# List all remote branches

$ Git branch-R

# List all local branches and remote branches

$ Git branch-

# Create a new branch, but remain 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 tracing 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 the previous Branch

$ Git checkout-

# Establish a tracing relationship between an existing branch and a specified remote Branch

$ Git Branch -- Set-upstream [branch] [Remote-branch]

# Merge the specified branch to the current Branch

$ Git merge [branch]

# Select a commit and merge it into the current Branch

$ Git cherry-pick [Commit]

# Deleting a branch

$ Git branch-d [branch-name]

# Deleting a remote Branch

$ Git push Origin -- delete [branch-name]

$ Git branch-Dr [remote/branch]

Vi. Labels

# List all tags

$ Git tag

# Create a tag at the current commit

$ Git tag [tag]

# Create a tag at the specified commit

$ Git tag [tag] [Commit]

# Deleting a local tag

$ Git tag-d [tag]

# Deleting a remote tag

$ Git push origin: refs/tags/[tagname]

# Viewing Tag Information

$ Git show [tag]

# Submit a specified tag

$ Git push [remote] [tag]

# Specify all tags

$ Git push [remote] -- tags

# Create a branch pointing to a tag

$ Git checkout-B [branch] [tag]

VII. View information

# Displaying changed files

$ Git status

# Display the version history of the current Branch

$ Git log

# Display the commit history and the files with each commit change

$ Git log-stat

# Search submission history based on keywords

$ Git log-s [keyword]

# Display all changes after a commit. Each commit occupies one row.

$ Git log [tag] Head-pretty = format: % s

# Show All changes after a commit. The "Submit description" must meet the search criteria

$ Git log [tag] Head-grep feature

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

$ Git log-follow [file]

$ Git whatchanged [file]

# Display each diff related to a specified file

$ Git log-P [file]

# Show the last five submissions

$ Git log-5-pretty-Online

# Display all submitted users, sorted by the number of submissions

$ Git commit log-Sn

# Display the time when the specified file was modified.

$ Git blame [file]

# Display the differences between the temporary storage zone and the Workspace

$ Git diff

# Display the differences between the temporary storage zone and the previous commit

$ Git diff-cached [file]

# Display the differences between the workspace and the latest commit of the current Branch

$ Git diff head

# Display the differences between two submissions

$ GTI diff [first-branch]... [second-branch]

# Show how many lines of code you have written today

$ Git diff-checkout stat "@ {0 day ago }"

# Display metadata and content changes submitted

$ Git show [Commit]

# Display a file that has been submitted for change

$ Git show-name-only [Commit]

# Display the content of a file when a file is submitted

$ Git show [Commit]: [filename]

# Display the latest submissions of the current Branch

$ Git reflog

8. Remote Synchronization

# Download all changes to the remote Repository

$ Git fetch [remote]

# Display all remote Repositories

$ Git remote-V

# Display the information of a remote warehouse

$ Git remote show [remote]

# Add a new remote repository and name it

$ Git remote add [shortname] [url]

# Retrieve remote repository changes and merge them with local branches

$ Git pull [remote] [branch]

# Upload a specified local branch to a remote Repository

$ Git push [remote] [branch]

# Forcibly push the current Branch to a remote warehouse, even if there is a conflict

$ Git push [remote]-Force

# Push all branches to a remote Repository

$ Git push [remote]-All

9. Undo

# Restore a specified file in the temporary storage area to the Workspace

$ Git checkout [file]

# Restore a specified commit file to the temporary storage area and workspace.

$ Git checkout.

# Reset the specified file in the temporary storage area, which is consistent with the previous commit, but the workspace remains unchanged.

$ Git reset [file]

# Reset the specified file in the temporary storage area to be consistent with the previous commit.

$ Git reset-hard

# Reset the pointer of the current branch to specify the commit and reset the temporary storage area, but the workspace remains unchanged.

$ Git reset [Commit]

# Reset the head of the current branch to specify the commit, and reset the temporary storage area and workspace at the same time, consistent with the specified commit

$ Git reset-hard [Commit]

# Reset the current head to the specified commit, but keep the temporary storage area and workspace unchanged.

$ Git reset-keep [Commit]

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.