How is it possible to commit before creating a branch?

Viewed 85

According to GitHub, to create a repository from cmd, we have to:

echo "# some-textR" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/username/repositoryname.git
git push -u origin main

But, here I can see that a commit is being made before creating a branch.
To check if what I am thinking is not wrong I ran:

git init
git branch

And no branch was displayed



Any suggestion gratefully received. Thanks in advance.

3 Answers

By default if there is no branch created, on the first commit the branch will take a default name usually master or main.

If you are using git 2.28 above, you can alter the default branch by using this command

git config --global init.defaultBranch {branchName}

example:

git config --global init.defaultBranch my-branch

if you want to alter it back, just delete the line with editor or git config --global -e

What is a branch and what is a commit?

A branch is a pointer to one specific commit, while a commit is a snapshot of your repository at a specific point in time.

How is it possible to commit before creating a branch?

A branch needs a commit to exist, but not the other way around.

Since git init creates a repository without any commits, you need to create at least one commit before being able to create a branch.

Both David Yappeter's answer and Juan Cruz Borssotto's answer are good. The one thing I would add is this, building on Juan's point that a branch needs a commit to exist:

  • Git's git init creates a new, totally empty repository (when used the way you used it: sometimes it just "reinitializes" an existing repository, which is kind of a useless mode and I wish it required a flag for this or something).

  • An empty repository has no commits, so it cannot have any branches.

  • And yet, you're still "on" some branch.

This means you can be "on" a branch that does not exist. That's kind of a special case: it's not a normal way to work at all. It's required in this new, totally-empty repository.

When you are in this state, the next commit you make (which is now the first and only commit in this otherwise-empty repository) creates the branch name. The branch name that Git creates at this time is the name of the non-existent branch you're on. That cause the branch to come into existence, simultaneously with creating the commit.

So that first commit both creates the branch, and creates the commit that's the only commit on the branch, even though you're on the branch that didn't exist. It's almost a sort of a big-bang create-the-universe event. It only happens once in each universe, er, repository.1 Once the branch exists, the normal operations—like "rename this branch"—all work.2

Because of this particular bit of weirdness, some web hosting sites will make one initial commit for you in a repository you create with their create a new repository web clicky button. Specifically, GitHub do this by default. That gets you out of this weird mode and means you don't have to learn to deal with the weirdness. Some people really like this (the GitHub folks in particular!).


1Technically, you can re-create this weird mode in a non-empty repository, using git checkout --orphan or git swtich --orphan. That's not something most people normally want to do, ever. This feature was new in Git version 1.7.2; before that, you had to reach deep into Git's guts to do the trick.

2A need to rename the as-yet-uncreated branch was not foreseen, so for a long time there was no official way to do this. Once people started renaming master to main, the need became obvious, and the Git folks released Git version 2.30 to make that easy.

Related