Can't create new branch on VS Code

Hey everybody!
This a bit embarassing but I’m once again stuck in the Git version control in VS Code. Following the video instructions, when I try to create a new branch, I get this message:

I’ve read in other forum post that the problem might be because I have no commits. How do I create a commit here? I tried clicking on terminal, type git init and then git commit -m “create newBranch from codecademy lesson” and I got a message saying:
nothing to commit (create/copy files and use “git add” to track)
I then created a new pasta (mkdir newdirectory) tried the commit again and got the same message.

I’ve reinstalled VS Code, switched folders but I always get this. I’m a bit stuck here, any ideas about what I might be doing wrong?

Thank you

Are you following some kind of guidance, it seems like some steps are missing or you’ve misunderstood them. As the warning you saw states, you’d first want to to stage some new files to be tracked and git add is the typical way to get this done. So create a folder, make a new file (even some basic text or what have you), git init to set the directory as a repo, git add the new file then commit.

bash example-

git init new_repo
cd new_repo
echo "text" > file.txt
git add file.txt
git commit -m "example"
git switch -c newbranch

As you say the problem is that there are no commits at present so there isn’t really a HEAD commit for that branch. Staged some changes and make a commit and things will likely behave better.

This all seems like a slightly overcooked set of commands for what you’re doing though, that may be a slightly dated video, git branch newbranchname would create you a new branch or git switch --create branchname if you wanted to create and switch to a new branch. For backwards compatibility checkout can still do almost everything but you may as well use the nice new(er) commands.

1 Like

Thank you so much for your reply. I did misunderstood the steps and still am a bit confused about all of this so please bear with me. So I followed your commands and was able to create a commit and a new branch.
Now, because I’m also trying to follow the steps from the video, I then tried to create a new branch by clicking in “master” at the bottom left and then click “+create a new branch” at the top and I still get that same message

The commit is there so what am I missing here?

I think you’re still on a branch with no commits (not 100% sure how but I don’t know what steps you took and in what order), you could confirm with git log or similar. Double check you’re on your master branch for that repo, try adding and committing a change and then try the command palette options.

1 Like

Got it, tank you so much for the help!

1 Like