What is Git? Learn Version Control with a Hands-On Tutorial

What is Git? Learn Version Control with a Hands-On Tutorial

 Git is a distributed version control system used by developers to track changes in source code during software development. It allows multiple people to collaborate, manage code versions, and revert to previous stages if needed.

What Git Does

  • Tracks changes in your files

  • Allows collaboration on code with others

  • Maintains a history of code updates

  • Helps manage different versions (branches) of your project

Key Concepts

TermMeaning
Repository (repo)A project folder tracked by Git
CommitA snapshot of your code changes
BranchA separate line of development
MergeCombining changes from different branches
CloneCopying a repository to your local system
Push/PullUpload/download changes to/from remote repo (e.g., GitHub)

Git Installation

Step 1: Install Git

Step 2: Set up your identity

git config --global user.name "Your Name" git config --global user.email "your@email.com"

Sample Git Tutorial

Let’s go through a simple example using Git on your local machine.

Step 1: Create a Project Directory

mkdir demo-git-project cd demo-git-project

Step 2: Initialize Git

git init

This creates a hidden .git folder that tracks changes.

Step 3: Create a File

echo "Hello Git" > index.html

Step 4: Check File Status

git status

Step 5: Add File to Staging Area

git add index.html

Step 6: Commit the Change

git commit -m "Initial commit with index.html"

Step 7: Create a New Branch (Optional)

git branch feature-login git checkout feature-login # or shortcut: git checkout -b feature-login

Step 8: Make Changes and Commit

echo "Login feature" >> index.html git add index.html git commit -m "Add login feature"

Step 9: Merge Changes to Main

git checkout main git merge feature-login

Step 10: View Commit History

git log

Push to GitHub (Optional)

If you want to store your project on GitHub:

Step 1: Create a repo on GitHub

Step 2: Link local repo to GitHub

git remote add origin https://github.com/your-username/demo-git-project.git git push -u origin main

Summary

CommandDescription
git initStart a new Git repository
git statusCheck current repo state
git add <file>Stage file for commit
git commit -m "msg"Save a snapshot
git branchView branches
git checkout -b <name>Create and switch to a branch
git merge <branch>Merge another branch
git logView commit history
git pushSend code to remote repo
Souy Soeng

Souy Soeng

Hi there šŸ‘‹, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
šŸ‘Æ I’m looking to collaborate on open-source PHP & JavaScript projects
šŸ’¬ Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close