Lesson 28 of 60 – Git Remote
47%

Git Remote

A Git remote is a connection to another repository, usually a repository hosted on a platform such as GitHub. Remote repositories allow developers to share code and synchronize changes between local and remote repositories.

Note: The most common remote name is origin. It is normally used to refer to the remote repository from which a project was cloned or to which the project was connected.

1. What is a Git Remote?

A Git remote is a reference to another Git repository.

For example, a local project can be connected to a repository hosted on GitHub:

Local Repository
       ↓
     origin
       ↓
GitHub Repository

2. Why Use Remote Repositories?

Remote repositories are useful for sharing and synchronizing project files.

  • Back up project code
  • Share code with other developers
  • Collaborate on projects
  • Store repositories online
  • Synchronize local and remote changes
  • Work with distributed development teams

3. Git Remote and GitHub

Git itself manages repositories locally. GitHub can host a remote repository that your local Git repository connects to.

Local Git Repository
        ↕
   Remote Repository
        ↕
      GitHub

4. The Origin Remote

When a repository is cloned, Git commonly names the original remote repository origin.

origin → Remote Repository

You can use origin in commands such as:

git push origin main

5. Viewing Remote Repositories

Use the following command to see configured remote names:

git remote

Example output:

origin

6. Viewing Remote URLs

To display remote names and their URLs:

git remote -v

Example:

origin  https://github.com/user/project.git (fetch)
origin  https://github.com/user/project.git (push)

7. Adding a Remote

Use git remote add to connect a local repository to a remote repository.

git remote add origin https://github.com/user/project.git

Here, origin is the remote name and the URL identifies the remote repository.

8. Remote Name

A remote has a name that you use in Git commands.

origin
upstream
company

The name origin is conventional, but Git allows other remote names.

9. Remote URL

A remote URL tells Git where the remote repository is located.

Example using HTTPS:

https://github.com/user/project.git

A remote can also use an SSH URL.

git@github.com:user/project.git

10. Fetch URL and Push URL

A remote can have separate URLs for fetching and pushing.

git remote -v

Typical output contains:

origin  https://github.com/user/project.git (fetch)
origin  https://github.com/user/project.git (push)

11. Getting Detailed Remote Information

Use:

git remote show origin

This displays detailed information about the origin remote, including remote branches and tracking information.

12. Renaming a Remote

You can rename a remote using:

git remote rename origin upstream

The remote previously named origin will now be named upstream.

13. Removing a Remote

To remove a remote connection:

git remote remove origin

This removes the remote configuration from the local repository. It does not delete the remote repository itself.

14. Git Remote Add Example

git init

git remote add origin https://github.com/user/project.git

git remote -v

This initializes a repository, adds a remote named origin, and then displays the configured remote URLs.

15. Pushing to a Remote

The git push command sends local commits to a remote repository.

git push origin main

Here:

  • origin is the remote name.
  • main is the branch being pushed.

16. Setting an Upstream Branch

You can connect a local branch with a remote branch using:

git push -u origin main

The -u option sets the upstream tracking relationship. Future pushes can often be performed with:

git push

17. Fetching from a Remote

The git fetch command downloads information from a remote repository without directly merging those changes into your current branch.

git fetch origin

18. Pulling from a Remote

The git pull command retrieves changes from a remote and integrates them into the current branch according to the configured pull behavior.

git pull origin main

19. Remote Branches

Remote-tracking branches represent branches that Git knows about from a remote repository.

origin/main
origin/develop

These names help you understand the state of branches on the remote.

20. Listing Remote Branches

To list remote-tracking branches:

git branch -r

Example:

origin/main
origin/develop
origin/feature-login

21. Listing All Branches

Use the following command to display local and remote-tracking branches:

git branch -a

Example:

* main
  feature-login
  remotes/origin/main
  remotes/origin/develop

22. What is Upstream?

An upstream branch is a remote branch associated with a local branch.

main → origin/main

When tracking is configured, Git knows which remote branch should be used for operations such as ordinary git pull and git push.

23. Multiple Remotes

A local repository can have multiple remotes.

origin
upstream
company

For example, a developer might use origin for their fork and upstream for the main project repository.

24. Changing a Remote URL

You can change the URL of an existing remote with:

git remote set-url origin https://github.com/user/new-project.git

After changing the URL, verify it with:

git remote -v

25. Remote Configuration

Remote information is stored in the local repository's Git configuration.

You can inspect remote configuration using:

git config --get-regexp "^remote\."

This can show configured remote names and URLs.

26. Common Git Remote Mistakes

  • Adding the wrong remote URL.
  • Using the wrong remote name.
  • Forgetting to add a remote before pushing.
  • Pushing to the wrong remote.
  • Confusing local branches with remote-tracking branches.
  • Forgetting to fetch remote changes.
  • Removing a remote when only its URL needed to be changed.

27. Useful Git Remote Commands

git remote
git remote -v
git remote add origin URL
git remote show origin
git remote rename origin upstream
git remote remove origin
git remote set-url origin URL
git fetch origin
git push origin main
git pull origin main

28. Practical Remote Workflow

git init

git remote add origin https://github.com/user/project.git

git remote -v

git add .
git commit -m "Initial project"

git push -u origin main

This is a common workflow for connecting a local repository to a remote repository and pushing the first branch.

29. Checking a Remote Before Pushing

git remote -v
git branch
git status

Checking the remote URL, current branch, and working tree before pushing can help prevent accidental operations on the wrong repository or branch.

30. Summary of Git Remote

Git remotes connect a local repository with another repository, commonly one hosted on a Git hosting service.

git remote add origin URL

After configuring a remote, you can use commands such as git fetch, git pull, and git push to exchange changes with the remote repository.

git remote -v

Use git remote -v regularly to verify the configured remote URLs.

📌 Key Points

  • A Git remote is a reference to another Git repository.
  • origin is the common default remote name.
  • git remote lists configured remote names.
  • git remote -v displays remote URLs.
  • git remote add adds a remote.
  • git remote show displays detailed remote information.
  • git remote rename changes a remote's name.
  • git remote remove removes a remote configuration.
  • git remote set-url changes a remote URL.
  • git fetch downloads information from a remote.
  • git pull retrieves and integrates remote changes.
  • git push sends local commits to a remote.
  • A repository can have multiple remotes.

🧠 Quick Quiz

Question: Which command adds a remote named origin to a Git repository?