Remotes#

Consider features of working with remotes in git. Check corresponding section in git documentation.

Set up#

Obviously, we need remotes for this section. To avoid repetitive manual operations like creating and deleting repositories in Git storage providers, we’ll need a specific setup.

The following cell shows a Dockerfile that creates an image for containers acting as toy servers. Learn more about setting up a Git daemon in the special section of the official documentation.

cat remote_files/dockerfile
FROM debian:bullseye-slim

# Install git (including git-daemon)
RUN apt-get update && apt-get install -y git && apt-get clean

# Create a directory for repositories and initialize a bare repository
RUN mkdir -p /git/project.git && git init --bare /git/project.git

# Add a file to allow git-daemon to serve the repository
RUN touch /git/project.git/git-daemon-export-ok

# Expose the Git protocol port
EXPOSE 9418

# Start git-daemon to serve repositories
CMD ["git", "daemon", "--verbose", "--export-all", "--base-path=/git", "/git"]

This dockerfile installs all the required packages on Debian Linux and creates an empty git repository, which we will basically use as an example.

Build image:

docker build -f remote_files/dockerfile -t git-daemon . &> /dev/null

Running container:

docker run -itd --rm --name git-daemon -p 9418:9418 git-daemon
92c52918a77da98d2a1221841c9af4bc90a2bc71beec3ce4e2dd2b5012eb448d

Now you can clone the repository into a local folder. The following code performs the cloning and displays the result of a Git commit from the repository.

git clone git://localhost:9418/project.git /tmp/remote
cd /tmp/remote
git status
Cloning into '/tmp/remote'...
warning: You appear to have cloned an empty repository.
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Note: Don’t forget to clean up the environment when you’re done.

docker stop git-daemon
git-daemon

Show remotes#

You can use the git remote command to list the short names of remotes you’re dealing with. With git remove -v you can show specific urls.


The following cell shows the result of the git remote command.

git remote
origin

origin is default name of the remote gitven by git clone.

The following command shows the result of the git remove -v command.

git remote -v
origin	git://localhost:9418/project.git (fetch)
origin	git://localhost:9418/project.git (push)

Now for remote consideration specified fetch and pull URLs.

Add/remove remote#

Use git remote add <remote_name> <remote_url> to add new remote to the git repository. Use git remote remove <remote_name> to remove remote.


The following cell adds remote to the git repository.

git remote add new_remote git://localhost:9418/newremote
git remote -v
new_remote	git://localhost:9418/newremote (fetch)
new_remote	git://localhost:9418/newremote (push)
origin	git://localhost:9418/project.git (fetch)
origin	git://localhost:9418/project.git (push)

The output of git remote -v have an extra remote.

The following cell removes the previously added remote.

git remote remove new_remote
git remote -v
origin	git://localhost:9418/project.git (fetch)
origin	git://localhost:9418/project.git (push)

Set url#

You can update url remote refers to by using command git remote set-url <remote name> <new url>.


The following cell changes ulr for an origin url that doesn’t exist - broken.git.

git remote set-url origin borken.git
git remote -v
origin	borken.git (fetch)
origin	borken.git (push)

The following cell applies git remote set-url again to fix remote url.

git remote set-url origin git://localhost:9418/project.git
git remote -v
origin	git://localhost:9418/project.git (fetch)
origin	git://localhost:9418/project.git (push)