DEV Community

Cover image for Intro to Git & GitHub: add files into your repository
Md Mahfuz Hossain
Md Mahfuz Hossain

Posted on

Intro to Git & GitHub: add files into your repository

Hi everyone, I'm Mahfuz Hossain, a student of Information and Communication Engineering from Bangladesh.

Git is a popular distributed version control system
that provides a robust framework for managing and organizing code repositories, while GitHub, a web-based hosting service, offers a platform for hosting Git repositories. It's a popular choice among developers for code hosting and collaboration.

In this article, I'm going to show you step by step how to create, add and push files to your project in GitHub.

Prerequisites

To initialize the repo and push it to GitHub, you’ll need:

  1. A free GitHub Account
  2. git installed on your local machine

Step 1 — Create a new GitHub Repo

Sign in to your GitHub account and create a new repository.Give your project name into the repository name.

adding new repository

Step 2 - Initialize the Git Repo

Open terminal, go to the folder you would like to add.Make sure you are in the root directory of the project you want to push to GitHub and run:

Note: If you already have an initialized Git repository, you can skip this command.

 git init
Enter fullscreen mode Exit fullscreen mode

This will create a hidden .git directory in your project folder

Step 3 - Add the files to the Git index for commit

git add <filename>
Enter fullscreen mode Exit fullscreen mode

replace filename with your file
The git add command will tell git which files to include in a commit. A commit is a basically check-point that captures the changes you have made to the files in your repository.

Instead of this, you can also run git add . if you want to add all the files at once.

Step 4 - Commit Added Files

git commit -m 'added myproject'
Enter fullscreen mode Exit fullscreen mode

This git commit command creates a commit, and the -m flag allows you to provide a commit message that describes the purpose or context of the changes made in the commit.

Step 5 -Add a new remote origin

git remote add origin git@github.com:username/myProject.git
Enter fullscreen mode Exit fullscreen mode

This command establishes a connection between your local Git repository(your PC) and a remote repository(the server) hosted on GitHub.

Step 6 - Push to GitHub

git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • It sends the local commits(from your pc) and changes to the remote repository(github server).
  • -u sets the upstream branch, linking the local "main" branch to the remote "main" branch. This allows you to simply use git push in the future without specifying the branch and remote.

All together

git init
git add <filename>
git commit -m 'added myproject'
git remote add origin git@github.com:username/myProject.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Congratulations! Now you are all set to track your file, code changes remotely in GitHub!
You can take additional help from the official Git resources.

That's all from me. Good luck with your Git journey!

Top comments (0)