Add Code on GitHub Repository
Last Updated :
11 Nov, 2025
GitHub is a powerful platform for hosting and sharing code. Whether you’re working on a solo project or collaborating with others, adding code to a GitHub repository is essential. Here’s a step-by-step guide on how to add your code to a GitHub repository.
Steps to Add Code on GitHub Repository
Step 1: Create a New Repository on GitHub
1. Log in to GitHub: Open your web browser and log in to your GitHub account.
2. Create a new repository:
- Click on the "+" icon in the top-right corner and select "New repository."
- Enter a repository name.
- Add a description (optional).
- Choose the repository type (public or private).
- Click "Create repository."
Step 2: Initialize Git in Your Project Directory
1. Open your terminal: Open your command line interface (CLI) or terminal on your computer.
2. Navigate to your project directory: Use the cd command to navigate to the directory containing your project. For example:
cd path/to/your/project
3. Initialize a Git repository:
- Run the following command to initialize a new Git repository in your project directory:
git init
Step 3: Add Your Code to the Repository
1. Add files to the staging area: Use the git add command to add your files to the staging area. To add all files, run:
git add .
2. Commit your changes: Use the git commit command to commit your changes. Add a meaningful commit message:
git commit -m "Initial commit"
Step 4: Connect Your Local Repository to GitHub
1. Add the remote repository:
2. Verify the remote repository:
- Ensure the remote repository is added correctly by running:
git remote -v
Step 5: Push Your Code to GitHub
1. Push your changes:
- Use the
git push command to push your changes to the GitHub repository:git push -u origin main
- If your default branch is
master, use master instead of main.
Step 6: Verify Your Code on GitHub
1. Check your repository:
- Go to your GitHub repository in your web browser.
- Refresh the page to see your files and commit history.
Tips for Effective Use
- Frequent Commits: Make small, frequent commits to track changes more effectively and keep your commit history clean.
- Branching: Use branches to work on new features or bug fixes without affecting the main codebase.
- Pull Requests: When your feature or fix is ready, create a pull request to merge your changes into the main branch.
Which command is used to initialize a local Git repository?
Explanation:
git init creates a new Git repository inside a project folder.
To add all project files to staging area, we use:
Explanation:
. stages every file in current directory.
Which command creates the first commit?
Explanation:
The first commit in a Git repository is made using the git commit -m "message" command. After staging files using git add, you can't store changes in the repository until you commit them. The commit records a snapshot of your project at that moment, along with a message describing what changes were made. Commands like git push only upload commits, git clone copies a repository, and git status just shows the current state. So the correct command to create the first commit is git commit -m "message".
The command to attach local repo to GitHub remote repo is:
-
-
git remote add origin <URL>
-
-
Explanation:
To connect your local Git repository to a GitHub remote repository, the command used is git remote add origin <URL>. The word origin acts as a default name for the remote server, and <URL> is the GitHub repository address where the code will be pushed. Other options are incorrect because git connect repo and git link <URL> are not valid Git commands, and git fork is used for making a copy of a remote repository on GitHub, not for linking a local repo. Thus, git remote add origin <URL> is the correct way to establish the connection.
To upload local commits to GitHub, use:
Explanation:
Once changes are committed in your local repository, the command git push is used to upload those commits to the remote repository like GitHub. This makes your work available online and allows others to pull, collaborate, or review it. git pull downloads changes instead of uploading, git clone copies a remote repo to your computer, and git checkout switches branches. Therefore, git push is the correct command for uploading your committed changes to GitHub.
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Git Introduction
Git Installation and Setup
All Git Commands
Most Used Git Commands
Git Branch
Git Merge
Git Tools and Integration
Git Remote Repositories
Collaborating with Git
Advanced Git Commands