3

I didn't used Git for a few months. I just started up a new project and one thing bothers me: everytime I create a new branch, the already existing one disappears as if it was replaced. I mean, when i do "git branch", i have nothing! Obviously, my "git checkout " doesn't work. I don't even have my branch anymore. Only the current one.

I don't have this problem with already working projects.

Why is that?

user2183282
  • 133
  • 2
  • 7
  • 1
    Sounds to me like your git repository is somehow corrupt, can you check the contents of the `.git/refs/heads` folder? – Lasse V. Karlsen Oct 17 '18 at 19:47
  • Make sure you spell the branch names *exactly* as they should be, that is, don't do `git checkout Master` with an uppercase M. git on WIndows might let it slip through, but it won't be completely correct and there can be all sorts of problems since git is case sensitive. – Lasse V. Karlsen Oct 17 '18 at 19:53

1 Answers1

3

This is normal: you have just created a new project:

$ mkdir newproj
$ cd newproj
$ git init
Initialized empty Git repository in .../newproj

At this point, no branches exist. Nonetheless, as git status will tell you, you are on the master branch:

$ git branch
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
$ 

You can switch to another branch, using git checkout -b:

$ git checkout -b xyz
Switched to a new branch 'xyz'

and now you still have no branches, but now you are on branch xyz, as git status will tell you.

This odd state is normal, because there are no commits. When there are no commits, there can be no branches, because a branch name simply contains the hash ID of some existing commit. Without commits, no branch name can exist.

When you create a new commit, Git does the following steps (the first several are not in any particular order; only the last few have an actual order):

  1. Gather your name and email address, to use in the new commit.
  2. Get (your computer's idea of) the current time, to attach to your name and email address.
  3. Gather a log message.
  4. Freeze the contents of the index (the index being where you build new commits: git add copies files from the work-tree into the index).
  5. Get the hash ID of the current commit, if there is a current commit. Get the hash ID of any commits being merged as well, if that's going on.
  6. Use all of this information to create a new commit. Creating the commit assigns it its hash ID, which is a cryptographic checksum of all of these contents (your name and email address with the time stamp, your log message, the hash ID of the frozen tree, and so on).
  7. Write the hash ID obtained by writing the new commit. This new commit's hash ID goes into the current branch name. If the branch name did not exist before this point, well, now it exists.

Hence, it's not until you make at least one commit that you can have any branch names. But you can still choose which branch name is your current branch, even if that branch can't exist: it's the creation of the first commit that will create the branch name.

(Once you do have at least one commit, you can attach as many names as you like to the one commit. If you have more than one commit, you can attach as many names as you like to every commit, though most commits don't need any names attached to them. Git finds them by working backwards from a later commit. Only the last commit—the tip of a branch—ever needs a name, so that Git can find it without first finding a later commit.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Alriiiight! I guess i always commited something after a "git init" in the past, before creating a new branch. Thank you! – user2183282 Oct 17 '18 at 22:05