Git Common commands detailed (ii)

Source: Internet
Author: User
Tags svn tag name using git git clone

Git is a powerful distributed versioning tool that applies not only to the source code for managing large open source software (such as Linux kernel), but also to managing private documents and sources (such as: Wsi-lgame-pro)

For more information about git, please refer to my previous blog: Git version management tool


First, Git command initial knowledge

Before you formally introduce the GIT command, first introduce Git's basic commands and operations, and have a general understanding of the GIT command

Example: There are usually two ways to initialize from a git version library:

1 git clone: This is a simpler way to initialize, and when you already have a remote git version of the library, just clone a copy locally

For example: Git clone git://github.com/someone/some_project.git some_project

The command above is to completely clone the remote version Library of the URL address of ' git://github.com/someone/some_project.git ' to the local some_project directory


2 git init and git remote: This approach is slightly more complicated, and when you create a working directory locally, you can access this directory and use ' Git Init ' command to initialize, and if you need to put it on a remote server, you can create a directory on the remote server and record the accessible URL at a later time, and you can use ' Git remote Add ' command to add a remote server side,

For example: Git remote add Origin git://github.com/someone/another_project.git

The above command will add a URL to ' git://github.com/someone/another_project.git ', a remote server named Origin, and only use Origin aliases when submitting code later


Two, Git common commands

1 Remote Warehouse related command

Checkout warehouse: $ git clone git://github.com/jquery/jquery.git

View remote warehouses: $ git remote-v

Add remote warehouse: $ git remote add [name] [url]

Delete Remote warehouse: $ git remote RM [name]

Modify Remote warehouse: $ git remote set-url--push [name] [Newurl]

Pull remote warehouse: $ git pull [remotename] [Localbranchname]

Push remote warehouse: $ git push [remotename] [Localbranchname]
* If you want to submit a local branch test to a remote repository and serve as the master branch of a remote repository, or as a branch of another named Test, the following:

$git Push Origin Test:master//submit local Test branch as remote Master Branch $git push Origin test:test//submit local Test branch as remote T EST Branch



2) Branch (branch) operations related command

View local branch: $ git Branch

View Remote branch: $ git branch-r

Create local branch: $ git branch [name]----Note the new branch is not automatically switched to the current branch when it is created

Toggle Branch: $ git checkout [name]

Create a new branch and immediately switch to the new branch: $ git checkout-b [name]

Delete Branch: $ git branch-d [name]-----the D option deletes only those branches that are already participating in the merge, and cannot be deleted for branches that do not have a merge. If you want to force the deletion of a branch, you can use the-D option

Merge branch: $ git merge [name]----Merge a branch with name [name] with the current branch

To create a remote branch (local branch push to remote): $ Git push origin [name]

Delete Remote branch: $ git push origin:heads/[name] or $ gitpush origin: [Name]
* Create an empty branch: (Remember to submit your current branch modification before executing the command, otherwise it will be forced to delete the clean without regret)

$git symbolic-ref Head Refs/heads/[name]

$rm. Git/index$git CLEAN-FDX



3) version (tag) operation related command

View version: $ git tag

Create version: $ git tag [name]

Delete version: $ git tag-d [name]

View remote version: $ git tag-r

To create a remote version (local version push to remote): $ Git push origin [name]

Delete Remote version: $ git push origin:refs/tags/[name]

To merge a remote warehouse tag to local: $ Git pull Origin--tags

Upload local tag to remote warehouse: $ GIT push origin--tags

Create annotated tag:$ git tag-a [name]-M ' YourMessage '



4) Sub-module (submodule) related Operation command

Add Child module: $ git submodule add [url] [path]

such as: $git submodule add Git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs


Initialize the child module: $ git submodule init----Only run once when the warehouse is first checked out

Update Sub module: $ git submodule update----need to run every time you update or switch a branch

To delete a child module: (4 steps away OH)

1) $ git RM--cached [path]

2 Edit ". Gitmodules" file, delete the related configuration node of the child module

3 edit ". Git/config" file, delete the related configuration node of the child module

4 Manually delete the child module residue directory



5 Ignore some files, folders do not submit

Create a file with the name ". Gitignore" under the warehouse root, write the unwanted folder name or file, and take one row for each element, such as

Target

Bin

*.db



Three, Git command detailed

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, which is similar to SVN's add

git rm: deletes files from the current workspace and index, such as ' git rm app/model/user.rb ', which is similar to SVN rm, Del

git commit: commits changes to the current workspace, similar to SVN's commit, such as ' Git commit-m story #3, add user Model ', the submission must use-M to enter a submit information, this function is similar to SVN commit

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, this feature is similar to SVN 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:

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, and another function is to restore the role of the code, such as ' 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 files and directories to explain: The head file holds the root node information, in fact, the directory structure represents a tree structure, git use 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 (references refer to information about the branches of each tree that you use locally and remotely), it has heads, remotes, stash, tags four subdirectories, respectively, storage for different root, remote version of the library, Four references to git stacks and tags, you can view reference information more clearly by command ' git show-ref '; The logs directory stores 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.



Git vs. SVN comparison

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:


------------


1) Local Increase branch

The Git local and server-side structure is flexible, all versions are stored in a directory, and you only need to switch branches to achieve the effect of working in 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 corresponding to an SVN server address


To give a practical example:

Using SVN as a version control tool, when you are trying to enhance a module and do half the work, the code is not submitted because it changes the behavior of the original module and causes many tests to fail on the code server.

Now if there is a very urgent bug that needs to be handled, it must be done 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.


2) Distributed submission

Git can submit code locally, so in the diagram above, Git is good for breaking down a big task, making a local multiple commit

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


3) Log View

Git's code log is local and can be viewed at any time

SVN logs are on the server and must be downloaded from the server each time the log is viewed

For example: Code server in the United States, log downloads may take 10 minutes each time you look at the work done a few years ago, which is a pain. But if you migrate to git and take advantage of the local nature of git logs, it takes only a few seconds to see all the code histories of a specific task, making it much easier to work and improve efficiency.

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.


v. Summary

It's a simple introduction to Git's basic concepts, some common commands and principles, and you can try to create your own open source project on Google Code or GitHub.


Git Create and use examples:

On Google Code, I created the GIT project: Linux-kernel-source

On GitHub, I refer to the GIT project: hiphop-php

On the Oschina, I share the project: gcc-4.5.2



Reference recommendation:

Git command parameters and usage detailed

Git Version Management Tools

SVN common commands

The comparison of SVN and Git


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.