Common git commands

Source: Internet
Author: User
Tags git commands
ArticleDirectory
    • 1. Concept
    • 2. Git Configuration
    • 3. View commands
    • 3. Local Database Operations
    • 4. Remote database operations
    • 5. View File modifications (diff)
    • 6. Reset and revert)
    • 7. Create a tag)
    • 8, Miscellaneous

Because git (GitHub) is used in the project, and git commands have many concepts, I will record them here.

1. Concept

Git is a distributed version control system with no central library in its git design. Of course, in actual development, we usually set a central database to facilitate management, such as the repository on GitHub as the central database. However, this is not mandatory by git. It is best practice, which we set in terms of concept to facilitate management. The central database is generally called origin.

Git only has local and remote repository ). Git is generally a level-3 commit:

1. We will first submit it to the stage and use git Add. | filename

2. submit it to the local database (this is also a better place for git than SVN, suitable for frequent submission), and use git commit-M "You comments here"

3. Finally, when we think we needCodeDuring the sharing, we submitted the information to the remote database and added the remote database through git remote add [remote name] git: // github.com/octocat/spoon-knife.git, use git push [remotename] Master to push updates to the remote database.

Git metadata is concentrated. There is a. Git directory in the directory where the library is located, and all metadata is under this directory.

2. Git Configuration

Git has three configuration files: repo/. Git/config, $ home/. gitconfig, and/etc/gitconfig.

* The configuration file at the repo/. Git/config database level is only valid for the current database and has the highest priority (GIT config-local)

* $ Home/. gitconfig user-level configuration files are valid for current users, followed by git config-Global)

* The global configuration file of the/etc/gitconfig system is valid for the entire system and has the lowest priority (GIT config-system)

Git config -- list # view the current valid configuration

Git config -- Global color. UI true # Set color. UI to true

Git supports Automatic completion. That is to say, if you enter git conf and press the tab key, the system will automatically expand to git config. This option is still valid, for example, git config-global-color. [Tab], you will see many options.

3. View commands

1. view the current git database status

Git status [-S] #-S is a brief output. The left column shows the status of the temporary storage zone and the right column shows the status of the workspace.

2. displays the remote repo of the current connection.

Git remote-V

3. Branch is displayed. In git, branch is a working context. You can create a new branch to create a working context.

Git branch [-v]

4. shows who last modified each line of the file.

Git blame a-file.c

5. Display Branch's graph in a concise way

Git log -- graph -- pretty = oneline [-5] #-5 shows the last five logs.

6. displays the details of the last two commit operations, including file changes.

Git log-P-2 #-P prints the diff Result of the change.

Git show <commit-ID> # view specific commit

3. Local Database Operations

1. Create a version Library

Git init reponame # create a local database

Git clone git@github.com: jatsz/snippets. Git # create a repository in remote clone mode

2. Add files to the stage)

Git add file # Add the file to the version library, which is actually in stage.

Git add-u # Add updates to all tracked files to stage

Git Add. # Pay attention to this point number, which is to add all the changes to the stage

3. Submit the temporary database (COMMIT) to the local database.

Git commit-m "you comments" # submit the changes in stage to the local library

 

4. Remote database operations

1. Add a remote database

Git remote add upstream git: // github.com/octocat/spoon-knife.git # create a remote database and name it upstream

2. Get code from remote database

Git fetch upstream # Get

Git merge upstream/master # merge to the current development branch. If you do not see any file updates in this step, git status displays "your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded."

3. Push local updates to the remote database.

Git push upstream master # upload the local master to upstream

4. delete a remote database

Git remote RM upstream # delete a remote database named upstream

5. View File modifications (diff)

Generally, each file has four images: Local workspace, stage, local Repo, and remote repo ). Therefore, we use different diff options to compare file images in different locations.

1. Comparison between the work zone and the temporary storage Zone

Git diff [file-name]

2. Comparison between the temporary storage zone and the local database

Git diff-cached [file-name]

3. Comparison between the workspace and local database

Git diff head [file-name] Or git diff master [file-name] # Head is usually the alias of the master.

4. Comparison between local and remote databases

This is more troublesome. We need to obtain the remoteSource code

Git fetch upstream # Here, upstream is a remote code library

Git diff master upstream/Master

6. Reset and revert)

Git withdrawal refers to the process in which you add the workspace modification to the temporary storage zone. Now you want to withdraw it without changing the file. You can use an image (staged snapshot) in the temporary storage area to overwrite the files in the workspace.

Withdrawal and remorse in git should also be taken into account: workspace, temporary storage area, local database.

1A, unstage)

Git reset-readme.txt convert readme.txt unstage. If you want to unstage all files, git reset them directly.

1b. I regret it from the temporary storage area and use the temporary storage area file to overwrite the workspace file.

Git checkout-readme.txt # You will lose all unstaged modifications

1 C. Use the snapshot of the local database to overwrite the files in the workspace.

Git checkout head-readme.txt # loss of all staged and unstaged modifications

2a, undo commit)

Git reset-soft head ^ # Head indicates the current version, and head ^ indicates the last submission currently submitted,

2b, directly withdraw from the local database to the workspace (UNDO commit => undo stage)

Git reset-mixed head ^

7. Create a tag)

1. Create a milestone for the latest release

Git tag rev-1.1-M "create tag for release v 1.1"

8, Miscellaneous

1. Ignore some files: add the. gitignore file.

The. gitignore file applies to the current folder and all subfolders. Wildcard characters can be used. Instance:

. Ds_store
*. PyC
*. Avi
*. MP4
*. Wmv

2. Do not use git reset-hard easily

If you want to restore the previous version, you can use git checkout head ^ [-cached]-readme to find out the latest changes, then in git commit-M "delete added line to readme file ".

The advantage of this is that the historical records are fully stored, and the graph of log history is also easy to understand. You can use git reset-hard head ^ to completely lose history. If you do this, you can only retrieve the history in this way:

Git reflog-10

Git resest-hard head @ {2}

 

Reference:

Http://kb.cnblogs.com/page/132209/

Http://book.douban.com/subject/6526452/-Git authoritative guide Jiang Xin

Http://marklodato.github.com/visual-git-guide/index-en.html -- a visual git reference

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.