Manage and use Git remote Repository

Source: Internet
Author: User

To participate in collaboration with any Git project, you must understand how to manage remote repositories. A remote repository is a project repository hosted on the network. There may be many projects, some of which can only be read and others can be written. When developing a project in collaboration with others, you need to manage these remote repositories to push or pull data and share their work progress. Manage remote repositories, including adding remote repositories, removing obsolete remote repositories, managing various remote database branches, and defining whether to track these branches. This section describes how to manage and use remote databases in detail.

View the current remote database

To view which remote repositories are currently configured, run the git remote Command to list the short names of each remote repository. After cloning a project, you can see at least one remote database named origin. Git uses this name by default to identify the original repository you cloned:

$ git clone git://github.com/schacon/ticgit.gitInitialized empty Git repository in /private/tmp/ticgit/.git/remote: Counting objects: 595, done.remote: Compressing objects: 100% (269/269), done.remote: Total 595 (delta 255), reused 589 (delta 253)Receiving objects: 100% (595/595), 73.31 KiB | 1 KiB/s, done.Resolving deltas: 100% (255/255), done.$ cd ticgit$ git remoteorigin

You can also add the-v option to display the corresponding clone address:

$ git remote -vorigin git://github.com/schacon/ticgit.git

If there are multiple remote repositories, this command is all listed. For example, in my Grit project, you can see:

$ cd grit$ git remote -vbakkdoor git://github.com/bakkdoor/grit.gitcho45 git://github.com/cho45/grit.gitdefunkt git://github.com/defunkt/grit.gitkoke git://github.com/koke/grit.gitorigin git@github.com:mojombo/grit.git

In this way, I can easily pull their submissions from these users' repositories to the local machine. Note that the addresses listed above only use ssh url links for origin, so only this repository can push data (we will explain the reason in chapter 4 ).

Add remote Repository

To add a new remote repository, you can specify a simple name for future reference and run git remote add [shortname] [url]:

$ git remoteorigin$ git remote add pb git://github.com/paulboone/ticgit.git$ git remote -vorigin git://github.com/schacon/ticgit.gitpb git://github.com/paulboone/ticgit.git

Now we can use the string petabytes to represent the corresponding warehouse address. For example, to capture all the information that Paul has but does not exist in the local repository, run git fetch pb:

$ git fetch pbremote: Counting objects: 58, done.remote: Compressing objects: 100% (41/41), done.remote: Total 44 (delta 24), reused 1 (delta 0)Unpacking objects: 100% (44/44), done.From git://github.com/paulboone/ticgit* [new branch] master -> pb/master* [new branch] ticgit -> pb/ticgit

Now, Paul's master branch can be fully accessed locally. The corresponding name is pb/master. You can merge it into one of your branches, or switch to this branch to see some interesting updates.

Capture data from a remote warehouse

As we can see before, you can use the following command to capture data from a remote warehouse to a local machine:

$ git fetch [remote-name]

This command pulls all the data that is not in your local warehouse from the remote warehouse. After running, you can access all the branches in the remote warehouse locally, merge one of them to the local machine, or just retrieve a branch to find out. (We will discuss in detail the concept and operations of branches in Chapter 3 .)

If a repository is cloned, the remote repository is automatically assigned to the origin. Therefore, the git fetch origin captures all updates uploaded to this remote repository since your last clone (or updates submitted by others since the last fetch ). It is important to remember that the fetch command only pulls remote data to a local warehouse and does not automatically merge the data into the current work branch. You can only merge the data manually when it is ready. (Description: You need to create a remote repository in advance, and then execute: git remote add [repository name] [repository url], git fetch [remote repository name], you can capture the remote repository data to the local machine, and then use git merge remotes/[repository name]/master to connect the remote repository merge to the local current branch. This branch method is suitable for independent-integrated development, that is, after each branch is developed and tested, it is integrated. For example, Android Framework and AP development.

You can use the -- bare option to run git init to set an empty repository. This will initialize a repository that does not contain a working directory.

$ cd /opt/git$ mkdir project.git$ cd project.git$ git --bare init

In this case, Join, Josie, or Jessica can add it as a remote repository, push a branch, and upload the project of the first version to the repository .)

If you have set a branch to track the branch of a remote repository (see the following section and chapter 3), you can use the git pull command to automatically capture data, then, the remote branch is automatically merged to the current branch in the local repository. In our daily work, we often use this method fast and well. In fact, by default, the git clone command automatically creates a local master branch to track the master branch in the remote warehouse (assuming that the remote warehouse does have a master branch ). Therefore, we generally run git pull to capture data from the original cloned remote repository and merge the data to the current branch in the working directory.

Push data to remote warehouse

To share the current results with others, you can push data in the local repository to a remote repository. The command to implement this task is simple: git push [remote-name] [branch-name]. If you want to push the local master branch to the origin server (again, the clone operation will automatically use the default master and origin names), run the following command:

$ git push origin master

This command will complete the task on schedule only when the cloned server has the write permission or no one else is pushing data at the same time. If you have already pushed several updates before pushing data, your push operation will be rejected. You must first capture their updates to the local device and add them to your project before pushing them again. For details about pushing data to a remote warehouse, see Chapter 3.

View remote Repository Information

Run the git remote show [remote-name] command to view details of a remote repository. For example, to view the cloned origin repository, run:

$ git remote show origin* remote originURL: git://github.com/schacon/ticgit.gitRemote branch merged with 'git pull' while on branch mastermasterTracked remote branchesmasterticgit

In addition to the corresponding clone address, it also provides a lot of additional information. It kindly tells you that if it is in the master branch, you can use the git pull command to capture data and merge it locally. All remote branches in the tracking status are also listed.

In actual use, the information provided by git remote show may look like this:

$ git remote show origin* remote originURL: git@github.com:defunkt/github.gitRemote branch merged with 'git pull' while on branch issuesissuesRemote branch merged with 'git pull' while on branch mastermasterNew remote branches (next fetch will store in remotes/origin)cachingStale tracking branches (use 'git remote prune')libwalkerwalker2Tracked remote branchesaclapiv2dashboard2issuesmasterpostgresLocal branch pushed with 'git push'master:master

It tells us what the default push branch is when running git push ). It also shows which remote branches have not been synchronized to the local database ), which remote branches have been synchronized to the local server have been deleted on the remote server, and which branches will be automatically merged when running git pull: issues and master branches listed in the first four rows ). (This command can also view the relationship between the local branch and the remote warehouse branch .)

Delete and rename a remote Repository

In the new version of Git, you can use the git remote rename command to modify the short name of a remote repository. To change pb to paul, run the following command:

$ git remote rename pb paul$ git remoteoriginpaul

Note that renaming a remote Repository also changes the name of the corresponding branch. The original pb/master branch is now paul/master.

When a remote repository server is migrated, the original cloned image is no longer used, or a participant no longer contributes code, you need to remove the corresponding remote repository and run the git remote rm command:

$ git remote rm paul$ git remoteorigin

Original article: http://selboo.com.cn/post/928/

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.