Remotes
What's a remote?¶
Suppose you're working on a software project with a friend. Presumably, each of you have your own isolated development environments. So, how do you and your friend share updates to the codebase?
A remote is a centralized place for you and your friend to upload and download changes to the commit graph, so that your code stays "in sync".
Info
In general, a remote is just a copy of a repository stored somewhere else. This includes
How does it work?¶
- Use
git push
to upload your commits to the the remote. - Use
git fetch
to download commits from the remote to your local environment.
What if I accidentally make changes that conflict with the remote?¶
For example, suppose your local commit graph matches the remote's commit graph.
Then the main branch on the remote receives a new commit.
Around the same time, you make a separate commit to main in your local repo.
Now your commit graph is misaligned with the remote. (Git would say that your local commit graph has diverged from the remote.)
This scenario should be avoided!
If each developer works on a separate branch (not main), they don't have to worry about their commit graphs becoming misaligned like above. Typically each developer will push their branch changes to the remote when ready, and the changes will be merged into main on the remote.
Each developer should periodically pull the main branch into their local repo and adjust their feature branches accordingly to stay up to date.
How do I create a remote?¶
There are lots of ways, but perhaps the most common method in practice is to use Github.
- Sign up or Sign in
- Click the New button under Recent Repositories
-
Give the remote repo a name and description before clicking the Create button
-
Run the commands suggest by Github on your local machine to connect your local repo to the remote repo
What is git clone
?¶
git clone
is used to download a remote repository to your local machine. Use git clone
if
- The remote repo already exists and has commits
- You haven't set up a local version of the repo
A common scenario for this is..
Suppose Bob has been working on project X for some months. Now Carrie wants to get involved in the project. Carrie should clone the remote repo to her local machine.
Cloning is something you do once. After you've cloned a remote, you should use git fetch
or git pull
to stay in sync.
How do I link my local repo to the remote?¶
Example
git remote add origin https://github.com/zoidberg1/my-remote.git
What's origin
?¶
origin is just a common name people use to reference their remote (1). You can rename the remote, if you'd like (2).
-
Other people won't see this name. It's only stored in your local repo, as an easy way to reference that ugly URL.
-
git remote rename <old-name> <new-name>
Can I have multiple remotes?
Yes! Git is designed with a distributed architecture in mind.
How do I list my remotes?¶
Use git remote
zoidberg:~$ git remote -v
origin https://github.com/zoidberg1/my-remote.git (fetch)
origin https://github.com/zoidberg1/my-remote.git (push)
(-v
is short for --verbose
)
What is git push
?¶
git push
is used to upload your local changes to a remote.
What is git fetch
?¶
git fetch
is used to sync your local machine with a remote. git fetch
downloads stuff in the remote that you local repo is missing.
What is git pull
?¶
git pull
is (usually) equivalent to running git fetch
and git merge
in succession.