How to Push Source Code on GitHub

GitHub has become an essential platform for developers to collaborate and share their code with the world. Whether you’re working on personal projects or contributing to open-source repositories, understanding how to push source code on GitHub is a fundamental skill for any developer. In this blog, we’ll walk you through the step-by-step process of pushing your source code to a GitHub repository, making it accessible to others and safeguarding it for future development.

Prerequisites:

Before we begin, make sure you have the following prerequisites in place:

  1. Git installed: Make sure you have Git installed on your computer. If not, you can download and install it from the official Git website (https://git-scm.com/).
  2. A GitHub account: You’ll need a GitHub account to create repositories and push code to it. If you don’t have one, sign up for free at https://github.com/join.

Step 1: Create a New Repository on GitHub

Once you have a GitHub account and Git installed, log in to your GitHub account and click the “+” button at the top-right corner of the page. Select “New Repository” from the dropdown menu. Provide a name for your repository, a brief description (optional), and set the repository visibility (public or private).

Step 2: Initialize a Local Git Repository

Now, it’s time to initialize a local Git repository on your computer. Open your terminal or command prompt, navigate to the root directory of your project, and use the following command:

git init

This command sets up a new Git repository for your project.

Step 3: Create a .gitignore File

Before adding any files, it’s essential to create a .gitignore file in the root of your project. This file tells Git which files and directories to exclude from version control. Common files to exclude are:

# Ignore IDE and editor files
.vscode/
.idea/

# Ignore build artifacts
build/
dist/

# Ignore sensitive information
config.ini
secrets.txt

You can customize the .gitignore file based on your project’s needs. For example, if you’re working with a specific programming language or framework, you can find predefined .gitignore templates at github.com/github/gitignore.

Some example of gitinore file for Django, Flask and Spring Boot.

Step 4: Add and Commit your Files

Next, add the files you want to include in your GitHub repository. Use the following command to stage all files for the initial commit:

git add .

If you want to stage specific files, you can use:

git add file1 file2 ...

Once the files are staged, it’s time to commit them with a meaningful commit message:

git commit -m "Initial commit"

Step 5: Connect Local Repository to GitHub

To push your local repository to GitHub, you need to establish a connection between the two. Go back to your GitHub repository’s page and locate the repository URL. It should look something like this:

https://github.com/your-username/your-repository.git

In your terminal or command prompt, add the remote URL with the following command:

git remote add origin <repository_url>

Replace <repository_url> with the URL you copied from your GitHub repository.

Step 6: Push Your Code to GitHub

Finally, it’s time to push your local code to GitHub. Use the following command:

git push -u origin master

This command pushes your code to the “master” branch on the remote GitHub repository. If you’re using a different branch, replace “master” with the name of your branch.

Step 7: Verify the Upload

After successfully executing the push command, visit your GitHub repository page. You should now see all your code files there, and any files specified in the .gitignore should be excluded. Congratulations! Your source code is now on GitHub, and you’ve ensured that sensitive or unnecessary files are not tracked by Git.

Conclusion

In this blog, we walked through the step-by-step process of pushing your source code to GitHub. By following these instructions, you’ve made your code publicly available and have a version-controlled repository to manage your project efficiently. Remember to push your changes regularly to keep your GitHub repository up to date. You can also use this method to push code using Visual Studio VS Code or IntelliJ Terminal.

Blogs You Might Like to Read!