Understanding Git the Easy Way

A Beginner’s Guide

William Maina
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨
3 min readSep 18, 2023

--

If you’re a web developer just starting with Git, you might have heard it’s a powerful tool but found it a bit daunting. Fear not! In this article, we’ll walk you through the fundamentals of Git in a beginner-friendly way. By the end, you’ll understand key concepts like branching, repositories, forking, rebasing, and merging, making version control a breeze.

Getting Started with Git:

Git is a distributed version control system that helps you manage your code, track changes, and collaborate with others effectively. Here’s a step-by-step guide to get you started:

1. Cloning a Repository:

To begin, you’ll need a Git repository. This is where your code lives. GitHub, GitLab, and Bitbucket are popular platforms for hosting Git repositories. To start working on an existing project:

Clone: Clone a repository from a remote source to your local machine using the git clone command. For example:

<code>git clone https://github.com/username/repo.git</code>

2. Understanding the Basics:

Before diving into advanced concepts, let’s clarify some fundamental terms:

Branching: Think of branches as separate lines of development. You can create branches to work on new features, bug fixes, or experiments without affecting the main codebase.

Main/Master: Typically, Git repositories have a default branch called main or master. This branch represents the latest stable version of your code.

Origin: Origin is the default name for the remote repository on the platform (e.g., GitHub). When you clone a repository, it’s automatically set up to track the origin remote.

3. Making Changes:

Now, you can start making changes to your code. Edit files, add new ones, and delete old ones. After making changes, follow these steps to save your work:

Add: Use git add to stage changes you want to commit. For example:

<code>git add filename.js</code>

also common is (git add .) where the period is for all changes.
Commit: Create a snapshot of your changes with git commit and a descriptive message:

<code>git commit -m “Add feature X” </code>

4. Branching and Merging:

Branching is a core concept in Git, enabling you to work on different features simultaneously. Here’s how you create, switch, and merge branches:

Create a Branch: To create a new branch and switch to it, use git checkout -b:
<code>git checkout -b new-feature</code>
Switch Branches: To switch between branches, use git checkout followed by the branch name.

Merge Branches: When you’re done with your feature, you can merge it into the main branch:

<code>git checkout main</code>
<code>git merge new-feature</code>

5. Forking:

Forking is a process that lets you make a copy of someone else’s repository on GitHub into your account. It’s useful when you want to contribute to open-source projects. After forking, clone your forked repository to your local machine and work on it.

6. Rebasing:

Rebasing is a way to incorporate changes from one branch into another. It’s often used to keep your commit history clean. Here’s how you rebase:

Update Your Main Branch: Before rebasing, make sure your main branch is up-to-date:

<code>git checkout main</code>
<code>git pull origin main</code>

Rebase Your Feature Branch: Switch to your feature branch and rebase it on main:
<code>git checkout feature-branch</code>
<code>git rebase main</code>
Resolve Conflicts: If there are conflicts during the rebase, Git will prompt you to resolve them manually.

7. Pushing Changes:

When you’re satisfied with your work, it’s time to push your changes to the remote repository:

Push: Use git push to send your commits to the remote repository (e.g., GitHub):
<code>git push origin main</code>
Now your changes are available for others to see and collaborate on.

8. Git Stash (Bonus):

Sometimes, you may have changes in your working directory that aren’t ready for a commit, but you need to switch branches or perform other actions. Git stash helps you temporarily save these changes:

Stash Changes: Use git stash to save your changes:
<code>git stash</code>
Apply Stash: Later, when you’re ready, you can apply your stashed changes:<code> git stash apply</code>

Conclusion: Understanding Git is crucial for web developers. With this beginner’s guide, you’ve learned the essentials of version control, including branching, forking, rebasing, merging, and pushing changes. Git empowers you to collaborate effectively, track your work, and contribute to projects confidently. Happy coding!

--

--

William Maina
𝐀𝐈 𝐦𝐨𝐧𝐤𝐬.𝐢𝐨

William Maina. Bsc. Math/CS, founder Ibconcept , Dynamic Entrepreneur, Digital Trailblazing Professional