When you forget to make your first git commit to remote origin, checkout into a new branch, and then merge into main

Sunday, January 19, 2025 at 11:59 AM | 3 min read

Last modified on Sunday, January 19, 2025 at 2:58 PM

#fullstack development, #macOS, #command line, #git, #local repository, #remote repository, #feature branch, #main branch, #git merge, #series, #terminal

Git logo

Photo by RealToughCandy.com on pexels.com

Table of Contents

Creating a new local Git repository

Recently, I created a new local Git repository called custom-abstract-base-user-model. Then I initialized Git with the following command:

git init

Checking out into new branch called create-config-project-and-users-app

However, instead of immediately making my first commit to a remote repository on GitHub, I checked out into a new branch called create-config-project-and-users-app:

git checkout -b create-config-project-and-users-app

Trying to check out into main branch after committing changes in create-config-project-and-users-app

Then, after adding all the features I wanted to add to that branch and committing my changes, I ran the following command:

git checkout main

Which returned the following in Terminal:

error: pathspec 'main' did not match any file(s) known to git

Creating a new branch called main in create-config-project-and-users-app and checking out into main all at once

I never actually "created" a main branch, because I never committed anything. I simply checked out into a new branch, and any content I had created in "main", was checked into the new branch as well. This was fine! I had to create a main branch that I could check into, merge create-config-project-and-users-app into main, and then push to remote origin on GitHub. To make this happen, I ran the following command:

git checkout -b main create-config-project-and-users-app

What this command means is that I am creating a new branch called main from a branch called create-config-project-and-users-app, and then checking out into the new main branch.

Creating the remote repository on GitHub and add adding remote origin

Then, I had to create my remote repository on GitHub, followed by running the following in Terminal:

git remote add origin git@github.com:interglobalmedia/custom-abstract-base-user-model.git

Merging create-config-project-and-users-app into main branch and pushing changes to remote origin

Then I ran the following command:

git merge create-config-project-and-users-app --no-ff

Which returned:

Already up to date.

This was because when I checked out into the new main branch, the changes I made in create-config-project-and-users-app were "merged" into main. All I had to do was run the following command:

git push -u origin main

Which returned the following in Terminal:

Enumerating objects: 30, done. Counting objects: 100% (30/30), done. Delta compression using up to 10 threads Compressing objects: 100% (28/28), done. Writing objects: 100% (30/30), 6.85 KiB | 3.42 MiB/s, done. Total 30 (delta 4), reused 0 (delta 0), pack-reused 0 (from 0) remote: Resolving deltas: 100% (4/4), done. To github.com:interglobalmedia/custom-abstract-base-user-model.git * [new branch] main -> main branch 'main' set up to track 'origin/main'.

Conclusion

In this post, I discussed creating a new local Git repository, checking out into a new branch called create-config-project-and-users-app, trying to check out into main branch after committing changes in create-config-project-and-users-app, and after not succeeding, creating a new branch called main in create-config-project-and-users-app and checking out into main all at once, creating the remote repository on GitHub, adding remote origin, merging create-config-project-and-users-app into main, and pushing changes to remote origin.

If you also ever forget to make your first commit in your local Git repository and subsequently check out into a new feature branch, follow the steps shown here, and you will have no issues checking out into your main branch and merging your changes made in the feature branch!