Linux Tutorial: git Basics

Source: Internet
Author: User
Tags svn update git commands

Git is a distributed version control tool. This article begins with an introduction to git, focusing on the basic commands and usage skills of git, so that you can try to use git and experience the original one.
Version control tools can have so much impact on development. This article is divided into two parts. The first part introduces some common git commands, which are interspersed with the basic concepts and principles of git, article 2 highlights
Git's usage skills will finally create an open-source project on git hub to start your git practical journey.

What is git?

Git defines Wikipedia: it is a free, distributed version control tool, or a source code management tool that emphasizes high speed. Git was initially replaced by Linus

Torvalds is developed to manage Linux kernel development. Each git working directory is a completely independent code library with complete history and version tracking capabilities, independent
On the network and the central server.

The emergence of git has reduced the pressure on many developers and open-source projects to manage branch code. Due to its good control of branches, developers are encouraged to contribute to projects they are interested in. In fact, many open
Source Projects include Linux kernel, Samba, x.org server, and Ruby on
Rails has been transitioned to using git as its own version control tool. For developers who like to write code, there are two major advantages: we can go anywhere (on the subway at work)
) Submit your own code and view the code version. We can open many branches to practice our ideas, and the overhead of merging these branches is almost negligible.

Git 1 + 1

Now I will go to the real topic of this article and introduce the basic commands and operations of git, starting with the initialization, basic operations, and unique common commands of git version libraries, let everyone start using git.

Git usually has two methods for initialization:

Git clone: this is a simple initialization method. when you already have a remote git version library, you only need to clone it locally, for example, 'git clone
Git: // github.com/someone/some_project.git
The some_project command is a remote version of The urladdress 'git: // github.com/someone/some_project.git '.
This database is completely cloned to the local some_project directory.

Git init and git remote: This method is a little more complicated. When you create a working directory locally, you can enter this directory and use 'git
The init 'COMMAND is initialized, and git will later control the version of the files under this directory. If you need to put it on a remote server, you can create a directory on the remote server, and
You can use the 'git remote add' command to add a remote server, for example, 'git remote add
Origin git: // github.com/someone/another_project.git'this command will increase the urladdress to 'git:
// Github.com/someone/another_project.git', which is called the remote server of originand used only when the code is submitted later
The origin alias.

Now we have local and remote version libraries. Let's try using the basic git command:

Git pull: Updates code from other version libraries (either remote or local) to the local machine, for example: 'git pull origin master' is to update the code of the source database version to the master branch of the local master. This function is similar to the svn update function.

Git
Add: adds the changed or new files to the GIT index. Adding the files to the GIT index indicates that the files are recorded in the version history, this is also a step that needs to be executed before submission, such as 'git
Add APP/model/user. rb' to add the APP/model/user. RB file to the GIT index.

Git RM: delete files from the current workspace and indexes, such as 'git rm app/model/user. rb'

Git commit: submits changes to the current workspace, similar to the svn commit command, such as 'git commit-M "story #3, add user model "', -M is required for submission

Git push: update the local commit code to the remote version library. For example, 'git push origin' updates the local code to the remote version library named orgin.

Git log: View historical logs

Git revert: to restore a version modification, you must provide a specific git version number, for example, 'git revert bbaf6fb5060b4875b18ff9ff637ce118256d6f20 '. The GIT version number is a generated hash value.

The above commands are almost all public in each version control tool. Next we will try some unique git commands:

Git branch: add, delete, and query branches, for example, 'git Branch
New_branch creates a new branch named new_branch from the current working version, 'git branch-d
New_branch will forcibly Delete the branch called new_branch, and 'git branch' will list all local branches

Git checkout: git checkout has two functions: one is to switch between different branchs, for example, 'git checkout
New_branch will be switched to the new_branch branch. Another function is to restore the Code, for example, 'git checkout
The app/model/user. RB will update the user. RB file from the previous submitted version, and all unsubmitted content will be rolled back.

Git rebase: the two figures below will be clear. After the rebase command is executed, the pivot point is actually moved from C to G, so that the Branch has the function from C to G.

 

 

Git Reset: rolls the current working directory completely back to the specified version number, assuming that, for example, we have a version submitted five times by the A-G, where the C version number is
Bbaf6fb5060b4875b18ff9ff637ce118256d6f20, we executed the 'git Reset
Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 'then the result is only the three submitted versions of the A-C

 

 

Git stash

Git config: You can use this command to add and modify git settings, such as 'git config branch. master. Remote
The remote version library of the master is set to the alias "Origin version library". Later, you will use this command to customize your git to create a unique
Git

Git Tag: you can add a tag to a specific version so that you do not need to remember the complex version hash value. For example, you can use the 'git tag
Revert_version
Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 'to mark the version you restored. You can use this version later.
The revert_version label name, instead of the hash value.

Git provides convenient features such as local branches, which are related to its file storage mechanism. Git uses its own file system storage mechanism to store version control information. There is a. Git folder under the Code root directory, which has the following directory structure:

 

 

There are several important files and directories that need to be explained: The head file stores the information of the root node. In fact, the directory structure represents a tree structure. Git uses this tree structure to store versions.
Information, then head indicates the root; the refs directory stores various references under your current version control directory (Reference refers to the information of each tree branch used locally and remotely). It has
The four sub-directories heads, remotes, stash, and tags are used to store four references to different root, remote repository, git stack, and tag. You can run the 'git
Show-Ref: view the reference information more clearly. The logs directory stores the log information based on different references. Therefore, git only needs to record this. Git directory under the Code root directory.
Instead of having the. SVN directory under the root directory and sub-directory like SVN. Let's take a look at the differences between git and SVN.

Git and SVN are different

SVN (subversion) is currently the most used version control tool. Compared with git, git has two major advantages: easy to add local branches and distributed features.

The following two figures show the differences between git and SVN.

 

 

 

 

For easy local addition of branches, the GIT local and server-side structures in the figure are flexible, and all versions are stored in a directory. You only need to switch the branches to work in a branch.
Effect. SVN is completely different. If you need to test your code locally, you can only maintain multiple copies locally. Each copy corresponds to a SVN server address. For example
Previously, my team used SVN as a version control tool. When I was trying to enhance a module and work halfway, many tests on the Code server failed due to changes in the behavior of the original module, so no
Code has been submitted. At this time, the superior told me that there is a very urgent bug to be handled,
It must be completed within two hours. I had to modify all the locally modified diff and output it into a patch file. Then I rolled back all the code about the current task and started the bug modification task.
After modification, apply the patch back. You must complete multiple tedious steps before, after, and after, regardless of the workload required for intermediate code conflicts. However, if you use git,
We only need to open a branch or switch back to the main branch to start the bug modification task at any time. After the task is completed, we can switch to the original branch to continue the previous Task elegantly. Every
A new job can start a branch, and then merge it into the main branch, which is easy and elegant.

Distributed for git, you can submit code locally. Therefore, in the figure above, git is conducive to breaking down a large task for local multi-commit, while SVN can only be large locally.
A one-time change of the quantity, resulting in a huge risk of merging to the trunk in the future. Git code logs are stored locally and can be viewed at any time. SVN logs are stored on the server.
. The Code server of my team is in the United States. Every time I view the Team's work a few years ago, it takes 10 minutes to download logs. This is a pain. Later we migrated to git
Using the local characteristics of git logs, I wrote a rake script in Ruby to view the history of all the code for a specific task. It takes only a few seconds each time, this greatly facilitates my work. Of course
Distributed deployment does not mean that you do not need a code Center Server when using git. If you work in a team, you still need a server to save all the code.

Summary

This article introduces the basic concepts, Common commands, and principles of git.

 

Address: http://www.jz123.cn/text/2129569.html

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.