I always struggle when I am ready to push my first commit to a new repository and always end up with errors, so I thought I would document the process here as an aide memoir for me and, hopefully, others will find it useful too.
Prerequisites
These instructions are for when you have already got a folder of code that you want to commit for the first time to git. The first thing to do is to create and setup your .gitignore file. This tells git which files to exclude when uploading. Things to include here are config files with live details, api keys etc. and other systems files such as .DS_Store files on a Mac.
Once you are happy with your .gitignore, you’ve created a README and LICENCE, if required, you can create your repository on your Git of choice. Here’s what it looks like on a couple of providers:
Steps
Once you have created your respository you need to carry out the following steps. These have been written for pushing to GitHub but will be very similar for other providers.
cd <your working folder>
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin [email protected]:<your GitHub username>/<your repository name>.git
git push -u origin main
What’s This Doing?
I have annotated a copy of the commands below so that you can see what each line is doing:
cd <your working folder>
# initialise the current folder and sub folders for pushing to git
git init
# add all changed files and folders to git (excluding those in .gitignore)
git add .
# add a commit comment so others (or you) can see what has changed
git commit -m "Initial commit"
# set the working branch to be main
git branch -M main
# add the url of your git respository to the local git
git remote add origin [email protected]:<your GitHub username>/<your repository name>.git
# push all changes from local to remote from the main branch
git push -u origin main
In theory that should be it all done and your output should look similar to the following:
Potential Errors
A few times when I have followed the instructions given on the GitHub new repository page, I have received the following error message:
git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'github.com:<your GitHub username>/<your repository name>.git'
The error you’re encountering indicates that Git does not recognize the main
branch in your repository, likely because the branch hasn’t been created or checked out locally yet. Here’s how you can resolve it step by step:
1. Check Your Local Branch
Run this command to see your current branch:
git branch
If you’re not on main
, switch to it or create it.
2. Switch or Create the Main Branch
If main
doesn’t exist locally, create it and switch to it:
git checkout -b main
3. Add and Commit Changes
If you haven’t added or committed any changes yet, do so now:
git add .
git commit -m "Initial commit"
4. Set the Remote Origin
Ensure your remote is set correctly:
git remote -v
5. Push the Main Branch
Push the main
branch to GitHub:
git push -u origin main
Additional Notes
- If you’re using
master
instead ofmain
, adjust accordingly by replacingmain
withmaster
in the above steps. - If the remote repository is empty, these steps will create the first branch on the remote.
Now you should be good to go.