Original address: http://blog.csdn.net/ithomer/article/details/7529022
-------------------------------
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 this directory and use ' Git Init ' command to initialize, Git will later version the file under this directory, if you need to put it on the remote server, you can create a directory on the remote server, and the accessible URL record, 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 remote set-url--push [name] [Newurl]
Pull remote repository: $ git fetch [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 Origin Test:master//submit local Test branch as remote Master Branch $git push Origin test:test//submit local test branch as remote Test Branch
2) Branch (branch) operation 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: $ git checkout-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 push origin [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-ref HEAD Refs/heads/[name]
$rm. Git/index $git CLEAN-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 push origin [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]
Example: $git submodule add Git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs
Initialize submodule: $ git submodule init----run only once when the warehouse is first checked out
Update submodule: git submodule update----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 submit
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 origin Master ' is to update the code of 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 ' The app/model/user.rb file is added to the Git index, which is similar to the SVN add
git rm: removes files from the current workspace and index, such as ' git rm app/model/user.rb ', which is similar to SVN's rm, Del
git commit: commits the changes to the current workspace, similar to the SVN commit command, such as ' Git commit-m story #3, ' Add user Model ', must be submitted with-m to enter a commit message, which is similar to SVN's commit
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 the history log, which is similar to the SVN log
git revert: to restore a version of the modification, 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:
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, such as ' Git checkout new_branch ' will switch to New_branch branch, and another function is to restore the code, such as ' Git Checkout app/model/user.rb ' will update the user.rb file from the last submitted 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