fatal: Not a valid object name: 'master'

Git

Git Problem Overview


I have a private server running git 1.7 When I

git init 

a folder it doesn't create a master branch. Cause when i do:

git branch 

it doesn't list anything. When I do:

git --bare init

it creates the files. When I type

git branch master 

it says:

fatal: Not a valid object name: 'master'.

Git Solutions


Solution 1 - Git

> When I git init a folder it doesn't create a master branch

This is true, and expected behaviour. Git will not create a master branch until you commit something.

>When I do git --bare init it creates the files.

A non-bare git init will also create the same files, in a hidden .git directory in the root of your project.

> When I type git branch master it says "fatal: Not a valid object name: 'master'"

That is again correct behaviour. Until you commit, there is no master branch.

You haven't asked a question, but I'll answer the question I assumed you mean to ask. Add one or more files to your directory, and git add them to prepare a commit. Then git commit to create your initial commit and master branch.

Solution 2 - Git

Git creates a master branch once you've done your first commit. There's nothing to have a branch for if there's no code in the repository.

Solution 3 - Git

First off, when you create a "bare repository", you're not going to be doing any work with it (it doesn't contain a working copy, so the git branch command is not useful).

Now, the reason you wouldn't have a master branch even after doing a git init is that there are no commits: when you create your first commit, you will then have a master branch.

Solution 4 - Git

You need to commit at least one time on master before creating a new branch.

Solution 5 - Git

copying Superfly Jon's comment into an answer:

To create a new branch without committing on master, you can use:

git checkout -b <branchname>

Solution 6 - Git

Not a traditional answer but I have a solution for my case too. For this exact same issue, I noticed this issue occured when I created another repository in an already existing repository.

So if you cloned a repo and then git init again, it'll cause this issue. Then you have to delete the whole folder and clone it again. Thanks.

Solution 7 - Git

please try to create any file in master branch and commit.Then create other branch . It should work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRoy van ZantenView Question on Stackoverflow
Solution 1 - Gituser229044View Answer on Stackoverflow
Solution 2 - Gituser489998View Answer on Stackoverflow
Solution 3 - GitBorealidView Answer on Stackoverflow
Solution 4 - GitFundhorView Answer on Stackoverflow
Solution 5 - GitkruboView Answer on Stackoverflow
Solution 6 - GitAlam JimView Answer on Stackoverflow
Solution 7 - GitNabendu DuttaView Answer on Stackoverflow