Post

Configure global GitHub Main & Create Git Repo

Configuring Global Main Branch locally

  • I faced an issue whenever I initialized Git on my local machine and tried to push the files to a remote repository on GitHub.
  • The issue was that the default branch on GitHub was main, but the default branch on my local machine was master.
  • To fix this issue, I had to set the default branch to main on my local machine. Here is how I did it:
  • In the terminal, run the following command:
1
git config --global init.defaultBranch main

Create a New Git Repository

Creating a new repository nowadays especially in VSCode’s User Interface is very easy. But sometimes, you might want to create a repository from the terminal.

Here is how you can do it:

Step 1: Create a new repository (on GitHub)

  • Go to Github.com and create a new repository
  • Select the owner and give the repository a name
  • select either public or private
  • click on create repository

Step 2: Initialize git locally (on your computer)

  • Open your terminal
  • Navigate to the directory you created for your project
  • Run the following command to initiate a git repository:
1
  git init

.gitignore

  • If you have any folders or files in the directory which you do not wish to add to the repository, create a .gitignore file and add the names of the files or folders to it. The contents of a typical .gitignore file for example with a node_modules folder and a .env file would look like this:
1
2
3
// .gitignore
node_modules
.env
  • Run the following command to add all files to the staging area:
1
git add .
  • Run the following command to commit the files to the repository with a message:
1
 git commit -m "first commit"
  • Run the following command to add the remote repository:
1
2
 git remote add origin <the link to the repository on GitHub>

  • Run the following command to push the files to the remote repository:
1
  git push -u origin main

When you refresh the repository on GitHub, you should see all the files from your local directory.

Next Commits

  • After the initial set up and repo creation, you can make changes to the files and commit them to the repository using the following commands:
1
2
3
  git add .
  git commit -m "commit message"
  git push
This post is licensed under CC BY 4.0 by the author.