Remote git
While you can totally use a git repository for your own personal use, git is really built for collaboration with others.
So here is a shocker
A remote git repository does not actually have to be remote. A bit misleading, yes. A remote git repository is just a copy of the local repository somewhere else.
Exercise: Create a new repo remote-git
and initialize it
- Create a new directory
remote-git
and navigate into it. Do not do this inside an existing git repository.- Initialize the repository.
Solution: Create a new repo remote-git
and initialize it
mkdir remote-git
cd remote-git
git init
This is distributed version control
So, a remote is just another git repository that is of the same project and has changes we may need or may need to share our changes with.
To manage remotes, we have the git remote
command. Read more with man git-remote
.
To add a remote, we use the git remote add <name> <uri>
command.
The URI can be a path to a directory or a URL, like SSH or HTTPS.
Exercise: Add learn-git
as a remote to the remote-git
repository
- Add a remote called
learn-git
to theremote-git
repository.- The URI for the remote is
<path-to-your-learn-git-directory>
.- Use
origin
as the name of the remote. This is good practice for the authority repo.- Verify that the remote has been added.
Solution: Add learn-git
as a remote to the remote-git
repository
Here
<path-to-your-learn-git-directory>
is the path to thelearn-git
directory on your system. We use../learn-git
as path in this example.
git remote add origin ../learn-git
We can check the remotes by using the git remote -v
command.
git remote -v
Output:
origin ../learn-git (fetch)
origin ../learn-git (push)
Good practices with remotes
Remote is a project repository
There is a single source of truth, which is the remote project repository. This should be named origin
.
Remote is a fork
In this case the fork remote repository should be named origin
and the project repository should be named upstream
.
Now we will need to merge our changes from learn-git
into remote-git
.
Fetch
All state can be fetched from a remote repository. We use the git fetch
command for this.
This operation does not affect currently checked out branches—just the history about them that is in origin/*
.
(you should do that now)
git fetch
The output will look something like this. We got all the information about branches from learn-git
.
Output:
remote: Enumerating objects: 28, done.
remote: Counting objects: 100% (28/28), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 28 (delta 5), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (28/28), 2.12 KiB | 57.00 KiB/s, done.
From ../learn-git
* [new branch] feature -> origin/feature
* [new branch] feature-delete-test -> origin/feature-delete-test
* [new branch] feature-rebase-main -> origin/feature-rebase-main
* [new branch] feature-two -> origin/feature-two
* [new branch] main -> origin/main
So now we are up to date, right?
git log
(try it now)
We can use
git log origin/main
to see the log of themain
branch in the remote repository.
We can also use
git branch -a
to see all the branches, including the remote branches.
Question: Can you think of a way to get the changes from the remote?
We can do a merge.
git merge origin/main
But there is also git pull
, which we will cover next.