Git series articles: git basic use

Two days ago, I mainly introduced some basic operations of local Git warehouse. Today, I will introduce the basic operations of remote warehouse.

7 use of remote warehouse

In ordinary projects, we often deal with remote warehouses. Remote warehouse refers to the project version library hosted on the Internet or other network servers. Through the remote warehouse, we can share the work results with other project team members. For the basic use of remote warehouses, we need to know how to add remote warehouses, view which remote warehouses are used in the project, remove remote warehouses that are no longer used, manage remote branches of remote warehouses, and so on.

7.1 clone remote warehouse

As mentioned in the previous article, one of the ways to initialize the Git project is to clone the remote warehouse. The command is:

$ git clone <url>

7.2 display remote warehouse

Use the command git remote to view which remote warehouses are set in the current project, and add the - v option to view the URL address of the remote warehouse

$ git remote
origin
$ git remote -v
origin	https://github.com/jiaoxn/vue-next.git (fetch)
origin	https://github.com/jiaoxn/vue-next.git (push)

If git clone is used to initialize the local Git warehouse, origin is the default name of the cloned remote warehouse and has no special meaning. If git clone - O < alias is used_ Name > set an alias for the remote warehouse, then the output of git remote is the set alias.

7.3 add remote warehouse

Execute git remote add [shortname] [url] to add the remote warehouse, where shortname is the reference name of the remote warehouse.

$ git remote add vue-next https://github.com/jiaoxn/vue-next.git
$ git remote -v
origin	https://gitee.com/jiaoxn/git_learn_note_202102.git (fetch)
origin	https://gitee.com/jiaoxn/git_learn_note_202102.git (push)
vue-next	https://github.com/jiaoxn/vue-next.git (fetch)
vue-next	https://github.com/jiaoxn/vue-next.git (push)

7.4 pull data from remote warehouse

Git provides two ways to pull data from a remote warehouse: git pull < remote_ name> <remote_ branch_ name>:<local_ branch_ Name > and git fetch < remote_ name> <remote_ branch_ name>.

If you clone a remote project using git clone < URL >, then remote_name is origin; If git clone - O < alias is used_ Name > < URL >, then remote_name is alias_nameļ¼› If you add a remote warehouse using git remote add [shortname] [url], then remote_name is shortname.

git fetch will get the data not available in the local warehouse from the corresponding remote warehouse, including the branches contained in the remote warehouse. It should be noted that this command only gets the data locally and will not merge with the files in the local workspace. If you want to merge with the current local branch, you need to execute the git merge command later.

git pull will also automatically obtain data from the remote warehouse, but will merge the remote warehouse branch into the current local branch. It can be considered that this command is a combination of git fetch and git merge.

7.5 push local data to remote warehouse

You can use git push [remote name] [local branch name] command to push the local data to the remote warehouse. For example, you can use git push origin master to push the local master branch to the remote warehouse corresponding to origin.

If you want to smoothly push local data to a remote warehouse, you should pay attention to the following:

  1. The current user needs to have read and write permission to the cloned remote warehouse
  2. After cloning from the remote warehouse, if the remote warehouse is modified before submission, the push will not succeed. You need to pull the latest remote warehouse and merge it into your own working directory, and then push the merged version content to the remote warehouse

7.6 inspection of remote warehouse

In section 7.2, use git remote -v to view the name and corresponding URL address of the remote warehouse set in the project. Here, you can use git remote show [remote name] to display more remote information.

$ git remote show origin
* remote origin
  Fetch URL: https://github.com/jiaoxn/vue-next.git
  Push  URL: https://github.com/jiaoxn/vue-next.git
  HEAD branch: master
  Remote branches:
    >linusborg/improve-render-warning-missing-props      tracked
    add-license-files-to-all-packages-2650               tracked
    akryum/on-server-prefetch                            tracked
    dependabot/npm_and_yarn/babel/parser-7.12.11         tracked
    dependabot/npm_and_yarn/babel/types-7.12.12          tracked
    dependabot/npm_and_yarn/estree-walker-2.0.2          tracked
    dependabot/npm_and_yarn/ini-1.3.8                    tracked
    dependabot/npm_and_yarn/node-notifier-8.0.1          tracked
    feat/expose                                          tracked
    linusborg/feat-warn-reserved-properties              tracked
    linusborg/fix-app-unmount-memleak-2907               tracked
    linusborg/handle-readonly-type-attr-on-textarea-2766 tracked
    linusborg/map-shallow-proxies-seperately-2843        tracked
    master                                               tracked
    postcss-8                                            tracked
    reactive-let                                         tracked
    script-setup-2                                       tracked
    test/types/props-default                             tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

The output result not only displays the name and URL address of the remote warehouse, but also lists which branches of the remote warehouse tracked by the current project and the branch name currently used.

Note the output content of the last four lines. The output content of the last two lines indicates that when git push is executed, the master branch in the local warehouse will be pushed to the master branch in the remote warehouse; The output of the last 3-4 lines indicates that when the git pull command is executed, the master of the remote warehouse will be pulled and merged into the master of the local warehouse.

7.7 rename remote warehouse

You can use the command git remote rename < remote_ name> <new_ remote_ Name > updates the name of the remote warehouse. When updating, the name of the remote branch will also be updated. For example, after updating, the previous < remote_ Name > / Master branch name will change to < New_ remote_ name>/master.

7.8 Delete remote warehouse

You can use the command git remote RM < remote_ Name > delete the specified remote warehouse.

Tags: git

Posted by andrewmay67 on Tue, 19 Apr 2022 05:13:31 +0930