From: http://www.open-open.com/lib/view/open1332904495999.html
Git is a distributed version Control tool, this article from the introduction of Git, the focus is to introduce the basic git command and usage skills, let you try to use Git, while the original version control tool can have so much impact on development, the article is divided into two parts, The first part introduces Git's common commands, which are interspersed with the basic concepts and principles of Git, the second focuses on the use of Git, and finally creates an open source project on the Git hub to start your git combat journey
1. What is git?
Git's definition of Wikipedia: It's a free, distributed version Control tool, or a source code management tool that emphasizes fast. 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, independent of the network and hub servers.
The advent of Git eases the pressure on many developers and open source projects to manage branch code, and thanks to the good control of the branch, developers are encouraged to contribute to the projects they are interested in. In fact, many open source projects include Linux kernel, Samba, x.org Server, and Ruby on Rails, all of which have transitioned to using Git as their version control tool. For those of us who like to write code, there are two great benefits, we can submit our 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 overhead of merging these branches is almost negligible.
2. Git
Now go to the real topic of this article and introduce Git's basic commands and actions, starting with Git's repository of initialization, basic operations, and unique commands for common use, so you can start using Git.
Git usually has two ways of initializing:
git clone: This is a simpler way of initializing, and when you already have a remote git repository, you just need to clone a copy locally, such as ' Git clone git://github.com/someone/some_project.git some_ The project ' command is to completely clone the remote repository of the URL address of ' git://github.com/someone/some_project.git ' to the local some_project directory
Git init and git remote: This is a little more complicated, and when you create a working directory locally, you can go to the directory, initialize it with the ' git init ' command, and Git will then version the files in that directory, At this point if you need to put it on a remote server, you can create a directory on the remote server and record the accessible URL, you can use the ' Git remote add ' command to add a remote server side, such as ' git remote add origin git ://github.com/someone/another_project.git ' This command will add a URL address of ' Git://github.com/someone/another_project.git ', A remote server named Origin, which will only need to use the Origin alias when committing to the code at a later time
3. Basic git commands
Now that we have a local and remote repository, let's try using Git's basic commands:
git Pull: Update the code locally from the other repository (either remotely or locally), for example: ' Git pull Origin master ' is updating the code for the Origin repository to the local master primary branches, which is similar to the SVN update
git add: is to add the current changes or new files to the Git index, added to the Git index is recorded in the version history, this is a step before committing, such as ' git add app/ Model/user.rb ' will add the app/model/user.rb file to the git index
git rm: Delete files from the current workspace and index, such as ' git rm app/model/user.rb '
git commit: commits changes to the current workspace, similar to the SVN commit command, such as ' Git commit-m ' story #3, add user model ', You must use-M to enter a commit message when submitting
git push: updates the local commit code to the remote repository, such as ' Git Push origin ', which updates the local code to the remote repository named Orgin
git log: View history Log
git revert: to Restore a version of the changes, you must provide a specific git version number, such as ' git revert bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ', The version number of Git is a hash value generated,
The commands above are almost all common to each version control tool, so let's try some of the git-unique commands:
4. Some commands that are unique to git
git branch: adding, deleting, and checking branches, such as ' Git branch new_branch ' creates 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 local branches
git checkout: GIT's checkout has two functions, 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, such as ' Git Checkout app/model/user.rb ' will update the user.rb file from the last committed version, and all uncommitted content will be rolled back
git rebase: explained in the following two diagram will be more clear, after the rebase command executes, it is actually to move the branch from C to G, so that the branch has a function from C to G
git reset: The current working directory is completely rolled back to the specified version number, assuming that we have a-g five commit version, where C is the version number is BBAF6FB5060B4875B18FF9FF637CE118256D6F20, we executed the ' Git reset bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ' so there's only a-c three committed versions left.
git stash: Put the current uncommitted work into the GIT work stack, when the time is ripe to apply back, here for the moment to mention the use of this command, the following in the tip of the key to explain
git config: This command allows you to add and change various git settings, such as ' git config branch.master.remote origin ' to set the remote repository of master to an alias called The Origin repository , you'll use this command to personalize your git for you, and create a unique git for you.
git tag: You can tag a specific version so you don't have to memorize a complex version number hash, for example you can use ' git tag revert_version Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ' to mark this version that you restored, then you can use the revert_version tag name instead of the hash value when you want to view the version later
Git's ability to provide convenient local branching features is related to its file storage mechanism. Git stores versioning information using its own set of file system storage mechanisms, with a. git folder under the code root, with a directory structure like this:
There are a few more important files and directories to explain: The head file holds the root node information, in fact, the directory structure represents a tree structure, Git uses this tree structure to store version information, then head represents the root; The refs directory stores the various references you have under the current version control directory ( Reference refers to the information you use for local and remote branches of each tree, it has heads, remotes, stash, tags four subdirectories, respectively, store different root, remote repository, git stack and tag four kinds of references, you can command ' git show-ref ' View reference information more clearly; The Logs directory stores log information based on different references. As a result, git only needs the. Git directory under the code root to record the full version control information, not the root directory and subdirectories like SVN. SVN directory. So here's a look at the difference between git and SVN.
5. The difference between git and svn
SVN (Subversion) is currently the most used version control tool. Compared to it, the biggest advantage of Git is the two point: it's easy to add branching and distributed features locally.
Here are two images that show how git differs from SVN
& nbsp
for easy local branching, the GIT local and server-side structures are flexible and all versions are stored in a single directory. You only need to switch branches to achieve the effect of working on a branch. And SVN is completely different, if you need to test some of your own code locally, you can only maintain a number of different copies locally, each of which 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, I was doing half of the work, because it changed the behavior of the original module and caused many tests on the code server to fail, so I didn't commit the code. At this time the superior said to me, there is a very urgent bug need to deal with, must be completed within two hours. I had to diff the local changes and output them as a patch file, then roll back all the code for the current task, start the task of modifying the bug, and then apply the patch back when it was modified. There are a number of tedious steps to complete, regardless of the amount of work that occurs in the middle Code conflict. However, if you use Git, we only need to open a branch or go back to the main branch, you can start the Bug modification 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, easy and elegant.
Distributed for Git, you can commit the code locally, so in the diagram above, git facilitates the decomposition of a large task for local multiple commits, and SVN can only make a large number of one-time changes locally, resulting in a huge risk of merging into the trunk in the future. Git's code log is local and can be viewed at any time. SVN logs are on the server, and each view log needs to be downloaded from the server first. I work in a group, the code server in the United States, each time the team to review the work done a few years ago, the log download takes 10 minutes, it is not a pain. Later we migrated to Git, leveraging the native features of the git log, I wrote a rake script in Ruby to see all the code history for a particular task, just a few seconds at a time, making it much easier for me to work. Of course, distributed does not mean that git does not need a code center server, if you work in a team, still need a server to save all the code.
Summary
This article introduces the basic concepts of git, some common commands and principles, you can try hands-on experience, the next one will focus on the use of GIT commands, Git comes with the tools, and finally create an open source project on the Git hub.
Git uses a basic article