Common git commands

Source: Internet
Author: User
Tags using git git commands

The current development of new projects using version control tools are basically git, old projects or SVN, online git resources are many, many and miscellaneous. I have compiled a study of git, hoping to help students who are learning about git.

First, Git commands to recognize

Before formally introducing GIT commands, let's introduce Git's basic commands and actions, and have a general understanding of GIT commands.

Example: initialization from a git repository, usually in two ways:

1) git clone: This is a simpler way to initialize, when you already have a remote git repository, you only need to clone a copy locally

Example: Git clone git://github.com/someone/some_project.git some_project

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

2) 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 and 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,

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

The above command will add the URL address ' git://github.com/someone/another_project.git ', the name is origin of the remote server, in the future to commit the code only need to use the Origin alias

Second, Git common commands

1) Remote Warehouse related commands


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

View remote repositories: $ git remote-v

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

Delete Remote repository: $ git remote rm[name]

Modify remote repository: $ git remoteset-url--push [name] [Newurl]

Pull remote repository: $ git pull[remotename] [localbranchname]

Push remote repository: $ git push[remotename] [localbranchname]


* If you want to submit a local branch test to the remote repository as the master branch of the remote repository, or as another branch called Test, as follows:

$git Push Origintest:master//submit local Test branch as remote Master Branch

$gitpush Origin Test:test//submit local test branch as remote Test branch

2 ) Branch (branch) operations related commands

View local branch: $ git Branch

View Remote branch: $ git branch-r

Create local branch: $ git branch[name]----Note that the new branch will not automatically switch to the current branch after it is created

Switch Branch: $ git checkout[name]

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

Delete branch: git branch-d[name]-----d option can only delete branches that have already joined the merge and cannot be deleted for branches that do not have a merge. If you want to forcibly delete a branch, you can use the-D option

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

Create a remote branch (local branch push to remote): $ git pushorigin [name]

Delete Remote branch: git push origin:heads/[name] or $ gitpush origin: [Name]


* Create an empty branch: (Before executing the command remember to submit your current branch of the changes, otherwise it will be forced to delete the clean without regret)

$git Symbolic-refhead Refs/heads/[name]

$rm. Git/index

$gitclean-FDX

3 ) version (tag) operation related commands

View version: $ git tag

Create version: $ git tag [name]

Delete version: $ git tag-d [name]

View remote version: $ git tag-r

Create a remote version (local version push to remote): $ git pushorigin [name]

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

Merge the remote repository's tag to local: Git pull Origin--tags

Upload local tag to remote repository: $ Git push origin--tags

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

4) sub-module (submodule) Related Operations Command

Add sub-module: $ git submodule add[url] [path]

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

Initialize submodule: $ git submoduleinit----run only once when the warehouse is first checked out

Update submodule: git submoduleupdate----need to run every time you update or switch branches



To delete a submodule: (4 steps away OH)

1) $ git rm--cached[path]

2) Edit the ". Gitmodules" file to delete the relevant configuration node of the Submodule

3) Edit the ". Git/config" file to delete the relevant configuration node of the Submodule

4) Manually delete the remaining sub-modules directory

5 ignore some files, folders do not commit

Create a file with a name of ". Gitignore" under the repository root and write an unwanted folder name or file with one line per element, such as

Target

Bin

*.db

Three, Git command detailed

Now that we have the local and remote repositories, 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 originmaster ' 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, which is added to the Git index to be recorded in the version history, which is a step before committing, such as ' Git addapp/model/user.rb ' will add app/ MODEL/USER.RB file into the git index, which is similar to the SVN add

git rm : removes files from the current workspace and index, such as ' Git rmapp/model/user.rb ', which is similar to the SVN rm,del

git commit : commits changes to the current workspace, similar to the SVN commit command, such as ' Git commit-mstory #3, add user Model ', which must be submitted with-m to enter a commit message, similar to SVN's Commit

git push : Update the local commit code to the remote repository, such as ' Git push origin ' to update the local code to the remote repository named Orgin

git log : View history log, which is similar to SVN's log

git revert : Restore a version of the modification, you must provide a specific git version number, such as ' git revert bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ', git version number is a generated hash value

The commands above are almost all common to each version control tool, so let's try some of the git-unique commands:

git branch : Add, delete, check, and so on branch, for example ' git branchnew_branch ' will create a new branch called New_branch from the current working version, ' Git branch-dnew_branch ' will force the deletion of the name new _branch's branch, ' Git branch ' will list all the local branches

git checkout : GIT's checkout has two functions, one is to switch between different branch, such as ' git checkoutnew_branch ' will switch to New_branch branch, and another function is to restore the function of the code, such as ' Git CHECKOUTAPP/MODEL/USER.RB ' will update the user.rb file from the last submitted version, and all uncommitted content will be rolled back

git rebase : The following two illustrations will be more clear, after the rebase command is executed, the branch is actually moved from C to G, so that the branches have 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 commits, where C is the version number is BBAF6FB5060B4875B18FF9FF637CE118256D6F20, we executed the ' Gitreset Bbaf6fb5060b4875b18ff9ff637ce118256d6f20 ' So there's only a-c three committed versions.

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. This command will be used to personalize your git and create a unique git for you.

git tag : You can tag a specific version so you don't have to memorize complex version number hashes, such as you can use ' Git tagrevert_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 several 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 all the different references you have in the current version control directory (refers to the information about the branches of each tree that you use locally and remotely), and it has heads, remotes, stash, tags, four subdirectories, stored separately for different roots, remote repository, Git stacks and tags are four kinds of references, you can use the command ' git show-ref ' to see the 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.

Four, Git with the SVN Compare

SVN(Subversion) is currently the most used version control tool. Compared to it, the biggest advantage ofGit is the two point: it's easy to add branching and distributed features locally.

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

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

1) Add local branch

The Git local and server-side structures are flexible and all versions are stored in a single directory, and 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, can only maintain a number of different copies locally, each copy corresponding to an SVN server address

To give a practical example:

Using SVN as the version Control tool, when trying to enhance a module, half of the work, because it would change the behavior of the original module caused many tests on the code server failed, so did not commit the code.

At this time, if there is a very urgent bug 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.

2) Distributed submissions

Git can commit the code locally, so in the diagram above, git facilitates the decomposition of a large task for local multiple commits

SVN can only make a large number of one-time changes locally, resulting in a huge risk of future consolidation to the trunk

3) Log View

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

For example: Code server in the United States, the log download can take up to 10 minutes each time you look at the work done a few years ago, which cannot be said to be a pain. But if you're migrating to Git, using the native features of Git logs to see all the code history for a specific task, it takes only a few seconds, and it's much easier to work and more efficient.

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.

Five, Summary

Here's a quick 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.

Common git commands

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.