Git Common Commands Summary

Source: Internet
Author: User

Definition: The following abbreviations are used in parentheses

Workspace: Workspace (W)

Staging Area: Index (I)

Local Warehouse: Repository (L)

Remote repository: Remotes Repository (R)

Second, commonly used commands:

1. Initialization

Git init

2. Common configuration information

(If you do not use--global, only the configuration information for the current project is set)

To set up a proxy:

git config--global http.proxy http://xxx.com:port

Set User name:

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

Set up your mailbox:

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

To set the command line color:

git config--global color.ui auto

View User name:

git config--global user.name

Configuration CRLF:

git config--global core.autocrlf [true/false/input]

(set to True under Windows, the submitted content is LF, Local is CRLF)

3. Add--from the working area W to staging area I

To add a single file/multiple files/directories:

git add [file1] [file2] [directory]

Add new and modified files, do not delete operations:

git Add.

Add modified and deleted files without adding new actions:

Git add-u

Add and delete files: equals git Add.; Git add-u;

Git add-a

4. Submission:

(1) From staging area I to local warehouse L

git commit-m [Submit message]

(2)

5. Revocation

(1) From staging area I to work area W

git rm--cached <file> ...

(2) Revert to head version

git reset--hard HEAD

(3) Modify the submitted user name information

git commit--amend--author= "author Name <[email protected]>"

6. Remote

(1) Change the remote warehouse address

git remote Set-url origin http://xxx.xx.com/x.git

(2) View Remote warehouse logs

git log origin/master

(3) Compare local and remote branches

git diff master Origin/master

(4) View Remote Branch

Git branch-r

(5) Pull remote

Method 1: Pull and merge

Git pull

Method 2: Merge after first pull

Git fetch origin

git merge Origin/master

7. Branch

To view branches:git branch

View Remote branch:git branch-r

To create a branch:git branch <name>

To switch branches:git checkout <name>

Create + switch Branches:git checkout -b <name>

Merge a branch to the current branch:git merge <name>

To delete a branch:git branch -d <name>

删除远程分支:Git push Origin--delete<name>

8. Storage

Save:git stash

View stash lists:git stash list

Recovery:git stash apply

Restore specific version:git stash apply [email protected]{2}

Clear a stash record:git stash drop [email protected]{0}

9. References

(1) View the submitted hash string

git log

git log--pretty=oneline

(2) View a submission

Git show 0c708f (hash)

(3) To view the hash of a branch, tag, or indirect reference

Git rev-parse Master

(4) View references

Under the. Git/refs Directory

Git show refs/heads/some-feature

There are some references under the. Git directory:

    • head– the commit or branch where the current is located.
    • fetch_head– the latest commit to fetch in the remote repository.
    • Orig_head–head the backup reference to avoid corruption.
    • merge_head– you git merge to refer to the current branch by merging them.
    • cherry_pick_head– cherry pick The reference you are using.

(5) Refspec

The definition of Refspec is this: [+]<src>:<dst> . The <src> parameter is the local source branch, which <dst> is the target branch of the remote. The optional + number forces the remote warehouse to adopt a non-fast forward update policy.

(6) Relative reference

git show head~2 grandfather node

git show head^2 the second parent node (when the branch is merged, the original branch is the first parent node)

git show head^2^1 can be multi-layered

(7) Reference log

Git reflog

git checkout [email protected]{1}

10. Code rollback

Reset checkout revert options

Reset checkout can specify a file or a commit, revert only for submission, not for file

Checkout revert is likely to rewrite the file before it is used to commit or cache modifications to the working directory

(1) Reset (only used on private branches, usually with HEADonly)

Git checkout Feature

git reset HEAD (end two commits become a suspension commit, git garbage collection will be deleted)

git reset--hard HEAD

    • --soft– buffers and working directories will not be changed.
    • --mixed– default options. The cache is synchronized with the commit you specified, but the working directory is not affected
    • --hard– buffers and working directories are synchronized to the commits you specify

git reset head file1.js adds the file in the head submission to the buffer , with no parameters such as--mixed for the file

(2) Checkout

Git checkout feature switch branches

git checkout head~2 move head to a specific commit, but it can cause head separation , very dangerous , if you add a new commit, Then after switching to another branch, there is no way to go back to the previously added commits. Therefore, you should create a new branch when adding a new commit to the detached head.

git checkout head~2 file1.js sync This file from your working directory to Head~2

(Much like git reset HEAD--hard, but only affects specific files)

(3) Revert (can be used in public branch, does not affect history, create a new commit to undo a commit modification)

Git checkout Feature

git revert head~2

11. Marking tags

(1) View current tab

git tag

(2) Marking the label

git tag-a v1.0.0-m "my version v1.0.0"

(3) View a tag

Git show v1.0.0

(4) Delete a tag

Git tag-d v1.0.0

(4) Push the tag to the remote

Git push Origin--tags

12. Compare

The difference between git diff Workspace (W) and staging Area (I)

The difference between the git diff HEAD Workspace (W) and the local repository (L)

The difference between git diff--staged staging area (I) and the local repository (L)

(Follow the file name in the back to compare only one of the files)

13. Packaging

Git archive--format zip--output/path/to/file.zip Master

Three, the common scene:

1. Create a new library

mkdir Tutorial

CD Tutorial

Git init

Touch readme.md

git add readme.md

Git commit-m "First commit"

Git remote add Origin http://xxx.xx.com/x.git

Git push-u Origin Master

2.push libraries already in existence

CD Existing_repo

Git remote add Origin http://xxx.xx.com/x.git

Git push-u Origin Master

Git Common Commands Summary

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.