Get Started with Git: Create Your First Repository, Commit, and Push

This guide is designed for absolute beginners who want to learn how to track their code changes and collaborate with others using Git. By the end of this tutorial, you will have successfully initialized your first local Git repository, recorded your changes with a commit, and pushed your code to a remote hosting platform.

In the modern software development landscape, version control is an absolute necessity. Whether you are writing simple HTML pages, building complex web applications, or writing documentation, tracking the history of your files ensures you never lose work. Learning Git for beginners can initially feel like learning a foreign language, but once you master the core concepts, it becomes an indispensable part of your daily workflow. Mastering Git is a foundational skill that every developer must acquire to build reliable software.

A cozy home office desk featuring a laptop displaying a terminal window with code, a notebook with a pen, and a warm cup of coffee under a soft desk lamp.

What to Check Before You Begin

Before running your first Git command, you need to ensure your environment is correctly configured. Taking a few minutes to verify your system setup now will prevent confusing errors later.

  1. Check Your Operating System: This guide supports current operating systems, including Windows 11, macOS Sonoma or Sequoia, and modern Linux distributions (such as Ubuntu 22.04 or later).
  2. Verify Git Installation: Open your terminal (Terminal on macOS/Linux, or Command Prompt / PowerShell / Git Bash on Windows) and type the following command to check if Git is installed:
    git --version
    
    If Git is installed, you will see an output like git version 2.43.0. If the command is not recognized, download and install the latest version from the official Git website.
  3. Set Up Your Text Editor: Ensure you have a text editor installed, such as Visual Studio Code, Sublime Text, or Notepad++.
  4. Create a Dedicated Project Folder: Create a new, empty directory on your computer where you will store your practice files.
  5. Save and Close: Save any open files in other programs and close unnecessary applications to keep your workspace clean.

Git for Beginners: Setting Up Your First Repository

A Git repository (often shortened to “repo”) is a virtual storage space where your project’s files and their entire revision history live. When you initialize Git in a folder, it starts tracking every change made to the files within that directory.

To set up your very first repository, follow these steps:

  1. Open Your Terminal: Launch your terminal or command line tool.
  2. Navigate to Your Project Directory: Use the change directory command (cd) to navigate to the empty folder you created. For example, if your folder is on your desktop, run:
    cd Desktop/my-first-project
    
  3. Initialize the Repository: Run the initialization command to turn this folder into a Git repository:
    git init
    
    Upon running this, Git will output a message stating: Initialized empty Git repository in /path/to/your/project/.git/. This command creates a hidden folder named .git inside your directory. This hidden folder contains all the metadata and tracking history for your project. Do not delete or modify this folder manually.

When starting any new software project, security and structure should be established right from the beginning. Implementing proactive methods to secure your software from the very first commit helps protect your intellectual property and prevents vulnerabilities from slipping into production.

How Git Identifies Your Files: The Math Behind the SHA-1 Hash

To guarantee data integrity, Git does not rely on filenames to track changes. Instead, it uses a cryptographic hashing algorithm called SHA-1 (Secure Hash Algorithm 1). Every file, directory, and commit in Git is converted into a unique 160-bit hash value, represented as a 40-character hexadecimal string.

The exact standard formula Git uses to calculate a file’s unique hash (known as a blob) is:

Hash = SHA1(“blob ” + size_in_bytes + “\0” + content)

Let us work through a concrete example using this formula. Suppose you create a file containing only the word “Hello” with no trailing newlines or spaces.

  1. Determine the Size: The text “Hello” consists of 5 characters. In standard UTF-8 encoding, this equals exactly 5 bytes.
  2. Construct the Header: The header is the word "blob", followed by a space, the size in bytes (5), and a null byte (\0).
  3. Combine Header and Content: The complete string to be hashed is "blob 5\0Hello".
  4. Calculate the SHA-1 Hash: When you run this combined byte sequence through the SHA-1 algorithm, it outputs the following unique hexadecimal hash: f04a87b9e57d4212de99a9ec9b4d57b41196153d

No matter where or when this file is created, Git will always generate this exact hash for this specific content. This mathematical certainty ensures that your code cannot be corrupted or altered without Git detecting it immediately.

A glowing digital padlock composed of geometric data blocks with blue and gold binary code streams, symbolizing cryptographic security and code integrity.

Making Your First Commit

A commit in Git is like a save point in a video game. It records the exact state of your files at a specific moment in time. Before you can commit, you must understand Git’s three-stage architecture: the Working Directory (where you edit files), the Staging Area (where you prepare files for a commit), and the Repository (where commits are permanently stored).

Follow these steps to create your first commit:

  1. Create a New File: In your text editor, create a file named README.md inside your project folder. Add a single line of text, such as # My First Git Project, and save the file.
  2. Check the Status: In your terminal, run the status command to see how Git views your directory:
    git status
    
    You will see README.md listed in red under a section titled “Untracked files”. This means Git sees the file but is not tracking changes to it yet.
  3. Stage the File: To prepare the file for your commit, move it to the staging area using the add command:
    git add README.md
    
    If you run git status again, the filename will now appear in green under “Changes to be committed”.
  4. Commit Your Changes: Record this snapshot to your repository by running the commit command with a descriptive message:
    git commit -m "Initial commit"
    
    The terminal will display a confirmation message showing that one file has changed and has been inserted into the repository history.

Learning to write clean, descriptive commit messages is an essential habit. Clear history prevents the common errors in AngularJS development and other major frameworks where developers struggle to trace back when and why a bug was introduced into the codebase.

A computer monitor displaying a dark-themed terminal with a successful "git commit" command execution, with a softly blurred RGB keyboard in the foreground.

Pushing Your Changes to GitHub

Once you have recorded commits locally, the next step is to upload them to a remote hosting service like GitHub. This process is called “pushing” your code. It backs up your work and allows other developers to access it.

  1. Create a Remote Repository: Log into your GitHub account, click the New button in the repositories section, and name your repository my-first-project. Leave the options to initialize with a README, .gitignore, or license unchecked.
  2. Copy the Repository URL: On the setup page that appears, copy the HTTPS remote repository URL (it will look like https://github.com/your-username/my-first-project.git).
  3. Link Your Local and Remote Repositories: Go back to your terminal and run the following command, replacing the URL with your copied link:
    git remote add origin https://github.com/your-username/my-first-project.git
    
  4. Rename the Default Branch: Ensure your default branch is named main (the modern industry standard) by running:
    git branch -M main
    
  5. Push Your Code: Upload your local commit to GitHub using the push command:
    git push -u origin main
    
    The -u flag tells Git to remember these parameters, meaning that for all future updates to this branch, you only need to type git push.

In Git, pushing is a manual, secure transmission of your structured code history from your personal computer to a remote server.

An abstract visualization of data transfer. Shimmering lines of golden and blue light ascend from a stylized physical hard drive on a dark surface up into a glowing, cloud-like network of interconnected nodes in the sky. The scene represents seaml…

Why Version Control Matters in Everyday Use

In professional environments, Git is the backbone of daily operations. It acts as a digital time machine, allowing you to view your project’s state at any point in its history. If a newly added feature breaks your application, you do not have to manually undo your code line by line; you can simply roll back to a previous, stable commit.

Furthermore, Git makes seamless collaboration possible. Multiple developers can work on the same files simultaneously by creating separate “branches.” Once their features are complete and tested, they can merge their changes back into the main codebase. This workflow allows software teams to constantly push the boundary of what their products can do without risking the stability of the live system.

Git gives you complete freedom over your development environment. You can experiment boldly, knowing that your working directory is completely protected by a secure, local history.


Common Mistakes and How to Undo Them

As a beginner, you will inevitably make mistakes. Fortunately, Git is designed to make undoing mistakes straightforward. Here are three common scenarios and how to resolve them:

Scenario 1: You Made a Typo in Your Last Commit Message

If you committed your changes but realized you made a spelling error in your message, you can easily correct it (provided you have not pushed to GitHub yet) by running:

git commit --amend -m "Your corrected commit message"

Scenario 2: You Accidentally Staged the Wrong File

If you ran git add on a file that you did not intend to commit, you can remove it from the staging area while keeping your local edits intact by running:

git restore --staged filename.txt

Scenario 3: You Want to Revert a Commit That Broke Your Code

If you pushed a commit that caused errors and you want to safely undo its changes, find the 40-character commit hash using git log and run:

git revert <commit-hash>

This command creates a brand-new commit that applies the exact opposite changes of the broken commit, keeping your project history clean and collaborative.

A close-up of a developer's hands typing on a mechanical keyboard in a dimly lit room. On the screen, a terminal window shows a git command being successfully executed to undo a mistake, with clean code visible in an adjacent window. The atmospher…

A team of developers reviewing a Git commit history timeline on a large wall monitor in a bright modern office, photorealistic, 16:9 aspect ratio, no text in image, no watermark

Conclusion

Mastering Git is one of the most empowering steps you can take on your journey as a developer. By understanding how to initialize a repository, stage files, record commits, and push your work to GitHub, you have established a professional workflow that protects your code and enables collaboration. Keep practicing these core commands, and soon version control will become a natural, effortless part of your daily development routine.

Rating: 5.00/5. From 1 vote.
Please wait...


Welcome to our TECH CRATES blog, a Technology website with deep focus on new Technological innovations in Hardware and Software, Mobile Computing and Cloud Services. Our daily Technology World is moving rapidly into the 21th Century with nano robotics and future High Tech.

No comments.

Leave a Reply