Elena' s AI Blog

Git Remotes

24 Jun 2024 (updated: 03 Jul 2026) / 9 minutes to read

Elena Daehnhardt


Midjourney AI-generated art, June 2024: teamwork with many computers, Bob and Alice work together, HD, Canon lens


TL;DR:
  • Use 'git remote add origin URL' to connect repos, 'git fetch' to update, 'git push' to share changes. Remotes enable backup, collaboration, and version control—essential for team workflows.

Previous: Part 22 — The Token Way to GitHub Security

Next: Part 24 — Git Checkout for overwriting directories from different branches

Overview: Managing Git Remote Repositories

This post is about managing remote repositories in Git. We explore tasks such as adding, renaming, removing remotes, and updating remote URLs. We also practice fetching, pulling, and pushing changes to and from remote repositories.

What Are Git Remotes? Definition and Purpose

A Git remote is a named reference to a version of your repository hosted on another computer or platform such as GitHub or Bitbucket. Git remotes connect your local project to those copies, letting you back up, share, and synchronise your code across machines.

Are Git Remotes similar to Git branches? Remotes are not branches, but they work together. Branches are like alternate timelines within your repository, while remotes are links to entirely different repositories (potentially with their own sets of branches). You can have branches on your local and remote repositories, and Git helps keep them in sync.

Using Git Remotes: Best Practices

You can use Git remotes while working in a team or alone. It is a good idea to follow best practices, such as:

  1. Use clear names like “origin” (main) or “upstream” (original project).
  2. Fetch Often to stay updated and avoid conflicts.
  3. Push with caution and double-check before sharing changes.

Git Remotes for Solo Coding: Backup and Version Control

Imagine Bob, a solo coder working on his passion project. He uses Git to track changes but wants an extra layer of security and organization. That’s where remotes come in.

Bob creates a remote repository on GitHub, calling it “origin.” Now, he has two versions of his project:

  1. Local Repository: On his computer, he actively codes and experiments.
  2. Remote Repository (“origin”): On GitHub, serving as a secure backup and a way to track his project’s history.

The remotes are essential for these:

  1. Backup: If Bob’s computer crashes, his code is safe and sound on GitHub. He can easily clone the “origin” repository to a new machine and pick up where he left off.
  2. Version Control: Bob can push different versions of his project to the remote, creating snapshots in time. If he ever needs to revert to an older version, it’s on GitHub.
  3. Organization: Remotes help Bob keep his project organized and separate from other projects he might be working on.
  4. Sharing (Optional): If Bob decides to share his project with others later, he can easily grant them access to the “origin” repository.

Git Remotes for Team Collaboration

Now, imagine Alice joining Bob’s project. Remotes make their collaboration seamless:

  1. Alice Clones: Alice uses git clone to get a copy of the project from the “origin” repository.
  2. Stay in Sync: Bob and Alice use git push and git pull to share their changes and keep their local copies up-to-date.
  3. Work on Different Features: They create separate branches and push them to the remote to work independently.
  4. Merge Their Work: They merge their branches on the remote to combine their contributions.

Should you be interested in teamwork with Git, read my post Collaboration in GitHub.

Git Remote Commands Reference

The most useful commands for working with Git Remotes are (see Git Basics - Working with Remotes):

  • git remote add <name> <url>: Adds a new remote.
  • git remote show <name>: Inspects a remote.
  • git remote -v: Shows your remotes and their URLs.
  • git fetch <name>: Gets updates from the remote without merging.
  • git pull <name> <branch>: Downloads and merges remote changes.
  • git push <name> <branch>: Uploads your local changes to the remote.
  • git remote rename <old-name> <new-name>: Renames a remote.
  • git remote remove <name>: Removes a remote.
  • git remote set-url <name> <new-url>: Updates a remote’s URL.

Error Fix: “fatal: remote origin already exists”

If git remote add origin <url> returns fatal: remote origin already exists, the name origin is already registered. Point it at the new URL with git remote set-url origin <url>, or remove it first with git remote remove origin and re-add it. For a full walkthrough, see Git: origin already exists — managing remotes.

Using Personal Access Tokens for Git Authentication

Previously,I have described tokens’ usage and setup instead of passwords to access your remote repositories. Create them in your account settings on GitHub or Bitbucket.

Why is it great to use Tokens:

  • Enhanced Security: Tokens offer a more secure way to authenticate than storing your plain-text password in Git configurations.
  • Fine-Grained Permissions: You can grant tokens specific access rights, limiting potential damage in case of compromise.
  • Platform Requirements: Many platforms (including GitHub) have transitioned to requiring token-based authentication for Git operations over HTTPS.

**Important security considerations: **

It is important to treat tokens like passwords and never share them publicly or expose them in code commits. It’s good practice to periodically rotate (revoke and create new) tokens to minimize risk. Only grant tokens the minimum necessary permissions.

Creating a Token

  1. GitHub (Example):
    • Go to your GitHub settings.
    • Navigate to “Developer settings” -> “Personal access tokens”.
    • Click “Generate new token” and provide a descriptive name.
    • Select the necessary scopes (permissions) for your Git operations (e.g., “repo” for complete control of private repositories).
    • Click “Generate token” and securely store the generated token.
  2. Other Platforms:
    • Consult your Git hosting provider’s documentation for instructions on creating access tokens or app passwords.

I often use tokens to clone my remote repositories to my laptop or desktop computers like this:

git clone https://<token>@github.com/<my_user_name>/<repo>

When cloned, I have two remotes saved, respectively, for push and fetch, with the token saved:

(base) My-fancy-comp edaehn.github.io % git remote -v
origin  https://<token>@github.com/edaehn/<repo> (fetch)
origin  https://<token>@github.com/edaehn/<repo> (push)

Embedding a Token in the Remote URL (Less Secure)

Please notice that embedding your token directly into the remote URL is less secure as it exposes your token in your Git configuration files. However, it can be helpful in limited scenarios (e.g., scripts).

Moreover, you can use a Git credential helper (like git-credential-cache) to store your token after the first login securely.

Git Credential Helper (Recommended)

This is the most user-friendly and secure approach:

  1. Install a Credential Helper:
    • If you haven’t already, install a Git credential helper like git-credential-cache or git-credential-store.
  2. Configure Git:
    • Tell Git to use the credential helper:
      git config --global credential.helper cache
      

      (Replace cache with store if using git-credential-store)

Now, the first time you perform an action that requires authentication (like git push), you’ll be prompted for your username and token:

git push origin <branch> 
# You'll be prompted for your GitHub username and token once (stored securely by the helper)

The credential helper will securely store it; you won’t need to enter it again for subsequent commands.

Conclusion

A Git remote is a named link to a hosted copy of your repository that serves as backup, version history, and the synchronisation point for team collaboration. This post explains how to add, rename, remove, and update remote repositories confidently. Additionally, you’ll have the knowledge to fetch, pull, and push changes effectively.

It is excellent to use version control and know that your code is safely stored in remote repositories, the history of changes is tracked, and you can always revert to previous states if needed. Remotes act as backups, ensuring your project isn’t lost if your local repository is compromised.

Good luck with your projects, and all the best!

Git Remotes FAQ

How do I connect a local Git repository to a remote?

Run git remote add origin <url> to register the remote, then git push -u origin main to upload your commits and set the upstream branch.

What is the difference between git fetch and git pull?

git fetch downloads remote changes without merging them into your working branch, while git pull downloads and merges them in one step.

How do I change a Git remote’s URL?

Run git remote set-url origin <new-url>. Verify the change with git remote -v.

How do I authenticate Git over HTTPS?

Use a personal access token instead of a password. Generate one in your GitHub or Bitbucket account settings and supply it when prompted, or store it with a credential helper.

References

1. The token way to GitHub Security

2. Collaboration in GitHub.

3. Git Basics - Working with Remotes

4. git-credential-cache - Helper to temporarily store passwords in memory

5. Reverting commits in GitHub

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.





Citation
Elena Daehnhardt. (2024) 'Git Remotes', daehnhardt.com, 24 June 2024. Available at: https://daehnhardt.com/blog/2024/06/24/git-remotes/
All Posts