Git getting started

Source: Internet
Author: User

Git getting started

1. Overview

Why should I select Git for software version management tools?

When you actually learn to use Git, you will feel that the answer to this question is very natural. However, when you really need to use text to answer the question, it seems that the text is not that adequate. Hi, what should I do?

In fact, the key question is not how to answer this question. The key to the problem is that the company has decided to use it. So, our programmers! Start your browser, and use your search engine tool to discover the answer. Here, I can only give you one of the most obscure feelings.

Unlike CVS and SVN, Git is a distributed source code management tool. The Linux kernel code is managed by Git. It is very strong and fast. Its direct benefits include:

1. the initialization of dummies will be finished, git init, git commit-. For anyone who writes two lines of code to the code management tool, it is not suitable. You can also use git as a backup system or synchronize the documents of the two machines.

2. Most of the operations are done locally, without interacting with the code management server in the cluster, and finally you can boldly check in code anytime and anywhere. Only the final version must be submitted to a centralized code management server.

3. Each commit creates a unique commit id for all codes. Unlike CVS, the version of a single file is changed separately. Therefore, you can check all the code before a certain submission at a time without considering the files submitted. (SVN can also do this)

4. branch management is much easier. No redundant directories need to be created, either by creating a new branch or by switching between branch.

5. When branch is merge, not only will the code be merge together, but the check in history will be retained, which is very important.

6 .... Too many

Of course, Git will also bring us some difficulties. First, if you want to use git well, you must really understand its principles and concepts. For those who are familiar with CVS, it is especially important to change the concept of pure centralized source code management, which may also make you feel a little difficult. You may feel a little difficult at the beginning of using git, but you will definitely like it when you get to know it. It must be, just as I asked you, "I like a gentle, unfeeling housewife, or a cool-running, so that you love a cool-eyed lover.

Next, let's start learning Git...

Keep in mind that this is a very simple and basic tutorial. To become a git expert, you need to constantly explore it yourself.

Gitolite-based Git service setup

Linux git command parameters and Usage Details

Fedora downloads Git through Http Proxy

Install Git on Ubuntu Server

Create a Git repository on the server (Ubuntu)

Git simple tutorial in Linux (taking Android as an example)

Git authoritative guide PDF

2. Git basic commands

2.1 create a Git Library: git-init

Have you ever created a CVS database? Few operations should have been performed, right? Because many people use the checkout code from the CVS library. Similarly, in cooperative development, if you are not the initiator of a code module, you will not use this command. More are using git-clone (see section 2.7 ). However, if you want to develop a small module and manage it with code management tools for the time being (in fact, I often do this, at least many personal development processes can be preserved, it is easy and convenient to create a Git library.

For Cool News, when a Git library of code is created, a code file will be added to the library, and the library will be placed on the company's server dedicated for code management, so that you can clone it later (do not understand? It doesn't matter. You can see it later. For individuals, you can place this library wherever you like, as long as you can access it.

It is easy and convenient to create a Git library. You only need to use the git-init command. For versions earlier than Git1.4 (including git1.4), this command is git-init.

A) $ mkdir dir

B) $ cd dir

C) $ git-init

In this way, an empty version library is created and a subdirectory named. git is created in the current directory. In the future, the file change information will be saved to this directory. Unlike CVS, a nasty CVS directory will be created under each directory and subdirectory.

There is a config file in the. git directory. You need to add your personal information before using it. Otherwise, we cannot add or modify any files.

The original config file is like this,

[Core]

Repositoryformatversion = 0

Filemode = true

Bare = false

Logallrefupdates = true

We need to join

[User]

Name = xxx

Emai = xxx@kuxun.cn

Now a git version library has been created, but it is empty and cannot do anything. The next step is how to add files to the version library. If you want to ignore some files, you need to add the. gitignore file under the git root directory.

2.2 An Important Command: git-update-index

Before introducing how to add files to the git library, you have to introduce the git-update-index Command. This command may cause many users familiar with CVS to be confused. Generally, code changes submitted to a source code management library are abstracted into the following actions: Change files; submit the changes to the source code management system identity. For example, to delete an object from a CVS database, you need to delete the object first, then cvs delete; and finally cvs commit.

Therefore, git-update-index is an abstract operation that changes the identity file of the source code management system. In brief, the git-update-index Command notifies the git database that the status of files has changed (new addition, modification, and deletion wait ). This command was frequently used in earlier git versions. The new git version (version 1.5 and later) has been packaged by other commands and is not recommended.

The most common methods of git-update-index are as follows. For more functions, see man git-update-index.

Method 1: git-update-index -- add file name list. If the file exists, this command identifies the file changes to the git Library (whether or not the file has been modified). If the file does not exist, this command indicates to the git library that a new file needs to be added.

Method 2: git-update-index -- force-remove file name list. This indicates that files are deleted from the git repository. Whether the file has been deleted or not, this command only notifies the git library to delete the files from the library. These files are not affected.

Therefore, git-update-index only serves as a notification and identifier for the git library, and does not operate on specific files.

2.3 add or delete files to or from the git Repository: git-add and git-rm

In fact, it is wrong to use the git-add command to add files to the git repository, or at least completely. The essence of the git-add command is a package of the "git-update-index -- add" command. Therefore, in addition to adding files, git-add can also identify file modifications. The commit operation can be performed only after git-add is called. The same is true for git-rm. It is a package of git-update-index -- force-remove.

For git-add, if git-add * is called in a directory, all files in the subdirectory are recursively added to the git library by default. The same is true for git-rm. This is a big difference from CVS.

In addition, you can run the git-ls-files command to view the files in the current git library.

2.4 check the version database status: git-status

With this command, we can view the status of the version library. We can see that those files have changed and those files have not been added to the git library. We recommend that you use this command to check the database status before each commit operation. To avoid misoperation.

In general, the most common misoperation is that a file has been modified, And the commit operation is called directly without calling git-add to notify the git library that the file has changed, as a result, the file is not actually submitted. If the developer thinks that the file has been submitted and continues to modify or even delete the file, the modified content is not managed by version. If you use git-status to view the error before each commit, you can find this error. Therefore, if you call the git-status Command, pay special attention to the files that prompt "Changed but not updated. These files are all changed compared with the previous commit, but they are not identified by git-add.

2.5 submit changes to the version Library: git-commit

Directly call the git-commit command and you will be prompted to enter a comment. You can also enter the submit comment in the command line as follows: git-commit-m "Initial commit of gittutor reposistory ". Note: Unlike CVS, The commit comments of git must not be empty. Otherwise, the submission fails.

Git-commit also has a-a parameter that can be forcibly submitted without any changes identified by git-add. However, this method is not recommended.

For each commit, git creates a unique commit ID code for the global code. You can use the git-revert command to restore the code at any commit. This is much easier to manage than different versions of CVS files. (Similar to SVN)

If you want to see the file changes before submitting the command, you can use git-diff to view the changes. However, the output of this command is unfriendly. Therefore, we recommend that you use other tools to implement this function. After submission, you can also run the git-log command to view the submission records.

2.6 branch management: git-branch

We ushered in the most powerful git, which is also more powerful than CVS and SVN-branch management.

Every programmer may often encounter this situation:

1. You need to immediately put down your work, modify the bug of a previous version, and launch it online.

2. I wanted to make an important modification to the central database commit, but because I had to back up the code frequently, I had to commit it frequently to the central database. As a result, a large amount of useless commit information is retained in the central database.

3. Submit a change to a colleague for code review. However, the code review of the colleague is slow. When feedback is received, the code has changed, which is difficult to merge.

If we use CVS or SVN to solve these scenarios, though they may not be able to solve them, they are complicated and complicated. It is certainly a sense that everyone else might feel worse! The key is that the branch management of CVS or SNV is too complex and is not available.

The cost of creating branches in the git version library is almost zero, so you do not have to create several more branches. When git-init is executed for the first time, the system creates a branch named "master. Other branches are created manually. The following lists some common branch policies that will facilitate your daily development.

1. Create a Personal Work Branch to avoid too much interference to the master of the master branch and facilitate communication and collaboration with others.

2. When performing high-risk work, it is better to create a pilot branch and discard a mess than to clean it up.

3. when merging other people's work, it is best to create a temporary branch for merging. After the merge is completed, "fatch" is added to your own branch (the merging and fatch are described later, if you don't understand it, continue reading it)

2.6.1 view branch:-git-branch

Call git-branch to view the existing branches and current branches in the program.

2.6.2 create a branch: git-branch name

To create a branch, use the following method:

1. git-branch name

2. git-checout-B branch name

In the first method, although a branch is created, the current working branch is not switched to the new branch. Therefore, you also need to run the "git-checkout branch name" command to switch, the second method not only creates a branch, but also switches the current working branch to the branch.

In addition, note that the branch name may be duplicated. For example, I have created two Branches a and B under the master branch, and then switched to the B branch, branch a and Branch c are created under branch B. This operation can be performed. In this case, branch a and branch a under the master are actually two different branches. Therefore, it is not recommended to perform this operation in actual use, which may lead to naming doubts.

2.6.3 Delete branch: git-branch-D

The git-branch-D branch name can be used to delete a branch, but be careful that after the branch is deleted, all changes that occur in the branch cannot be recovered.

2.6.4 switch branch: git-checkout branch name

If the Branch already exists, you can use the git-checkout branch name to switch the working branch to the branch name.

2.6.5 view branch history: git-show-branch

You can call this command to view the historical changes of a branch. For example:

* [Dev1] d2

! [Master] m2

--

* [Dev1] d2

* [Dev1 ^] d1

* [Dev1 ~ 2] d1

* + [Master] m2

In the preceding example, the two rows above "--" indicate that there are two branches: dev1 and master, and the last commit log of the dev branch is "d2 ", the last submitted log on the master branch is m2 ". "--" Indicates the evolution history of the branch. dev1 indicates the last commit that occurred on the dev branch, dev ^ indicates the second-to-last commit on the dev branch. Dev1 ~ 2 indicates the last and third commit on the dev branch.

2.6.6 merge branch: git-merge

Git-merge is used as the source branch of the target branch merged by git-merge "some memo. For example:

Git-merge master dev1 ~ 2

If there is a conflict in the merge, git will prompt that git-merge is rarely used at present and replaced by git-pull.

Usage: Source branch of the target branch merged by git-pull. For example, git-pull. dev1 ^

2.7 remotely obtain a git Repository: git-clone

As mentioned in section 2.1, if you are not the initiator of a code module, you will not use the git-init command, but rather use git-clone. With this command, you can obtain a complete git library from the remote end and interact with the remote git through some commands.

The git-based code management organization structure usually forms a tree structure. developers generally obtain the development environment from the git library of the manager of a code module through git-clone. After local iterative development, then, the module Administrator submits the code to the module administrator. The module administrator checks the submitted code and merges the code into his/her own database. The module code is submitted to the higher-level code manager.

For Cool News, the company has a central git library. during development, everyone obtains the latest code from the central git-clone library.

Git-clone is used as follows: git-clone [ssh: //] username @ ipaddr: path. Here, "ssh: //" is optional, and there are other obtaining methods, such as rsync. Path is the root Path of the remote git, also called repository.

After obtaining the remote git library through git-clone, the developer information in. git/config will not be cloned together. You still need to add developer information to the. git/config file. In addition, developers must add their own. gitignore files.

In addition, the remote git library obtained through git-clone only contains the current working branch of the remote git library. If you want to obtain other branch information, you need to use "git-branch-r" to view it. If you need to obtain other remote branch code, you can run the command "git checkout-B local branch name remote branch name", where the remote branch name is the branch name listed by git-branch-r, it is generally like "origin/branch name. If the local branch name already exists, the "-B" parameter is not required.

2.8 obtain a git branch remotely: git-pull

Unlike git-clone, git-pull can obtain the content of a branch from any git library. The usage is as follows:

Git-pull username @ ipaddr: Remote repository name remote branch name: local branch name. This command obtains a local branch of the local git library from the remote branch name of the remote git library. If the local branch name is not specified, the default pull is to the local branch.

Note that git-pull can also be used to merge branches. It works the same way as git-merge. Therefore, if your local branch already has content, git-pull will merge these files. If there is a conflict, an alert will be reported.

2.9 submit the local branch content to the remote branch: git-push

Git-push and git-pull are the opposite. They submit the content of a local branch to a remote branch. Usage:

Git-push username @ ipaddr: Remote repository name local branch name: Remote branch name. This command pushes a local branch of the local git Library to the remote branch name of the remote git library.

Note that git-push does not automatically Merge files. This is my test, but I am not sure if it is my use error. Therefore, if a conflict occurs during git-push, the content of the Post-push file will be forcibly overwritten without any prompt. This is very dangerous during cooperative development.

2.10 database reversal and Restoration: git-reset

In addition to Resetting discarded R & D code, database reversal and restoration also play an important role. For example, we cloned a code library from a remote server and submitted it back to the remote server after local development. However, the local code library has a functional commit and a commit for backup purposes during development. In short, there are a lot of useless logs in commit logs. We do not want to submit these logs to the database when they are submitted back to the remote. Therefore, git-reset is required.

The concept of Git-reset is complicated. Command Format: git-reset [-- mixed | -- soft | -- hard] [<commit-ish>]

Command Options:

-- Mixed

This is the default option. For example, git-reset [-- mixed] dev1 ^ (for the definition of dev1 ^, see 2.6.5 ). It only resets the branch status to dev1 ^, but does not change the content of any working file. That is, all file changes from dev1 ^ To dev1 are retained, but all commit logs between dev1 ^ and dev1 are cleared, and, the changed file content is not identified by git-add. If you want to re-commit the file, you also need to perform git-add on the changed file. In this way, a very clean submission record is obtained after the commit operation.

-- Soft

It is equivalent to doing git-reset-mixed, and then done git-add for the changed file. If this option is used, you can directly commit it.

-- Hard

This command will cause the rollback of all information, including the file content. It is generally used only when the discarded code is reset. After execution, the file content cannot be restored.

2.11 more operations

The previous 10 sections only briefly introduced the basic commands of git. For more details, go to the man git document in linux.

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

Related Article

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.