Push my Nodejs project to Github.

Hi!

GitHub is a web-based interface that uses Git, the open source version control software that lets multiple people make separate changes to web pages at the same time.

Here we will understand how to push our existing Node project to github repository.

First up, go to github.com, and login to your account. If you don't have an account, try creating one, it's simple and easy. Now when you land on your home page on github, create a repository by clicking on the plus (+) icon on the top right corner and select New repository. Give it a name you like and choose whether you want your repo to be private or public and then click on Create repository.

After you are done creating the repository, you'll be landed on the repository main page which looks something like this.

image.png

Now open terminal and move to the main folder where your node project is created and type the command below

git init

Now git will be initialised in your project. To confirm the same, type the command below to check if everything is working fine

git status

Now it's time to link github repo created to the project, so type the command below in the format as git remote add origin https://{github_username}@github.com/{github_username}/{repo_name}.git In my case, github_username is nkgoudar and repo_name is node-mongo so the command will be image.png

To confirm you've linked it correctly type the command below

git remote -v

and you should see results like below in your terminal (Except username and repo name)

image.png

Now that you've linked your project to github repository, you're now ready to push your code to the repository. Now type git status command line and see the file and folders which are ready to be committed to the repo.

git status

But before that few points to consider

  • There are certain files like env or config files which shouldn't be made public and hence shouldn't be pushed to github repo.

  • You should not push node_modules to the repository as the folder will be huge and you can download all the modules and create the folder with the help of package.json file.

So in order to not push those files and folders to repo, you don't have to neglect them everytime you want to push some code instead you can create gitgnore file and add all the files and folders that you want to ignore while pushing the code. For that create .gitgnore file at the root of your project

touch .gitignore

Once the file is created you can simply add the paths to file/folder you wanna ignore. For now let's add node_modules in that file and see what happens. Save the file and type git status in the terminal and see that node_modules which you saw earlier is not there and that won't be committed to repo. Now it's time to push your code to the repository. Few steps to push the code

  1. Type git status and confirm the changed files.
  2. Confirm the branch you want to push your current code to.
  3. Now add the files you want to push the code from. For that use command git add file_name.extension You will get the path to file when you do git status.
  4. If you want to push all the changes then type git add . This will add all the changed files.
  5. Once the files are added, it's time to commit the changes using the command git commit -m "commit_message" commit_message will be seen by the people who visit your repository and it should have information about the changes made in the code.
  6. After committing the changes, push your changes to repo using the command git push remote_name branch_name Here remote_name is the one you added earlier i.e. origin and branch_name will be master by default. You can always change your branch.
git status
git add .
git commit -m "Initial commit"
git push origin master

And now you can see all the code in your github repo, just refresh your github repo page if you haven't close it yet or visit the page again and you'll see the files and folder that you had in your machine.

image.png

And you can see the commit message that you gave at the top. It will be the latest commit message.

Thanks for reading. Hope it helps. Any suggestions/corrections/queries ? Please let me know in the comment box.

For more important git command lines check the doc I prepared, which will help you or your friends in some way. docs.google.com/document/d/1eeC1fmBxRDNFZkU..

Request for the access, so I'll know you've read till the end😉.