Push Code to GitHub Using Command Line: A Simple Guide
Welcome back to Web Tech Knowledge! In this guide, we'll walk you through the process of pushing your code to GitHub using only the command line. No complicated tools are needed – just a few simple commands. Let's get started!
Prerequisites
Before we begin, make sure you have the following:
- Git: Download and install Git on your machine. If you're unsure how to do this, check out our previous video on Git installation.
- GitHub Account: Create a free GitHub account if you don't already have one.
StepbyStep Guide
1. Navigate to Your Project Folder
First, locate the folder containing your project files. Open a command prompt (cmd) within that folder. You can do this by typing cmd
in the folder's address bar.
2. Initialize a Git Repository
Once the command prompt is open, initialize a Git repository within your project folder using the following command:
git init
3. Add Your Files
Next, add all the files in your project folder to the staging area. This prepares the files to be committed. Use the following command:
git add .
The .
(dot) represents all files in the current directory.
4. Commit Your Changes
Now, commit your staged changes with a descriptive message. This creates a snapshot of your code. Use the following command:
git commit m "First commit"
Replace "First commit"
with a relevant message describing your changes.
5. Create a GitHub Repository
Head over to your GitHub account and create a new repository. You can click on the "New" button. Give your repository a name (e.g., "demo") and optionally add a README file. Then, click "Create repository."
6. Link Your Local Repository to the Remote Repository
Copy the repository URL from GitHub. You can find it on the repository's main page by clicking the "Code" button. Then, go back to your command prompt and link your local repository to the remote GitHub repository using the following command:
git remote add origin <repository_url>
Replace <repository_url>
with the URL you copied from GitHub.
7. Push Your Code to GitHub
Finally, push your local code to the GitHub repository. Use the following command:
git push u origin master
You may be prompted to log in to your GitHub account.
This command pushes your code to the master
branch of your GitHub repository. The u
flag sets the upstream branch, so subsequent pushes can be done with just git push
.
Conclusion
That's it! You've successfully pushed your code to GitHub using only the command line. You can now view your code on your GitHub repository.
If you found this guide helpful, please like this video and subscribe to Web Tech Knowledge for more tutorials. Let us know in the comments below if you'd like to learn more Git basics!