trusted

Quick Guide to add your local code to GitHub

The tutorial provides a quick guide to add your local code to GitHub. It offers a step-by-step guide to help beginners add their local code to GitHub using the command line and GitHub Desktop. It also provides a brief overview of the benefits of using GitHub and the importance of version control in software development.

Recent posts

...
Git cheatsheet

The tutorial provides a quick guide to Git commands and their usage. It is designed to help beginner...

26 May 2024

Continue reading
...
Host your websites on Github pages for FREE!!!

The tutorial provides a step-by-step guide to host a websites on GitHub Pages for free. It is design...

30 April 2024

Continue reading

Yasir Mansoori

MERN Developer || Full Stack Developer Enthusiast || Software Developer Enthusiast || CS undergrad at SRM University || Lead @GFG-SRMIST

Last Updated - January 2024

Table of Contents


Creating a git repository onto github.

To put your project up on GitHub, you will need to create a repository for it to live in.

GitHub repositories can house a wide range of projects, including open source projects. You can share code in open source projects to create better, more dependable software. Repositories can be used to collaborate with others and track your progress. .

  1. Login to "Github"
  2. In the upper-right corner of any page, select + then click New repository.
  3. Type a short, memorable name for your repository. For example, "hello-world".
  4. Optionally, add a description of your repository. For example, "My first repository on GitHub."
  5. Choose a repository visibility. For more information, see "About repositories."
  6. Select Initialize this repository with a README.
  7. Leave other fields as it is.
  8. Click Create repository.

Initializing a Git repository

If your locally-hosted code isn't tracked by any VCS, the first step is to initialize a Git repository.

  1. Open Git Bash.
  2. Navigate to the root directory of your project.
  3. Initialize the local directory as a Git repository. By default, the initial branch is called main.
  4. git init
  5. If you're using Git 2.28.0 or a later version, you can set the name of the default branch using -b.
  6. git init -b main
  7. Add the files in your new local repository. This stages them for the first commit.
  8. git add .

    OR:

    git add -A
  9. Commit the files that you've staged in your local repository.
  10. git commit -m "Initial commit"

Here in the above code we gave a shortcut "-m" so that we can write the commit message quickly, if you want to add commit message seperately remove -m and it will open vim editor.

Connect Local Repository with GitHub Remote Repository.

As we all know, the GitHub repository is a cloud-based repository. This means that whatever data is available in the Local Repository can be posted to the GitHub Remote Repository. We previously created a GitHub remote repository called "hello-world", now it's time to send our local data to a GitHub remote site.

  1. Navigate to your repository on github.
  2. Follow this steps after first commit.
  3. Update the default branch name from "master" to "main"
  4. git branch -M main
  5. Copy remote repository URL field from your GitHub repository, in the right sidebar, copy the remote repository URL.
  6. In Terminal, add the URL for the remote repository where your local repostory will be pushed.
  7. git remote add origin <remote repository URL>
  8. Sets the new remote:.
  9. git remote -v
  10. Push the changes in your local repository to GitHub.
  11. git push -u origin main

Here in the above code we gave a shortcut "-u" so that we can track the relationship between local branch and the remote branch. This means that, in the future, when you run git push or git pull without specifying the branch names, Git knows which remote branch to push to or pull from. It sets the default remote branch for your local branch.

Conclusion:

In Conclusion, adding your local repository to GitHub enhances collaboration, provides a secure and accessible backup, facilitates project management, and integrates with a variety of tools to streamline development workflows. Whether you're working on personal projects, collaborating with a small team, or contributing to open source, GitHub serves as a powerful platform for version control and project management.

The only way to achieve the impossible is to believe it is possible.
- Charles Kingsleigh