19 | Adding and Removing Remotes |
Git allows you to have as many remote repositories as you like. It’s common to have a different remote for each member of your team in a fully distributed architecture for your repositories. Git requires that each remote have a unique name.
You must tell Git where to access remote repositories. You do this using the git remote add command.[15] It requires two parameters: a name and a repository URL.
The first is simply the short name you use to reference the remote repository by. The name origin is the conventional name for the repository that you clone from. Git uses this convention in several commands that allow you to skip the remote name when you’re working with the origin repository.
The repository URL points to the actual location of the remote repository. This can be in another directory on your system or, more commonly, a repository that is accessible via a network connection. Git can transfer over its own git protocol, over git using SSH to encrypt the data transfer and handle authentication, and over HTTP/HTTPS.
Git sets up tracking branches for you whenever you create a local branch from a remote branch. Adding a repository after you start working locally, though, doesn’t give Git a chance to do that setup. You can add this by removing the local branch and re-creating it from the remote branch. Don’t worry—as long as you make sure all the changes in your local branch have been pushed to your remote repository and your local and remote branch have the same things in them, you aren’t going to lose any commits.
You can remove remote repositories by using the git remote rm command. It removes any tracking branch information in addition to removing the remote definition.
What To Do... |
| prompt> git remote add <name> <repository URL> |
| ... example ... |
| prompt> git remote add tswicegood |
| git://github.com/tswicegood/bobby-tables.git |
| propmt> |
| prompt> git remote rm <name> |
| ... example ... |
| prompt> git remote rm tswicegood |
Run these commands after you push to your remote repository for the first time if you want to set up your local branch as a tracking branch of the remote. As an example, here’s the workflow in a project of mine:
| prompt> git checkout origin/master |
| Note: checking out 'origin/master'. |
| |
| You are in 'detached HEAD' state. ... and so on ... |
| |
| git checkout -b new_branch_name |
| |
| HEAD is now at d7c8880... ignore stuff from virtualenv |
| prompt> git branch -d master |
| Deleted branch master (was d7c8880). |
| prompt> git checkout -b master |
| Switched to a new branch 'master' |
Task 13, Creating and Switching Branches
Task 20, Retrieving Remote Changes
Task 21, Retrieving Remote Changes, Part II
Task 22, Sending Changes to Remotes
18.119.235.79