Creating and Pushing to a Fresh GIT Repository

Creating and Pushing to a Fresh GIT Repository

·

2 min read

This is a super short post, in case it helps others.

Assumptions:

  • GIT is installed and you have configured your SSH keys

  • You understand how GIT works at a basic level

Let's say you have a project locally you've been working on that hasn't been added to GIT (including a local repo) and you don't have a remote repo on GitHub yet either. To get the project into GitHub do the following.

Locally

Initialize the local git repo if you haven't already:

git init

# optionally run the following if you are using multiple GIT accounts on the machine
git config user.name "<username>"
git config user.email "<email>"

Then, add a .gitignore file

Update .gitignore to include any files/folders that you don't want to push to GitHub (e.g. IDE folders, logs, env files, build output etc). An example of this is:

.idea
*.iml

.gradle
build
target

OK, now, let's add your project to a local GIT repo:

git add .
git commit -m "<a descriptive message>"

For example:

Remote

Log in to GitHub and create a new repo. I generally elect not to create a new ReadMe as part of this process but instead, create it locally and then push it.

Copy your new repo URL (either HTTPS or SSH will be fine).

Back Locally

Now, it is time to push our local copy to GitHub. We are using the SSH endpoint, adjust the URL for your circumstances.

If you are using a single account (most use cases):

git remote add origin git@github.com:<username>/<repo-name>.git 
git branch -M main 
git push -u origin main

If you are using multiple accounts on your laptop (e.g. work, personal):

git remote add origin git@<username-in-config>:<username>/<repo-name>.git 
git branch -M main 
git push -u origin main

And now you should be done.

Screenshot of the end-to-end process:

If this helps or I've missed something please leave a comment.

Thanks, Michael.