Skip to main content

Command Palette

Search for a command to run...

Git And Github

Published
5 min readView as Markdown
U

Transformative Tech Leader | Serial Entrepreneur & Machine Learning Engineer Leveraging 3+ years of expertise in Machine Learning and a background in Web Development, I drive innovation through building, mentoring, and educating. Passionate about harnessing AI to solve real-world problems."

What is Git?


Introduction to Git

Git is a popular version control system used widely by developers and teams for tracking changes in code. Created by Linus Torvalds in 2005 and currently maintained by Junio Hamano, Git helps manage and collaborate on code effectively, enabling multiple developers to work on projects from anywhere in the world.


Why Use Git?

  • Tracking Code Changes: Git records every change made to the codebase, helping track modifications and understanding the evolution of a project.

  • Identifying Contributors: Git tracks who made each change, so the contributions of every team member are clear.

  • Facilitating Collaboration: Git allows multiple developers to work on the same project without overwriting each other’s changes.


What Does Git Do?

Git’s primary functions help developers manage and maintain projects smoothly and are divided into a few main areas:

  1. Manage Projects with Repositories:

    • A repository is essentially a project or folder managed by Git. It keeps track of every version of the files in that folder.
  2. Clone a Project:

    • Developers can create a local copy of a project by cloning it from an online repository. This allows work to happen offline.
  3. Control Changes with Staging and Committing:

    • Changes are staged (selected for addition to the repository) and then committed (permanently saved to the repository), giving developers control over what changes are added.
  4. Branch and Merge:

    • Developers can create separate branches to work on different parts of a project or features simultaneously without impacting the main codebase. Later, branches are merged back into the main project.
  5. Pull and Push:

    • Pull: Updates the local copy of a project with the latest changes from the main repository.

    • Push: Sends local changes to the main project repository so others can access the updates.


Working with Git: Key Concepts and Workflow

  1. Initialize Git on a Folder:

    • When starting a new project, initialize Git in a folder. This action makes the folder a repository, and Git creates a hidden .git folder to track all changes.
  2. Making Changes and Staging:

    • Every time a file is changed, added, or deleted, Git detects it as modified.

    • To save changes, stage the files you want to add to the repository.

  3. Commit Changes:

    • After staging, commit the changes to take a permanent snapshot. This lets you save specific versions of your code, with each commit creating a checkpoint you can return to if needed.
  4. Track and View History:

    • Git enables you to view the full history of all commits, so you can see what changes were made and by whom. You can even revert to a previous commit if necessary.
  5. Efficient Storage:

    • Git doesn’t create a new copy of every file with each commit. Instead, it tracks the differences, which makes it memory efficient and faster.

  • Over 70% of developers use Git.

  • Git facilitates teamwork from any location, making it ideal for global collaboration.

  • Developers can track and view the entire history of a project, making it easy to review the evolution of code.

  • Git’s revert feature allows returning to earlier versions, making it safer to experiment with new ideas.


What is GitHub?

GitHub is a platform that hosts Git repositories, adding a collaborative layer on top of Git’s features. GitHub offers tools that work seamlessly with Git, helping developers share code, manage projects, and contribute to open-source projects. Acquired by Microsoft in 2018, GitHub is the largest host of source code in the world.

Remember: Git and GitHub are not the same. Git is the version control system, while GitHub is a hosting service that uses Git.

Installing Git and Initializing a Repository


Step 1: Download Git

Git is free to download and install. You can get it from the official Git website:


Step 2: Open the Command Line Interface

To start using Git, we’ll work with a command shell. Here are the options for different operating systems:

  • Windows: Use Git Bash, which is included with Git for Windows.

  • Mac & Linux: Use the built-in terminal.


Step 3: Verify the Git Installation

After installing Git, open your command shell and check if Git is installed correctly:

git --version

Expected Output:

If Git is installed, the output will show the version number, like:

git version 2.30.2.windows.1

Note: the version number depends on the current version you are installing, so this might differ from what you display on your system

Step 4: Configure Git with Your Name and Email

Every Git commit uses a name and email address to identify the contributor. Setting these globally will apply your details to every Git repository on your computer.

  1. Set Your Name:

     git config --global user.name "YourName"
    
  2. Set Your Email Address:

     git config --global user.email "youremail@example.com"
    
  3. Setting Name and Email per Repository:

    • If you want to specify a different name/email for a specific repository, leave out the --global flag.

    • Run the command within the repository folder.


Step 5: Creating a Project Folder

Let’s create a new folder for your Git project and navigate to it:

  1. Create a Directory:

     mkdir myproject
    
  2. Navigate to the Directory:

     cd myproject
    
  • mkdir creates a new directory (or folder).

  • cd changes the current working directory to the one you created.


Step 6: Initializing Git

Now that you’re in the correct directory, it’s time to initialize Git in that folder. This command will tell Git to start tracking changes:

git init

Expected Output:

Initialized empty Git repository in /path/to/your/myproject/.git/
  • Explanation: This command creates a hidden .git folder within your project directory, where Git will store all version history and track changes.

Tip: If you already have a folder you’d like to turn into a Git repository, navigate to it in your command line or right-click on the folder and select "Git Bash here" (for Windows users) to start working with Git immediately.


Summary

  • Download Git from https://www.git-scm.com.

  • Verify Installation: Use git --version.

  • Configure Name & Email: Use git config --global user.name and git config --global user.email.

  • Create & Navigate to Project Directory: Use mkdir and cd.

  • Initialize Git in the project directory with git init.


With these steps completed, you’re now ready to start working with Git on your project! In the next lesson, we’ll cover tracking and committing changes to your Git repository.

More from this blog

Coder Connect

23 posts

Join the coding revolution: learn, innovate, transform. The only limit is your mind."