Git basic command line operations

Source: Internet
Author: User
Tags git commands
Git remote operation details

Ruan Yifeng

Git is currently the most popular version management system. Learning Git has almost become a required skill for developers.

Git has many advantages, one of which is that remote operations are very simple. This article describes five Git commands in detail, their concepts and usage, and understanding the content, you will have a full grasp of Git remote operations.

  • Git clone
  • Git remote
  • Git fetch
  • Git pull
  • Git push

This article is intended for novice users, starting with the simplest, but readers need to understand the basic usage of Git. At the same time, this article covers almost all the common usage of the above five commands, so it also has reference value for skilled users.

1. git clone

The first step in remote operations is to clone a version library from the remote host. In this case, you need to use the git clone command.

$ Git clone <repository URL>

For example, clone the jQuery version Library.

$ Git clone https://github.com/jquery/jquery.git

This command will generate a directory on the local host, with the same name as the version library of the remote host. If you want to specify different directory names, you can use the directory name as the second parameter of the git clone command.

$ Git clone <repository URL> <local directory name>

Git clone supports multiple protocols. In addition to HTTP (s), it also supports SSH, Git, and local file protocols. Below are some examples.

$ Git clone http [s]: // example.com/path/to/repo.git/$ git clone ssh: // example.com/path/to/repo.git/$ git clone git: // your git clone/opt/git/project. git $ git clone file: // opt/git/project. git $ git clone ftp [s]: // example.com/path/to/repo.git/$ git clone rsync: // example.com/path/to/repo.git/

There is another way to write the SSH protocol.

$ Git clone [[email protected] example.com: path/to/repo. git/

Generally, the Git protocol downloads the fastest, and the SSH protocol is used for scenarios requiring user authentication. For details about the advantages and disadvantages of various protocols, refer to the official documentation.

II. git remote

To facilitate management, Git requires that each remote host must specify a host name. The git remote command is used to manage the host name.

If no option is available, the git remote command lists all remote hosts.

$ Git remoteorigin

Use the-v option to view the remote host URL.

$ Git remote-vorigin [email protected]. com: jquery/jquery. git (fetch) origin [email protected]. com: jquery/jquery. git (push)

The preceding command indicates that there is currently only one remote host called origin and its URL.

When cloning the version Library, the remote host used is automatically named as origin by Git. To use another host name, use the-o option of the git clone command.

$ Git clone-o jQuery https://github.com/jquery/jquery.git$ git remotejQuery

The preceding command indicates that the remote host is called jQuery during cloning.

Run the git remote show command with the host name to view the details of the host.

$ Git remote show 

The git remote add command is used to add a remote host.

$ Git remote add 

The git remote rm command is used to delete a remote host.

$ Git remote rm 

The git remote rename command is used to rename the remote host.

$ Git remote rename <original host name> <new host name>
III. git fetch

Once the version library of the remote host has been updated (Git is called commit), you need to retrieve these updates locally and then use the git fetch command.

$ Git fetch <remote host name>

The above command retrieves all updates from a remote host to the local device.

By default, git fetch retrieves updates from all branches. If you only want to retrieve updates from a specific branch, you can specify the branch name.

$ Git fetch <remote host name> <branch name>

For example, retrieve the master branch of the origin host.

$ Git fetch origin master

The retrieved update must be read on the local host in the form of "remote host name/branch name. For example, the master of the origin host must be read by the origin/master.

The-r option of the git branch command can be used to view remote branches, and The-a option can view all branches.

$ Git branch-rorigin/master $ git branch-a * master remotes/origin/master

The preceding command indicates that the current branch of the local host is master and the remote branch is origin/master.

After retrieving updates from the remote host, you can use the git checkout command to create a new branch.

$ Git checkout-B newBrach origin/master

The preceding command indicates that a new branch is created based on the origin/master.

In addition, you can use the git merge command or git rebase command to merge remote branches on the local branch.

$ Git merge origin/master # or $ git rebase origin/master

The preceding command indicates that the origin/master is merged on the current branch.

IV. git pull

The git pull command is used to retrieve updates from a branch on the remote host and merge the updates with a specified branch on the local host. Its complete format is a little complicated.

$ Git pull <remote host name> <remote branch name>: <local branch name>

For example, to retrieve the next branch of the origin host and merge it with the local master branch, you need to write it as follows.

$ Git pull origin next: master

If the remote branch is merged with the current branch, the part after the colon can be omitted.

$ Git pull origin next

The preceding command indicates that the origin/next branch is retrieved and then merged with the current branch. In essence, this is equivalent to running git fetch and git merge.

$ Git fetch origin $ git merge origin/next

In some cases, Git automatically establishes a tracking relationship between the local branch and the remote branch ). For example, during git clone, all local branches are connected to the branch with the same name as the remote host by default. That is to say, the local master branch automatically "tracks" origin/master branch.

Git also allows manual tracing.

Git branch -- set-upstream master origin/next

The above command specifies the master branch to trace the origin/next branch.

If the current branch has a tracing relationship with the remote branch, git pull can omit the remote branch name.

$ Git pull origin

The preceding command indicates that the current local branch is automatically merged with the corresponding remote-tracking branch of the origin host.

If the current branch has only one tracing branch, the remote host name can be omitted.

$ Git pull

The preceding command indicates that the current branch is automatically merged with the only tracing branch.

If the merge operation requires the rebase mode, you can use the -- rebase option.

$ Git pull -- rebase <remote host name> <remote branch name>: <local branch name>
5. git push

The git push command is used to push updates of local branches to remote hosts. Its format is similar to that of the git pull command.

$ Git push <remote host name> <local branch name>: <remote branch name>

Note that the branch push order is written as <source>: <destination>, so git pull is <remote branch >:< local branch>, while git push is <local branch>: <remote branch>.

If the remote branch name is omitted, the local branch is pushed to a remote branch with a "tracing relationship" (usually with the same name). If the remote branch does not exist, it is created.

$ Git push origin master

The command above indicates that the local master branch is pushed to the master branch of the origin host. If the latter does not exist, it will be created.

If the local branch name is omitted, the specified remote branch is deleted, because it is equivalent to pushing an empty local branch to the remote branch.

$ Git push origin: master # equivalent to $ git push origin -- delete master

The preceding command deletes the master branch of the origin host.

If there is a tracing relationship between the current branch and the remote branch, both the local branch and the remote branch can be omitted.

$ Git push origin

The command above indicates that the current branch is pushed to the corresponding branch of the origin host.

If the current branch has only one tracing branch, the host name can be omitted.

$ Git push

If the current branch has a tracing relationship with multiple hosts, you can use the-u option to specify a default host, so that you can use git push without adding any parameters.

$ Git push-u origin master

The above command pushes the local master branch to the origin host, and specifies the origin as the default host, so you can use git push without adding any parameters.

Git push without any parameters. By default, only the current branch is pushed, which is called the simple method. In addition, there is also a matching method that pushes all local branches with corresponding remote branches. Before Git version 2.0, the matching method was used by default. Now, the simple method is used by default. If you want to modify this setting, you can use the git config command.

$ Git config -- global push. default matching # or $ git config -- global push. default simple

In another case, whether or not the corresponding remote branch exists, push all the local branches to the remote host. In this case, you need to use the -- all option.

$ Git push -- all origin

The command above indicates that all local branches are pushed to the origin host.

If the version of the remote host is later than the local version, Git reports an error when pushing. You must merge the differences locally before pushing them to the remote host. In this case, you can use the -- force option if you must push it.

$ Git push -- force origin

The preceding command uses the -- force option. As a result, the updated version on the remote host is overwritten. Unless you are sure to do so, try to avoid using the -- force option.

Finally, git push does not push tags unless the -- tags option is used.

$ Git push origin -- tags

 

 

GIT basic command line operations

 

A. Create A Git repository and A new folder
Git init
 
B. Add files to git indexes
Git add <filename> --- add a single file
Git add * --- add all files

C. Submit it to the local repository
Git commit-m "code submission description"

D. Submit to the remote warehouse
Git push origin master
* ** The master can be changed to any branch you want to push.


Branch:
1. Create a branch called "lee" and switch it over.
Git checkout-B lee

2. Switch back to the main branch
Git checkout master

3. Delete the new branch
Git branch-d lee

4. Before pushing the branch to the remote warehouse, the branch will not be seen by anyone.
Git push origin <branch>

Update and merge
A. Update the local repository
Git pull

B. Automatically merge branches. If multiple branches cause conflicts, manually resolve the conflicts.
Git merge <branch>

C. After merging, add
Git add <branch>

D. We recommend that you use a comparison tool before merging.
Git diff <source_branch> <target_branch>

E. Software release is used to create tags, which must be unique.
E.1 obtain the submission ID
Git log
E.2 create tags
Git tag 1.2.3 submission ID

 

F. Roll back to a previous version

F.1 obtain the submission ID
Git log
F.2 roll back to the specified version
Git reset -- hard commit ID

G. After the reset command is used, the log is not fully informative. In this case, we need to use reflog and then reset
Git reflog

H. Color git output
Git config color. ui true

I. View Remote branches and local branches
Git branch-

J. push a specified branch name to the remote branch. If the remote server does not have this branch, create
Git push origin <brancheName>

K. Delete a remote branch
Git push origin -- delete <branchName>

L. If you use rm to accidentally delete the file, you can restore it in two steps.
1. git reset HRAD file name
2. git checkout -- file name

M. Delete an object
Git rm file name (delete files in the working directory and local repository at the same time)
Git rm -- cached file name (delete the local repository file without affecting the working directory)

N. Change the upload address.
Git remote set-url origin ssh: // [email protected]/~ /WeiYu

O. Create local git and server address associations based on the server address
Git remote add origin ssh: // [email protected] _ server/var/lib/scm/git/lht/test. git

  

Collection of common Git Operation Commands

1) remote warehouse commandsCheck out repository: $ git clone git: // github.com/jquery/jquery.git#remote repository :$ git remote-v add remote repository: $ git remote add [name] [url] delete remote repository: $ git remote rm [name] pull remote repository: $ git pull [remoteName] [localBranchName] push remote repository: $ git push [remoteName] [localBranchName]2) branch Operation CommandsView local branch: $ git branch view remote branch: $ git branch-r create local branch: $ git branch [name] ---- note that after the new branch is created, it will not automatically switch to the current branch: $ git checkout [name] create a new branch and immediately switch to the new branch: $ git checkout-B [name] delete the branch: the $ git branch-d [name] -----d option can only delete the branches that have already been merged. To forcibly delete a branch, use the-D option to merge the branch: $ git merge [name] ---- merge the branch named [name] with the current branch to create a remote branch (push local branch to remote ): $ git push origin [name] delete remote branch: $ git push origin: heads/[name]3) version (tag) related commandsView version: $ git tag created version: $ git tag [name] delete version: $ git tag-d [name] View Remote version: $ git tag-r create remote version (local version push to remote): $ git push origin [name] delete remote version: $ git push origin: refs/tags/[name]4) submodule related Operation CommandsAdd sub-Module: $ git submodule add [url] [path] initialize sub-Module: $ git submodule init ---- update the sub-module only once when the first warehouse is detected: $ git submodule update ---- run the following command to delete the sub-module after each update or switch branch: $ git rm -- cached [path]5) ignore some files and folders and do not submit them.Create a file named ". gitignore" in the root directory of the repository and write the folder name or file that is not required. Each element occupies one row, for example, targetbin *. db.

Git basic command line operation ()

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.