Understanding the basics of GIT

posted 2 min read

Git is a powerful Source Control Management (SCM) tool, and when combined with GitHub, it becomes much more than just version tracking. In this article, I'll walk you through the essential Git commands I regularly use in my projects. Let's dive in!

What is a Repository?

A repository is essentially a folder on GitHub that holds your project files. But it's more than just a folder-it comes packed with features that help track changes, collaborate with others, and manage your code efficiently.

What is a Commit?

A commit is a snapshot of your code at a specific point in time. It lets you track changes and roll back to previous versions when needed.

What is a Branch?

A branch is like a sandbox. It lets you test or add new features without affecting the main codebase. By default, the primary branch is called main (earlier known as master). You can safely experiment on other branches and merge changes when ready.

Initializing a Git Repository

To create a new Git repository for your project:

Initialize a new Git repository

git init

(Optional) Rename the default branch to 'main'

git branch -m master main

Connect your local repo to a remote one (GitHub, GitLab, etc.)

git remote add origin <your-remote-url>

Verify the remote URL

git remote -v

Boom! Your local repository is ready and connected to the remote one.

Add README.md and .gitignore

README.md

This is the face of your project on GitHub. It's a Markdown file that can include formatted text, links, images, badges, and more to explain your project.

.gitignore

This file tells Git which files/folders to ignore. You should add files like:

  • IDE config files
  • Environment variables
  • Secret keys
  • Build directories

This prevents accidentally pushing sensitive or unnecessary files to your repo.

Staging and Committing Changes

After creating your files, it's time to track and commit them:

Add specific files

git add <filename> <another-filename>

Or add all changes

git add .

Commit with a message

git commit -m "Initial commit with README and .gitignore"

A commit creates a restore point in your project. Make sure your commit messages are meaningful!

Pushing to Remote

Your code is still local. To push it to your remote repository:

git push origin main

If you're working on a different branch, just replace main with your branch name.

Wrapping Up

That's it! You've now initialized a Git repo, tracked changes, and pushed your code to GitHub. This is just the beginning. Git has a lot more to offer-branching strategies, rebasing, stashing, cherry-picking, and so on.

But we'll cover all that in the next article. Until then, happy coding!

Stay Curious. Adios

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Nicely written, The examples are easy to follow, and the explanations make it less intimidating for beginners. Looking forward to more content like this!✨

Well said, simple, and straight to the point! Thanks for the info!

More Posts

Rewinding Time in Git: Exploring Commits & Branching

Hiruthic Sha - Jun 27

Git and GitHub for Python Developers A Comprehensive Guide

Tejas Vaij - Apr 7, 2024

Git Under the Hood

István Döbrentei - Jun 28

The Ultimate Guide to Mastering Git Commands: Everything You Need to Know

Hanzla Baig Dev - Feb 21

Mastering Collaborative Coding: A Deep Dive into Git Branching and Merging for Teams

Aditya Pratap Bhuyan - Jun 24
chevron_left