DEV Community

Hrithik Bansal
Hrithik Bansal

Posted on

How to push MERN/MEAN projects to GitHub?

Image description

To push a new project to GitHub repository, follow these steps:

You need to understand that you're not supposed to commit all files to GitHub. So, you need to ignore unnecessary files. For example, in React projects, you can totally ignore "node_modules" folder which contains all the files you installed as dependencies because it is super heavy and unnecessary. The reason is that all the dependencies are specified in "package.json" file which means that when you or someone else download that project in future, you can reinstall all the dependencies locally on your machine without polluting GitHub using npm install command. To ignore unnecessary files and folders, create a file named ".gitignore" in the root of your project. In the context of React application, you can include "node_modules" inside that file. Of course, there is more to this (such as hiding API keys and sensitive information), but for starters ignoring "node_modules" is pretty sufficient.

1: Perform a git init command in the root folder of the existing project.

2: Use git status command to displays the state of the working directory and the staging area.

3: Create a .gitignore file touch .gitignore that specifies intentionally untracked files like node_modules that Git should ignore.

Image description

4: Add your files to staging area (a queue where your files "chill" before you decide to actually push it to GitHub).
git add .

5: Now, commit the files to your local repository. Write a meaningful message in quotation marks. Then, put the terminal aside (don't close it) and open your browser.
git commit -m "This is my initial commit."

6: To push a project to GitHub, you must first create a GitHub repository. To do this, simply click the green “Create repository” button in GitHub’s online console and provide a repository name.

For this example, we will name the repository DEMO.

Image description

7: To allow your existing project to synchronize with GitHub, issue a git remote add command to configure a reference from you local Git installation to the repository on GitHub.

Image description

8: Remember the terminal that you left aside? Paste the first line from GitHub under "…or push an existing repository from the command line".

git remote add origin https://github.com/yourgithubusername/REPOSITORYNAME.git
Enter fullscreen mode Exit fullscreen mode

9: git branch -M main

10: Push the first commit to GitHub
git push -u origin main

Verify the GitHub push

To verify that the existing project was pushed to GitHub successfully, log into the GitHub website and browse the repository. All of the files from your existing project should be visible on GitHub’s.

And that’s how easy it is to push an existing project to a GitHub repository.

Top comments (0)