Home > front end >  How is it possible to commit before creating a branch?
How is it possible to commit before creating a branch?

Time:01-26

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.

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • Related