Sub topic 2
What a remote is and why origin is just a name
This chapter assumes you have Git installed, you can make commits with git add and git commit, and you are comfortable in a terminal. You do not need a GitHub account yet.
What a remote is
A remote is a named bookmark in your local repository that points to another copy of the repository, usually on a server. Git stores two things for each remote: a name and a URL. Commands such as git push, git fetch, and git pull use the name so you do not have to type the URL each time.
A remote is not a live connection. Git contacts the other copy only when you run a command that needs it. Between those commands, your repository works entirely offline.
Why origin is just a name
origin is the name Git gives to the remote that git clone creates. It is a convention, not a keyword. Git treats origin exactly like any other remote name, so you can pick github, backup, or upstream instead.
You can check this without GitHub. The commands below create a bare repository (a repository with no working files, which is what servers hold) and register it under a name that is not origin.
git init --bare /tmp/demo-server.git
git init demo-work
cd demo-work
git commit --allow-empty -m "First commit"
git remote add backup /tmp/demo-server.git
git remote -v
git push backup HEADgit remote -v lists each remote's fetch and push URL. The push works the same as it would with origin. The only difference is the word you type.
git config --get remote.backup.urlThis reads the saved URL back from the repository's configuration. Git keeps remotes in .git/config, in a section named after the remote:
[remote "backup"]
url = /tmp/demo-server.git
fetch = +refs/heads/*:refs/remotes/backup/*The fetch line is a refspec. It tells Git to copy each branch from the remote's refs/heads/ into your local refs/remotes/backup/. That is why the names of remote-tracking branches begin with the remote name, such as backup/main or origin/main.
Remote-tracking branches
A remote-tracking branch such as origin/main is your local record of where main was on the remote the last time you contacted it. You cannot commit to it directly. Git moves it when you fetch, pull, or push.
Common mistakes
- Treating
originas special. Git does not require it, but most tutorials, tools, and teammates assume it, so keep it for your main remote. - Expecting
origin/mainto update by itself. It changes only when you run a command that talks to the remote. - Confusing the remote with the repository. The remote is the name and URL stored on your machine. The repository is the data at that URL.
Exercise
A teammate runs git remote add github [email protected]:team/app.git. What command pushes their local main branch to that repository, and why does it not use the word origin?
Show answer
git push github main. Remote names are arbitrary labels chosen when the remote is added. Here the name is github, so every command that refers to this remote must use that name.