Git uses basic articles (some common commands and principles) _ Server Other

Source: Internet
Author: User
Tags svn svn update tag name using git git clone ruby on rails

Git is a distributed version Control tool, this article starts with the introduction of Git, focusing on the basic commands and use of git, let you try to use Git at the same time, experience the original version control tool can have so much impact on development, the article is divided into two parts, The first part introduces some of Git's common commands, interspersed with the basic concepts and principles of Git, and the second focuses on Git's use tips, and finally creates an open source project on Git hub to open your Git real-world tour

1. What is git

The definition of git on Wikipedia: It's a free, distributed version Control tool, or a fast source code management tool. Git was originally developed by Linus Torvalds to manage the development of the Linux kernel. Each git working directory is a completely separate code base with complete history and version tracking capabilities, not dependent on the network and hub servers.

The presence of git eases the pressure on many developers and Open-source projects to manage branch code, and encourages developers to contribute to projects of interest to them due to good control of the branch. Many open source projects, including Linux kernel, Samba, Server, Ruby on Rails, have transitioned to using Git as their version control tool. For those of us who like to write code, there are two of the biggest benefits, we can submit our own code and view the code version at any location (on the subway to work), we can open many branches to practice our ideas, and the cost of merging these branches is almost negligible.

2, Git 1+1

Now go to the real theme of this article, and introduce Git's basic commands and operations, starting with the initialization of Git's repository, basic operations, and the unique common commands that allow you to start using Git.

Git usually has two ways to initialize:

git clone: This is a simpler way to initialize, and when you already have a remote Git version library, just clone a copy locally, such as ' Git clone git:// some_ Project ' command is to completely clone the remote version Library of the URL address of ' git:// ' to the local some_project directory

Git init and git remote: This is a little more complicated, when you create a working directory locally, you can access this directory and use ' Git Init ' command to initialize, Git will later version control of the files in the directory, this time if you need to put it on the remote server, you can create a directory on the remote server, and the access to the URL record, you can use the ' Git remote Add ' command to add a remote server side, such as ' git remote add Origin git:// ' This command will add URL to ' git:// ', a remote server named Origin, you only need to use Origin alias when submitting code later

3, git the basic command

Now that we have a local and remote version library, let's try to use the basic commands for Git:

git Pull: updates code to local from other version libraries (either remotely or locally), such as: ' Git pull origin Master is to update the code for this version of origin to the local master primary, which is similar to the SVN update

git add: Adding the current change or new file to Git's index, adding it to Git's index means that it is recorded in version history, which is a step that needs to be performed before submitting, such as ' git add app/model/user.rb ' Will add app/model/user.rb files to Git's index

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

git commit: submit changes to the current workspace, similar to SVN's commit, such as ' Git commit-m ' story #3, add user model ', must be submitted with-m to enter a submit information

git push: update the code for a local commit to a remote version library, such as ' Git push origin ' to update the local code to a remote version library named Orgin

git log: View history log

git revert: restore a version of the modifications that must provide a specific git version number, such as ' git revert Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ', Git's version number is a generated hash value,

Almost all of the above commands are public to every version control tool, so let's start by trying some of Git's unique commands:

4, some of Git's unique commands

git branch: Add, delete, and search operations on branches, such as ' Git branch new_branch ' will create a new branch called New_branch from the current working version, ' Git branch-d new_ Branch ' will force the deletion of the branch called New_branch, ' git branch ' will list all the local branches

git checkout: GIT's checkout has two roles, one is to switch between different branch, for example, ' Git checkout new_branch ' will switch to New_branch branch; Another function is to restore the code, for example, ' Git checkout app/model/user.rb ' will update the user.rb file from the last submitted version, and the uncommitted content will be rolled back

git rebase: explained in the following two diagrams will be more clear, rebase command execution, actually is to move the branch pivot from c to G, so the branch also has the function from C to G

git reset: completely rollback the current working directory to the specified version number, assuming the following figure, we have a-g five submitted versions, where C's version number is Bbaf6fb5060b4875b18ff9ff637ce118256d6f20, We executed the ' git reset bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ' So the result is only the A-C three submitted versions

git stash: Put the currently uncommitted work into the GIT stack and apply it back when the time is ripe, here for a moment to mention the usage of the command, followed by a tip on the tips

git config: Use this command to add and change the various settings for git, such as ' git config branch.master.remote Origin ' sets the remote version Library of Master to an alias called the Origin version library, and the following tips will use this command to personalize your git, creating a unique git for you.

git tag: You can label a specific version so that you don't need to memorize complex version number hashes, such as you can use ' git tag revert_version Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ' to mark the version you restored, you can use the revert_version tag name instead of the hash when you want to view the version later.

The fact that git can provide convenient local branching features is related to its file storage mechanism. When Git stores version control information with its own set of file system storage mechanisms, there is a. git folder under the code root that has the following directory structure:

There are several more important documents and directories that need to be explained: The head file holds the root node's information, in fact, the directory structure represents a tree structure, Git uses this tree structure to store version information, then the head represents the root; The refs directory stores the various references you have under the current version control directory ( Reference refers to your local and remote use of the various tree branches of information), it has heads, remotes, stash, tags four subdirectories, stored separately for the different root, remote version of the library, git stack and tag four kinds of references, you can by command ' git Show-ref ' to see the reference information more clearly; The logs directory stores the log information according to different references. As a result, git only needs this one in the code root directory to record the full version control information, rather than having an SVN directory under the root directory and subdirectories like SVN. So let's look at the difference between git and SVN.

5. The difference between git and svn

SVN (Subversion) is the most currently used version control tool. In contrast, GIT has the biggest advantage of two: it is easy to add branching and distributed features locally.

Here are two images that show the difference between Git and svn

For easy local branching, the GIT local and server-side structure is flexible, all versions are stored in a directory, and you only need to branch out to achieve the effect of working on a branch. SVN is completely different, if you need to test some of your own code locally, you can only maintain a number of different copies, each copy corresponds to an SVN server address. As a practical example, my team used SVN as a version control tool, and when I was trying to enhance a module, half of the work was done, because the behavior of the original module resulted in the failure of many tests on the code server, no code was submitted. At this time the superiors told me that there is a very urgent bug that needs to be handled and must be completed within two hours. I had to make a diff of all the local changes and output it as a patch file, then roll back all the code on the current task, then start modifying the bug, and wait until it's modified to apply the patch back. There are a number of tedious steps to be done backwards and forwards, regardless of the amount of work that occurs when the intermediate code conflicts. However, if you use git, we just need to open a branch or go back to the main branch, you can start the Bug modified task at any time, after completion, as long as the switch to the original branch can gracefully continue the previous task. As long as you want, each new task can open a branch, complete, and then merge it into the main branch, relaxed and elegant.

Distributed for Git, you can submit your code locally, so in the diagram above, Git is good for breaking down a large task, making local multiple submissions, and SVN can only make a large number of one-time changes locally, resulting in a huge risk of merging into the backbone in the future. Git's code log is local and can be viewed at any time. SVN logs are on the server, and the logs need to be downloaded from the server each time they are viewed. I work in the team, the code server in the United States, every time we look at the work of the Group a few years ago, the log download will take 10 minutes, which can not but be said to be a pain. Then we migrated to Git, leveraging the local nature of git logs, and I wrote a rake script in Ruby that looked at all the code histories for a specific task, and it took only a few seconds to make my job much easier. Of course distributed does not mean that using git does not require a code center server, if you work in a team, or need a server to save all the code.


This article introduces the basic concept of git, some common commands and principles, you can try to experience, the next one will focus on the use of git command tips, git with the tools, and finally create an open source project on the Git hub.

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: 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.