elias soltanie
elias soltanie

Reputation: 89

Git branch and remote in my terminal is different from what I see in github

I am learning git and it is getting confusing for me. I am confused about branches, remote repository and directories. Someone else cloned a code on my local machine. When I do git branch I see these three branches:

ChrisMerge
* EliasWork
develop

and when I do git remote I get these

chris
origin
prime

I thought maybe they are directories but I cannot find any directory named chris, develop. So where are the codes stored? Because the codes in each of them are different.

On my git account on github.com, I thought maybe I can see one of these names there. But under branches there are other names like, devel, ver, ... I need someone to make this clear for me once and for all.

Upvotes: 0

Views: 424

Answers (2)

elias soltanie
elias soltanie

Reputation: 89

Because I did not know even very basic things I could not ask the right questions. I tried to read and read and to understand git a little bit. I want to share the basic things that I did not know and hopefully it helps people who are not majored in computer science like me to understand git. Note that I am not trying to be accurate here, I just want you to get started like I did. You can see this awesome tutorial for more information.

  1. This is commit objects that has all the files
  2. The difference between commit objects are usually modified or added files. So they can have different files
  3. Branches are commit objects that we name them. Their purpose is to add some features or fix some bugs. In my case, ChrisMerge,EliasWork and develop.
  4. Otherwise, every commit object has a long name called SHA1. For example, my EliasWork branch has this SHA1: 007415674d8e44908ce2786e164e9e8a751c3984 and a98fd670b9b5c39c8b049671a349f9dfd7b362e3 is a commit object that has no particular branch or head name but the other commit object that I mentioned has a name, EliasWork.
  5. practically head means the general branch name, and HEAD is the branch that is active.
  6. by active branch, I mean when you look at that directory you see the files related to that branch. if you change your active branch you see different files.
  7. So it is like you have some branches that act like different directories. but you can only see and modify the files for the active branch. EliasWork is active branch here and I see those files related to this branch.
  8. Chris, origin and prime are remote repositories. They have their own branches and commits. here origin is the one that I see on my github account. I have cloned this remote repository on my local machine and I have all its commit objects but I only have the remote active branch, i.e. develop, and not its other branches. And then I created another branch called EliasWork so I can modify the codes and later on push it to main repository

Upvotes: 0

torek
torek

Reputation: 489748

A Git remote is just a short name under which Git stores some data. It's not a directory. Running git remote lists all the remote names that Git knows about, which is basically everything that git config --list will list that starts with remote.: you will see:

remote.chris.url = <some url>
remote.origin.url = <another url>
remote.prime.url = <yet another url>

and probably additional lines such as:

remote.chris.fetch = +refs/heads/*:refs/remotes/chris/*

and so on. It's these lines' existence that creates the so-called remote. The fetch lines in particular tell git fetch what your Git should copy from some other Git, and the url lines tell your Git how to contact each of the other Gits.


A branch name, by contrast, is a name that your Git uses to store one specific commit hash ID. This name is up to you—it's not controlled by any other Git, it's controlled by you when you ask your Git to create branch names. In general, if your remote named origin has a branch named master, your Git will remember that name as origin/master and you might want your branch name master to remember origin/master which is your Git's way of remembering the master on the Git that your Git calls up via remote.origin.url.

Git calls these names—origin/master and so on—remote-tracking names or remote-tracking branch names. They are not actually branch names, and they are different from (but obviously related to) the remotes. They're used for your Git to remember ("track") the branches on the remote, which is why they are called remote-tracking names.


Normally, none of this is particularly confusing, because most users in their Git repositories have only one remote, named origin, so their master will go along with origin/master. But in your case, you have three remotes. The Git at the URL named via prime probably also has a master, which your Git will remember via prime/master. So now you have both origin/master and prime/master. If you want to work with these, the usual recommendation would be to use your branch named master to work with origin/master, and to use your branch named master to work with prime/master. But this is doomed to failure—so you'll need to use some other local branch name(s), and that's where things get confusing.

Fortunately, as we just saw, your own branch names don't have to match some remote's name. You can have a branch like EliasWork that is, or is not, connected to any of your remote-tracking names. The limitation is that each branch name can only have one of these set. Git calls this the upstream of the branch.


Extra-confusingly, when we set a remote-tracking name as the upstream of a branch, Git says that this branch is now "tracking" that remote-tracking name. So now we have:

  • A branch name, which is a local name we make up for ourselves ...
  • that is tracking (as a stand-alone word) an upstream made out of ...
  • a remote-tracking name or remote-tracking branch-name, which, as a complete phrase, is really a local name ...
  • that remembers a branch name as seen on a remote ...
  • where a remote is a short name for a URL for some other Git repository (whew!).

Note that this keeps re-using various words, and having them mean different things! As terminology goes, it leaves a lot to be desired.

In any case, none of these are directories that you will see in your work-tree, which is where you do your work. (Git will sometimes create some directories within .git using these names, but the .git directory, which stores the repository itself, is not technically included in your work-tree.)

Upvotes: 2

Related Questions