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.
origin. It is normally used to refer to the remote
repository from which a project was cloned or to which the project was
connected.
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
Remote repositories are useful for sharing and synchronizing project files.
Git itself manages repositories locally. GitHub can host a remote repository that your local Git repository connects to.
Local Git Repository
↕
Remote Repository
↕
GitHub
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
Use the following command to see configured remote names:
git remote
Example output:
origin
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)
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.
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.
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
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)
Use:
git remote show origin
This displays detailed information about the
origin remote, including remote branches and tracking
information.
You can rename a remote using:
git remote rename origin upstream
The remote previously named origin will now be named
upstream.
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.
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.
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.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
The git fetch command downloads information from a remote
repository without directly merging those changes into your current
branch.
git fetch origin
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
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.
To list remote-tracking branches:
git branch -r
Example:
origin/main
origin/develop
origin/feature-login
Use the following command to display local and remote-tracking branches:
git branch -a
Example:
* main
feature-login
remotes/origin/main
remotes/origin/develop
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.
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.
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
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.
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
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.
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.
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.
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.Question: Which command adds a remote named origin to a Git repository?