We may already have an existing directory where we were devloping code, then we want to upload it to Github. In this tutorial, we’ll walk you through the steps to get your folder uploaded to GitHub.
Step 1: Create a New Repository on GitHub
The first step is to create a new repository on GitHub where you can store your code. To do this, log in to your GitHub account and click on the “New Repository” button on the main dashboard.
Next, give your repository a name, description (optional), and select any other desired settings for your repository. Make sure to select the “Initialize this repository with a README” option to create an initial README.md file in your repository.
Once you’ve entered all the required details, click on the “Create Repository” button to create your new repository.
Step 2: Initialize a Local Git Repository
Now that you have a repository set up on GitHub, it’s time to create a local Git repository on your computer. To do this, open a terminal window on your computer and navigate to the folder that you want to upload to GitHub.
Once you’re in the folder, run the following command to initialize a new Git repository:git init
This command will create a new .git folder in your folder, which will store all the necessary Git metadata and tracking information.
Step 3: Add and Commit Your Files
With your local Git repository set up, you can now add and commit your files to it. To add all the files in your folder to the repository, run the following command:
git add .
This command will stage all the files in your folder for committing. To commit the changes, run the following command:
git commit -m "Initial commit"
This command will commit your changes to the local repository with a message “Initial commit”.
Step 4: Connect Your Local Repository to the Remote Repository on GitHub
Now that you have a local Git repository set up and have committed your changes, it’s time to connect your local repository to the remote repository on GitHub. To do this, you’ll need to get the remote repository URL from GitHub.
To get the remote repository URL, go to the repository you created on GitHub and click on the “Clone or download” button.
Next, copy the HTTPS or SSH URL for your repository.
With the remote repository URL in hand, you can now connect your local repository to the remote repository on GitHub. To do this, run the following command:git remote add origin <remote_repository_url>
This command will add a new remote named “origin” to your local repository and connect it to the remote repository on GitHub.
Step 5: Push Your Changes to GitHub
With your local and remote repositories connected, you can now push your changes to GitHub. However, before doing so, it’s important to make sure that you’re pushing to the correct branch on the remote repository.
By default, GitHub creates a “main” branch in your repository. However, some older repositories may still use the “master” branch. To check which branch your remote repository is using, run the following command:git branch -r
This command will list all the remote branches in your repository. The branch with an asterisk next to it is the currently checked out branch.
If the remote repository is using the “main” branch, you can push your changes to GitHub by running the following command:git push -u origin main
This command will push your changes to the “main” branch on the remote repository and set the upstream branch to track the remote branch with the same name.
If the remote repository is using the “master” branch, replace “main” with “master” in the above command.
Once the command has finished executing, your code will be available on GitHub for others to view and collaborate on.
One thing to notice is that:
When you run git push -u origin main
, Git will prompt you to confirm that you want to set the upstream branch and provide you with a suggested command to do so if you choose to proceed.
The suggested command will look something like this:git push --set-upstream origin main
This command tells Git to push your changes to the “main” branch on the “origin” repository and set the upstream branch to track the remote branch with the same name.
By setting the upstream branch, you can use shorthand Git commands like git push and git pull without having to specify the remote branch name each time.
If you choose to proceed with the suggested command, Git will push your changes and set the upstream branch for you.