A Newcomer's Guide to Git and GitHub: My First Experience

A Simple Way to Understand Git and GitHub

Imagine you're writing a big story with your friends, and each of you is working on different chapters. You want to keep track of what everyone is doing, but you don't want to accidentally overwrite someone else's work. This is where Git and GitHub come in.

Git is like a personal notebook that records every change you make to your story. Every time you add or modify something, Git writes it down. If you make a mistake, you can always go back to a previous version and start over. It's like having a history of everything you’ve done so far.

Now, imagine that instead of keeping your story in a notebook, you decide to save it in an online library where all your friends can access it. This online library is GitHub. It allows everyone to store their changes in one place, share their work with others, and even contribute to the story together. GitHub makes it easy for everyone to collaborate, track changes, and keep everything organized.


What Exactly Are Git and GitHub

As a developer, you’ll encounter many situations where you need to manage and collaborate on code. Git and GitHub are tools that help make this process seamless and efficient.

What is Git?

Git is a version control system that tracks code changes, enables collaboration, and allows developers to revert to previous versions. It’s essential for both solo and team projects.

What is GitHub?

GitHub is a cloud-based platform for hosting Git repositories, enabling collaboration, sharing code, and managing development with tools like pull requests and issue tracking.

Why Are Git and GitHub Essential for Developers?

  1. Collaboration: Git and GitHub make it easier to collaborate with others. Developers can work on different parts of a project simultaneously without interfering with each other’s work.

  2. Version Control: With Git, you can keep track of every change made to your code. This means you can easily revert to an earlier version if something breaks or if you need to compare changes.

  3. Backup and Safety: Storing your code on GitHub ensures that you always have an online backup. If something happens to your local machine, your code is safe and accessible from anywhere.

  4. Code Review and Quality Control: GitHub provides a way to review code changes through pull requests. This helps ensure code quality and consistency, as others can review your work and suggest improvements before merging it into the main project.

  5. Open-Source Contribution: GitHub is a hub for open-source projects. By contributing to these projects, developers can learn from others, improve their skills, and collaborate on a global scale.

Installing Git on Windows

One of the first steps I took to learn Git was installing it on my Windows machine. Here’s how I did it:

  1. Download Git:

    • I visited git-scm.com to download the latest version of Git for Windows.

    • The website provides an easy-to-follow interface for downloading the appropriate installer

      .

  2. Installation:

    • After downloading, I ran the installer and followed the installation prompts.

    • During the setup, I chose the default options, which are beginner-friendly.

  3. Verify Installation:

    • Once the installation was complete, I opened the command prompt (or PowerShell) and typed:

      git --version

    • This command confirmed that Git was installed correctly by displaying the installed version number.

Before Git: Learning Basic Windows Commands

To get comfortable with Git, I started by learning a few basic Windows commands that are essential for working with the command line. Here's a quick overview:

  1. List Files and Folders

Command: dir

This command displays the list of files and folders in the current directory.

  1. Change Directory

Command: cd <folder_name>

Used to navigate into a specific folder. For example: cd Documents

  1. Go Back to the Previous Directory

Command: cd ..

This moves you one level up in the directory structure.

  1. Create a Folder

Command: mkdir <folder_name>

For example, to create a folder named "gitpractice": mkdir gitpractice

  1. Create a File

Command: echo > <file_name>

Example: To create a file named example.txt: echo > example.txt

Alternatively, you can use: notepad <file_name>

This opens the file in Notepad for editing.

  1. View File Content

Command: type <file_name>

Displays the content of a file in the terminal.

  1. Clear the Command Line

Command: cls

Clears all the text from the terminal screen.

Understanding these commands is crucial because Git operations often involve navigating directories, creating files, and interacting with the file system


Getting Hands-On with Git: Creating a Practice Repository

To understand how Git works, I decided to create a practice repository. Here's how I went about it:

Step 1: Creating a Folder

I created a folder named gitpractice inside the Desktop folder to use as my Git repository.

Command: mkdir gitpractice

cd gitpractice

Step 2: Initializing Git

Next, I initialized Git in this folder. This step creates the .git folder, where all Git-related data (like commit history and configuration) is stored.

  • Command: git init

  • Output: Initialized empty Git repository in C:/Users/YourName/Desktop/gitpractice/.git/

  • This confirmed that Git was successfully initialized in the folder.

Step 3: Observing the .git Folder

  • I noticed that a hidden folder named .git was created. This folder contains all the metadata and history of the repository.

  • To see it on Windows, I used: dir /a

  • Alternatively, enabling “Show Hidden Files” in File Explorer also reveals the .git folder.

Step 4: Adding Files and Making Changes

To see how Git tracks changes, I created a new file and modified it:

  • Creating a file: echo > example.txt

  • Checking the file’s content: type example.txt

  • Making changes to the file: I edited the file using Notepad: notepad example.txt

    I added the text: "This is my first Git experiment!".


How Git Tracks Changes

After making changes, they can be tracked using Git commands:

  • Checking the status of the repository:

    Command : git status

  • Adding the file to staging:

    Command : git add example.txt

  • Committing the changes:

    Command : git commit -m "Added example.txt to gitpractice folder"

Getting Started with GitHub

After getting familiar with Git, I wanted to take the next step and explore how GitHub fits into the picture. Here's how I created my GitHub account and repository:

Step 1: Creating a GitHub Account

  1. I went to GitHub and clicked on "Sign Up."

  2. I followed the prompts to create my account, including verifying my email address.

Step 2: Creating a New Repository on GitHub

  1. Once logged into GitHub, I clicked on the + icon in the top-right corner and selected "New repository".

  2. I filled in the details:

    • Repository Name: GitPractice

    • Description: A practice repository to learn Git and GitHub.

    • I chose public as the repository type and clicked Create repository.

  • GitHub then gave me the URL of my new repository, which will look something like:

    github.com/YourUsername/GitPractice

    Step 3: Connecting Local Git Repository to GitHub

    Now that I have a GitHub repository, I needed to link it to my local Git repository (gitpractice folder) to push changes. Here's what I did:

    1. First, I went to my local gitpractice folder in the terminal.

    2. Then, I linked my local repository to the GitHub repository using the following command:

      git remote add origin https://github.com/YourUsername/GitPractice.git

    3. I verified the remote connection:

      git remote -v

  • Step 4: Pushing Local Changes to GitHub

    When I tried to push changes to GitHub for the first time, Git asked for my username and password. Since GitHub no longer allows password authentication, I had to generate a Personal Access Token (PAT). Here's what I did:

    1. Logged into my GitHub account and navigated to: Settings > Developer settings > Personal access tokens > Tokens (classic) > Generate new token.

    2. Selected the necessary scopes:

      • repo (for full access to private and public repositories).
    3. Generated the token and copied it. (Important: Save the token somewhere safe, as you won’t be able to see it again.)

    4. To upload my local repository to GitHub, I used the following commands:

git push -u origin master

  1. To avoid entering the token every time, I configured Git to store my credentials using:

git config --global credential.helper store

Basic Git Commands for Branching

Branching in Git allows you to work on different tasks or features in isolation, without affecting the main branch. Let’s walk through branching with a practical example using the gitpractice folder and an example.txt file.

1. Create a New Branch

Let’s say you want to add a new feature to your project. Start by creating a new branch:

git branch feature-update-example

This creates a new branch called feature-update-example. You can check your branches using:

git branch

The current branch will be highlighted (e.g., master).

2. Switch to the New Branch

To work on the new feature, switch to the feature-update-example branch:

git checkout feature-update-example

Now, any changes you make will be saved to this branch.

3. Make Changes on the Branch

Let’s modify the example.txt file in the gitpractice folder to simulate changes:

  1. Open the example.txt file in any text editor and add the following line:

    Adding a new feature to the project.

2. Save the file.

Next, stage and commit the changes:

git add example.txt

git commit -m "modified example.txt"

4. Merge the Branch into the Main Branch

Once you’re done working on the feature, merge the changes into the main branch:

  1. Switch back to the master branch:

    git checkout master

2. Merge the feature-update-example branch:

git merge feature-update-example

At this point, the changes you made to example.txt in the feature branch will now be part of the main branch.

5. Delete the Feature Branch

After merging, you can clean up by deleting the feature branch:

git branch -d feature-update-example

5. Syncing Branch with GitHub

We can push the changes using below command as covered earlier

git push -u origin master

On Github you can see the example.txt got updated

Visualize Git Branching in Action

Understanding branching can feel abstract at first, but there’s a fantastic way to make it more tangible: Learn Git Branching.

This interactive tool is perfect for visualizing how branches, commits, and merges work together. It provides a sandbox where you can practice Git commands like:

  • git branch – to create a new branch.

  • git checkout – to switch between branches.

  • git merge – to combine changes from different branches.

As you type commands, the tool dynamically generates diagrams to help you see how Git tracks your project's history. Whether you're just getting started or refining your skills, it’s a must-try resource!

While I didn’t create diagrams for this blog, I highly recommend giving the tool a try—it’s a game-changer for beginners.

Collaborating on GitHub: Pull Requests, Forking, and Cloning

GitHub shines brightest when it comes to collaboration. Here are three key features you’ll use when working with others:

Forking a Repository

Forking allows you to create a personal copy of someone else’s repository under your account. It’s commonly used for contributing to open-source projects.
Here’s how it works:

  1. Navigate to the repository you want to fork on GitHub.

  2. Click the Fork button in the top-right corner.

  3. A copy of the repository is now in your GitHub account, where you can freely make changes.

Cloning a Repository

Once you’ve forked a repository, you can clone it to your local machine to work on it:

git clone <repository-url>

This creates a local copy of the repository on your system so you can modify files, commit changes, and push updates.

Making a Pull Request

After making changes to the forked repository, you can contribute those changes back to the original repository using a pull request:

  1. Push your changes to your forked repository.

  2. On GitHub, navigate to your forked repository and click Pull Requests.

  3. Click New Pull Request and follow the instructions to submit your changes for review.

Pull requests are how teams review and discuss changes before merging them into the main codebase. They are also a fantastic way to contribute to open-source projects!

Handling Merge Conflicts:

When multiple people work on the same file, Git might struggle to merge changes automatically. This results in a merge conflict. Don’t worry—it’s a normal part of collaboration, and resolving it is straightforward:

  1. When a Conflict Occurs:
    Git will notify you during a merge if there’s a conflict. The conflicting file will contain markers like this:

2. Resolve the Conflict:
Open the file, decide which changes to keep (or combine both), and delete the markers.

3. Stage and Commit the Fix:

git add

git commit -m "Resolved merge conflict in <file>"

Merge conflicts might seem intimidating at first, but they become easier with practice!

Get Hands-On with Git: Watch This Tutorial

Git can be tricky to grasp at first, but there are fantastic resources to help you master it. One highly recommended video is Git for Beginners, which simplifies Git concepts with visuals and examples.

Whether you’re learning about commits, branching, or pull requests, this video is a great way to reinforce what you’ve read here.

Let’s Talk About Your Git Journey!

What was your biggest hurdle when learning Git or GitHub? Share your experience in the comments below, and let’s discuss! If you’ve already started using Git and GitHub in your own projects, feel free to share the link to your repo—I’d love to check it out!