Table of contents
- What is Git?
- What then is GitHub?
- Essential Components of a Git Repository
- Git Commands and What They Do!
- 1. git init git init
- 2. git add git add index.html
- 3. git commit git commit -m "Added homepage"
- 4. git status git status
- 5. git log git log
- 6. git remote git remote add origin https://github.com/yourusername/your-repo.git
- 7. git push git push -u origin master
- 8. git pull git pull origin master
- 9. git clone
- git clone https://github.com/yourusername/your-repo.git
- 10. git branch git branch
- 11. git checkout git checkout feature-branch
- RECAP OF TERMS USED IN THE ARTICLE.
Are you tired of hearing about "Git" and "GitHub" without really understanding what they are? Don't worry; you're not alone! The constant requests to share your GitHub profile or link can be frustrating for someone unfamiliar with these tools. I've been there too!
Finding the right tutorial as a total beginner seemed like an impossible task. But my determination to overcome this annoyance drove me to learn Git and GitHub through every available resource. Now, I'm thrilled to present the result of my journey: The Ultimate Simplified Guide!
Even a 9-year-old could grasp the concepts explained in this tutorial. So, let's dive in and make Git and GitHub your new best friends! Get ready to explore the world of version control with ease!
What is Git?
To explain Git, let's use two illustrations:
Illustration 1:
Imagine you had a magical notebook with just one page in it, where you draw beautiful pictures every day. Sometimes, you want to change something in your drawing or try a different idea that had recently popped into your brain โ your "inspirational moments." Now, instead of erasing and starting over, wouldn't it be cool if you had many copies of your notebook, so you can try different things in each one?
That's kind of what Git does! It's like your magical copies of the notebook for your computer programs and codes. It helps you keep track of all the changes you make, just like your different drawings in the magic notebooks.
Illustration 2:
Imagine you're writing a story in a special notebook. As you write, you realize you want to change a few things, but you also want to keep the original version safe, just in case. So, you decide to use many pages in the notebook, each page showing a different version of your story with changes.
Git works a bit like that notebook for computer programmers. It helps them keep track of all the changes they make to their code. Instead of using physical pages, Git stores different versions of the code in a digital way. This way, programmers can experiment, fix mistakes, and work with others without worrying about losing their original work. It's like having a safety net for their code, so they can be more confident and creative while building amazing computer programs!
What then is GitHub?
Now that we know what Git is, let's talk about GitHub. To help you understand it, let's refer back to the first illustration we used for Git.
First Scenario - Magical Notebook:
In this scenario, Git was like the magical notebook where you have different versions of your computer work. It helped you keep track of changes and allowed you to experiment without losing your original work.
Now, imagine GitHub as a fantastic playground where not only you but all your friends who have their own magical notebooks (these are called Git repositories) can come together.
Git repositories are like special folders or containers that hold all the versions and history of a project's code and files.
GitHub provides a big digital space where you and your friends can share your notebooks with each other. Just like you can see your friends' drawings and they can see yours, programmers using GitHub can share their code with others and collaborate.
Just like working on different pages of the magical notebook, on GitHub, everyone can work on their separate versions of the code (these are called branches) and then come together to combine their changes into one awesome final version (this process is called a merge).
So, in this context, GitHub is like a big platform where programmers come together, share their code (magical notebooks), and collaborate on creating amazing software, just like you and your friends create beautiful drawings together!
Essential Components of a Git Repository
A Git repository contains several essential components:
Versioned Files: The files in your project are stored inside the repository. Every time you make a change to a file, Git keeps track of those changes and saves a snapshot of the file at that point in time. This allows you to go back and see the history of the file, as well as revert to previous versions if needed.
Commit History: Git maintains a history of all the changes made to the files in your project. Each time you save a snapshot of your project (called a "commit"), Git records what changes were made, who made them, and when they were made. This commit history is like a timeline of your project's development.
Branches: A repository can have multiple "branches," which are different lines of development. Branches allow you to work on new features or bug fixes without affecting the main or "master" version of the project. Once you're done working on a branch, you can merge it back into the main version.
Collaboration: Git repositories are often used for collaborative projects. Developers can clone (copy) a repository to their local computer, make changes, and then push (upload) those changes back to the central repository on a remote server, such as GitHub or GitLab. This way, team members can work together on the same project, and Git helps manage the changes and prevent conflicts.
Git Commands and What They Do!
Before getting to work on git commands, I'd advise that you install Git in your VS Code CLI.
Here are the examples of the Git commands in code form, along with explanations of what they do:
1. git init git init
Initializes a new Git repository in your project folder. Creates a hidden .git directory that stores version control information.
2. git add git add index.html
Stages or adds the changes made to the index.html
file to the "staging area." Prepares the changes for the next commit. It is noteworthy that you should replace the file with your file name.
3. git commit git commit -m "Added homepage"
Creates a new version (commit) of your project with the changes staged in the previous step. The commit message "Added homepage" describes what changes were made in this commit.
4. git status git status
This shows the current status of your repository. Displays which files have been modified, which files are staged for the next commit, and which files are untracked.
5. git log git log
Displays a list of all the commits made in your repository. Shows the commit hash, author, date, and commit message for each commit.
6. git remote git remote add origin
https://github.com/yourusername/your-repo.git
Connects your local repository to a remote repository on GitHub. The remote repository URL is where you can collaborate with others and push your changes.
7. git push git push -u origin master
Sends (also known as a push) your committed changes from your local repository to the remote repository (on GitHub). The -u origin master
sets the default upstream branch for future pushes.
8. git pull git pull origin master
Fetches and merges changes from the remote repository into your local repository. i.e. it combines the changes you have made on your local computer with the version of the remote repository on Git hub. Ensures your local repository is up-to-date with the latest changes from others.
9. git clone
git clone
https://github.com/yourusername/your-repo.git
Creates a copy of a remote repository on your local machine. Used to start working on an existing project or contribute to someone else's project.
10. git branch git branch
Lists all the branches in your repository. Helps you see which branch you are currently on and what other branches exist. Each branch represents a separate line of development, allowing you to work on different features or versions of your project.
The git branch
command will show you the names of all the branches, and it marks the branch you are currently on with an asterisk (*
). This way, you can easily identify which branch you are currently working on.
For example, if you run git branch
in your Git repository, the output might look like this:
markdownCopy code* master
feature-branch
bug-fix-branch
development
In this example, the *
next to the master
branch indicates that you are currently on the master
branch. The other branches listed (i.e., feature-branch
, bug-fix-branch
, and development
) are other branches that exist in the repository.
11. git checkout git checkout feature-branch
Switches to the feature-branch
, allowing you to work on a different feature or version of the project.
Please note that some of these commands require you to be in a Git repository (created with git init
) and may require proper configuration and access permissions when working with remote repositories on platforms like GitHub.
RECAP OF TERMS USED IN THE ARTICLE.
Repo (Repository): Imagine a magic box where you keep all your toys safe. A "Repo" is like that magic box for computer programs and files. It's a special place where we store all the different versions of our computer work, like a treasure chest full of our favorite toys!
Branches: Picture a big tree with many branches, and each branch has its own set of leaves. In Git, "Branches" are like these separate parts of a tree. They let us work on different ideas without messing up the main part. We can play with one set of leaves while keeping the other branches safe.
Merge: Imagine you and your best friend drew two parts of a beautiful painting, and now you want to put them together to make one amazing artwork. "Merge" in Git is like that! It's when we take the best parts from different branches and combine them to make something fantastic!
Commit: Think of "Commit" like taking a snapshot of your favorite drawing and putting it in a photo album. In Git, a "Commit" is like saving a special version of your work, so you can always go back and see it later, just like flipping through your photo album to see your favorite pictures.
Push and Pull: Imagine you have a magic wand that can send your drawings to your friends instantly. "Push" is like using that wand to send your work to a special place where your friends can see it too. And when your friends have new drawings, "Pull" is like using the magic wand to bring those drawings to your special place!
GitHub: Think of GitHub like a big playground where you and your friends can show your amazing drawings and share your toys. It's a place where people from all around the world can work together on their projects, share their magic boxes (Repositories), and make cool stuff as a team!
So, in simple words, "Repo" is a magic box for computer work, "Branches" are separate parts like tree branches, "Merge" is combining the best parts, "Commit" is saving snapshots, "Push" is sending your work, "Pull" is getting your friends' work, and "GitHub" is a fun playground where everyone can work together and create awesome things! ๐จ๐ณโจ