2.Git Basics
2.5.GitBasics - Working with Remotes
To be able to collaborate on any Git project, you need to know how to manage your remote repositories. Remote repositories are versions of your project that are hosted on the internet or network somewhere. You can have several of them, each of which generally is either read-only or read/write for you. Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work. Managing remote repositories includes knowing how to add remote repositories, remove remotes that are no longer valid, manage various remote branches and define them as being tracked or not, and more. In this section, we’ll cover some of these remote-management skills.
note:
Note Remote repositories can be on your local machine. It is entirely possible that you can be working with a “remote” repository that is, in fact, on the same host you are. The word “remote” does not necessarily imply that the repo is somewhere else on the network or Internet, only that it is elsewhere. Working with such a remote repo would still involve all the standard pushing, pulling and fetching operations as with any other remote.
Showing your remotes
To see which remote servers you have configured, you can run the git remote
command. It lists the shortnames of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see origin
— that is the default name Git gives to the server (you cloned from:
$ git clone https://...
...
$ cd ticgit
$ git remote
origin
You can also specify -v
, which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote:
$ git remote -v
origin https://github.com/schacon/ticgit (fetch)
origin https://github.com/schacon/ticgit (push)
If you have more than one remote, the command lists them all.
Notice that these remotes use a variety of protocols; we’ll cover more about this in Getting Git on a Server.
Adding Remote Repositories
We’ve mentioned and given some demonstrations of how the git clone
command implicitly adds the origin
remote for you.
how to add a new remote?
run git remote add <shortname> <url>
$ git remote add pd https://github.com/paulboone/ticgit
Fetching and Pulling from Your Remotes
$ git fetch <remote>
This command pulls down all the data from that remote project that you don’t have yet. After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time.
It’s important to note that the git fetch
command only downloads the data to your local repository — it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
If your current branch is set up to track a remote branch (see the next section and Git Branching), you can use the git pull
command to automatically fetch and then merge that remote branch into your cur branch. and by default, the git clone
command automatically sets up your local master
branch to track the remote master
branch (or whatever the default branch is called)
you can run this to push any commits you’ve done to the server:
$ git push origin master
This command works only if …and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to fetch their work first and incorporate it into yours.
[ incorporate ]: to include sth so that it forms a part of sth. 吸收 absorb, integrated, contain, involve. -corporate,全体的 公司的
Inspecting a Remote
see more information about a remote
$ git remote show origin
* remote origin
Fetch URL: https://github.com/schacon/ticgit
Push URL: https://github.com/schacon/ticgit
HEAD branch: master
Remote branches:
master tracked
dev-branch 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)
master merges with remote master
For example, it tells you that if you’re on the master
branch and you run git pull
, it will automatically merge the remote’s master
branch into the local one after it has been fetched.
It also tells you which remote branches on the server you don’t have.