How to create a new repository on the GitHub

Create a new repo page on the GitHub

Create a new GitHub repository by pressing the “New” button at the top right corner inside the “Repositories” tab.

You can also jump to the new repository page by clicking the plus sign at the top right corner of the top page.

Enter the repository name

After checking either public or private, it is recommended not to check the options: “Add a README file”, “Add .gitignore” and “Choose a license” at the beginning. If you check them, you have to carefully pull the project from the GitHub before pushing your local project to the GitHub. Finally, press “Create repository” button.

In the next page, copy your repository’s HTTPS address.

Local git settings

The initial setup in the GitHub page is done. Go back to your local project and “cd” into the root directory of your project by the terminal. The first thing you have to do is to create a .gitignore file in the directory. In the .gitignore file, you specify the files you do not want to be tracked by git and thus not to be uploaded to GtiHub. The following is an example of .gitignore. We specify three files here. One is, another is the python cache folder and the final one is “*.db” or “asterisk .db” files, which means any .db files.

The next step is to initialize the git repository in this local directory by the following code.

git init

If you press “git status” here, you can check the files which can be seen from the git. The files you add to the .gitignore file are not in the list.

The local project has been linked to the git repository (not GitHub yet). Add all files in the project to the git by:

git add .

If you want to add a file to the repository, you can put the name of the file after the git add <filaname>. For example, .gitignore file itself is sometimes not included in the repository. You can check the state of the directory by:

git status

After add the files to the repository, you have to commit or take the snapshot of the changes by:

git commit -m “first commit”

“-m” is for a message. You have to write some commit message between the semicolons. The message should by specific so that you can see what changes had been made when you look back the git history.

Pushing your local project to the GitHub

After committing the changes to the local git repository, you can add the remote GitHub repository by the following code. Replace the address following the “https:” with your own repository’s GitHub address.

git remote add origin[Your id]/[Your repository].git

After the remote origin being set, you can push your local repository files to the remote GitHub repository by:

git push

If this is the first time to push, you may mot have any upstream branch. You can set the current branch as upstream by:

git push --set-upstream origin main

If you are not logged in the GitHub through your browser, you might be asked to log in through the terminal. After pushing successfully, if you go back to your GitHub page, you can see all files are up to the new repository.

Creating a branch

If you “git status” in the root directory of your project, you will get the “On branch master” unless you have created a new branch and you are on in. “On branch master” means that you are on the branch named “master”. Because the master branch has to be kept clean and deploy-ready, working on the master branch is not always best choice in case for example you want to try some new feature. You can create a new branch by typing:

# Create a new branch named newbranchname:
git branch newbranchname

# The branch can have a tree structure, for example:
git branch feature/newbranchname

You can switch to the new branch from the master branch by:

git checkout newbranchname

After you have made changes to the git branch, you can push this new branch in the local repository to the remote GitHub repository by:

git push --set-upstream origin feature/item-categories

After making changes to the new branch, and once you are confident that everything new works fine, you can bring in all the new changes you have made in the the new branch to the master branch. This is called “merge”. To merge, you first have to move back to the master branch by:

git checkout master

Then merge the new branch to the master branch by:

git merge newbranchname

and push the local branch master to the remote branch master by:

git push

Once you merged the new branch to the master branch, you do not need the new branch. You can delete the branch by:

git branch -d newbranchname

How to clone an existing project

Another way to create a new git repository in your GitHub is to clone an existing one. If you are not the owner or a contributor of the project I recommend that you fork it before clone it. Please read the next section, “Forking a project before cloning it” first.

To clone a repository, go to the repository you want to clone, and click the “Code” button. After you click on the “Code” button, copy the HTTPS link appeared in a new box.

Then create a new project folder in your local computer, open a terminal in the project and git clone the project by the code below. The https: address is the one you copied in the git repository.

git clone[The ID]/[The project name].git

Once you have downloaded the project, cd into it.

cd nextjs-styled

The git repository used in this example is using npm, but If the cloned project is using yarn and you want to use npm, just delete the yarn.lock file.

rm -rf yarn.lock

Next, you need to install all the dependencies used in this project by npm update. This command is named “update” but it also installs the necessary dependencies if it does not exist. -D is for the development dependencies.

npm update -D

If the message appeared in the terminal is “found 0 vulnerabilities”, everything is fine.

If any vulnerabilities are found, you can fix them by just typing “npm audit fix”. Npm is a package manager and knows how to fix this problem.

npm audit fix

Similarly, if any errors are found in the package.json file, run “npm install” to fix it.

npm install

Finally, you can check vulnerability issue by the following code.

npm audit

Now the project is cloned to your local environment. After doing your own work on the project, the next thing you might want to do is to either git push the project to the original repository or git push the project to another remote GitHub project.

If you are the owner or a contributor of the project, you may use the same remote origin to keep your remote GitHub project updated. You can do it by simply adding, committing and pushing.

git add .
git commit -m"updated"
git push

If you are not the owner or a contributor, you might want to push the project to your own remote GitHub project. However, you cannot push this project to your own GitHub project because the remote origin of this project is linked to the original repository you pulled. You can check the current remote origin by the following command:

git config --get remote.origin.url

Before pushing the project to your you have to remove the origin by:

git remote remove origin

Now that you remove the origin, you can add your new original origin by:

git remote add origin[Your id]/[Your repository].git

Forking a project before cloning it

You can see the fork button in the top right corner of every GitHub project; click it and a copy of the project will be added to your GitHub profile under the same name as the original project. After forking the project, simply clone it in the way above and you can commit and push to it. You do not have to remove the origin before adding.

Thank you very much for reading my article. I’ll see you in the next article.