Beginner-Friendly Introduction to Git & GitHub
Beginner-Friendly Introduction to Git & GitHub
Getting Started with Git and GitHub
Git is a distributed version control system widely used by developers to manage and track changes in source code during software development. It enables teams and individuals to work collaboratively on projects while keeping a history of every change made to the codebase. Here are some key features of Git:
- Version Control:It tracks changes, so you can revert to earlier versions of your work if needed.
- Branching and Merging:Developers can create independent branches for features or fixes, then merge them seamlessly into the main project.
- Collaboration:Git allows multiple people to work on the same project simultaneously without overwriting each other's changes.
- Distributed System:Each user has a complete copy of the repository, so they can work offline and still access the full history.
GitHub is a cloud-based platform that provides storage for your projects. In this guide, you'll learn how to create a new repository on GitHub, clone it locally, add files, and push your changes back to GitHub. Follow along and try the commands on your own!. Make sure you have Git Installed Download Git
We will begin by visiting github.com to create a new account. After verifying your account
Step 1: Create a New GitHub Repository
create new repository using the + sign at the top of your account
- Go to GitHub's New Repository Page.
- Enter a repository name, for example: MyFirstRepo.
- Choose "Public" or "Private" as per your preference.
- Check "Add a README file" (this creates an initial file you can see later).
- Click "Create repository".
Step 2: Clone Your Repository Locally
In your repository, Locate to the right top and click on the code and copy the link
Now, create a new folder on your computer where you'd like to store your project. Right-click in the folder, choose "Open Git Bash here, and run the following command:
git clone https://github.com/your-username/MyFirstRepo.git
This command downloads your repository to your local machine. You can now see the README.md file that GitHub automatically added.
Step 3: Make Changes Locally
Inside your cloned folder, create a new file. For example, create a file named example.txt and write something inside it.
After saving your changes, open your terminal and type the following command to check your repository's status:
git status
You should see that example.txt is listed as an untracked file.
Step 4: Push Your Changes to GitHub
Follow these commands to add your new file, commit your changes, and push them to GitHub:
- Stage your file:
git add example.txt
- Commit your changes:
git commit -m "Add example.txt with initial content"
- Push to GitHub:
git push origin main
Once the push is complete, go to your GitHub repository page and refresh it. You should see the example.txt file listed along with your changes.
Using VS Code with Git and GitHub
Visual Studio Code (VS Code) is a powerful, free code editor that integrates well with Git. In this section, you'll learn how to use VS Code's integrated terminal (using Bash) to clone your repository, make changes, and push your code back to GitHub.
You can download VS Code from the official website: Visual Studio Code.
Step 1: Open Your Repository in VS Code
- Open VS Code.
- From the menu, click on File > Open Folder and select the folder where you want your project to reside.
Step 2: Open the Integrated Terminal with Bash
- Click on Terminal > New Terminal in the top menu.
- Ensure the terminal is set to Bash. If it's not, click the dropdown in the terminal window to change the default shell.
Step 3: Clone Your GitHub Repository Using Bash
In the integrated terminal, navigate to your preferred folder and run the following command to clone your repository:
git clone https://github.com/your-username/MyFirstRepo.git
This command downloads your repository into a new folder inside your current directory. You can now see the repository files, including the README.md file that you added on GitHub.
Step 4: Explore Your Repository
Once the cloning is complete, use the VS Code file explorer (on the left side) to view your project files. You should see the README.md along with any other files from your repository.
Step 5: Make Changes and Commit Your Code
Let's make a change to your repository. Follow these steps:
- Create a new file in VS Code (e.g., demo.txt) and add some text content to it.
- Save the file (choose File > Save or press
Ctrl+S
). - Return to the integrated terminal and check the status of your repository by typing:
git status
You should see that demo.txt is listed as an untracked file.
- Stage the new file by typing:
- Commit your changes with a message:
- Push your changes to GitHub:
git add demo.txt
git commit -m "Add demo.txt with initial content"
git push origin main
After the push is complete, visit your GitHub repository in your web browser to verify that demo.txt is now part of your repository.
Keeping Your Code Safe
While Git and GitHub are secure, follow these best practices to keep your code safe:
- Use SSH keys for authentication: Read more in the GitHub SSH Guide.
- Add sensitive files (like
.env
or configuration files) to a.gitignore
file. See the .gitignore Documentation for details. - For private projects, make sure your repository is set to "Private": Check Repository Visibility.
Additional Tools for Git and GitHub
Enhance your workflow with these useful tools:
- Git CLI – The command-line tool for Git.
- GitHub Desktop – A graphical interface for GitHub.
- GitKraken – A feature-rich Git GUI.
- GitLens – An extension for VS Code that enhances Git capabilities.
Comments
Post a Comment