Git, GitHub & GitLab Tutorial for Beginners | DevOps Day 04

Learn Git, GitHub, and GitLab with practical commands, branch management, merge conflicts, SSH authentication, push, pull, CI/CD, and real-world
Github

DevOps Day 04: Git, GitHub ও GitLab – Complete Bangla Tutorial

আপনি যদি Full Stack Developer বা DevOps Engineer হতে চান, তাহলে Git, GitHub এবং GitLab সম্পর্কে পরিষ্কার ধারণা থাকা খুবই জরুরি। Software development-এ code লেখা যেমন গুরুত্বপূর্ণ, তেমনি code-এর changes track করা, team-এর সঙ্গে কাজ করা এবং application-এর updated version manage করাও গুরুত্বপূর্ণ।

এই কাজগুলো সহজ করার জন্য developers Git, GitHub এবং GitLab ব্যবহার করেন। এই tutorial-এ আমরা একদম basic থেকে Git installation, configuration, Git commands, branch, merge, GitHub repository, GitLab repository, SSH authentication, merge conflict এবং CI/CD-এর basic ধারণা শিখব।

এই article-এর প্রতিটি section-এ practical command এবং example দেওয়া হয়েছে, যাতে আপনি নিজের Windows computer-এ PowerShell, Git Bash অথবা Windows Terminal ব্যবহার করে practice করতে পারেন।

Git কী? (What Is Git?)

Git হলো একটি free এবং open-source Distributed Version Control System (DVCS)। সহজ ভাষায়, Git আপনার project-এর file এবং code-এর পরিবর্তনগুলো track করে রাখে। ফলে কোন সময়ে কী পরিবর্তন করেছেন, কে পরিবর্তন করেছে এবং আগের version-এ কী ছিল, সেগুলো জানা যায়।

ধরুন, আপনি React দিয়ে একটি website তৈরি করছেন। প্রথমে Header component তৈরি করলেন, পরে Navbar update করলেন এবং তারপর Login page যোগ করলেন। কোনো update করার পর application-এ সমস্যা হলে Git-এর সাহায্যে আগের code history দেখতে এবং প্রয়োজন অনুযায়ী পরিবর্তন revert করতে পারবেন।

Git কেন ব্যবহার করা হয়?

  • Version Control: Project-এর প্রতিটি গুরুত্বপূর্ণ change-এর history রাখা যায়।
  • Code Tracking: কোন file-এ কী পরিবর্তন হয়েছে তা দেখা যায়।
  • Branching: Main code পরিবর্তন না করে নতুন feature develop করা যায়।
  • Collaboration: একাধিক developer একই project-এ কাজ করতে পারেন।
  • Rollback: প্রয়োজন অনুযায়ী আগের version-এ ফিরে যাওয়া যায়।
  • Code Review: Team-এর অন্য সদস্যের code review করা যায়।
  • DevOps Workflow: CI/CD pipeline এবং deployment workflow তৈরি করতে Git ব্যবহার করা হয়।

GitHub কী? (What Is GitHub?)

GitHub হলো Git repository hosting এবং collaboration platform। Git আপনার computer-এ code-এর version history manage করে, আর GitHub সেই repository online-এ host করতে এবং অন্যদের সঙ্গে share করতে সাহায্য করে।

GitHub ব্যবহার করে আপনি নিজের project online-এ রাখতে পারবেন, অন্য developer-দের সঙ্গে collaborate করতে পারবেন এবং Pull Request-এর মাধ্যমে code review করতে পারবেন। GitHub Actions ব্যবহার করে automated testing, build এবং deployment workflow-ও তৈরি করা যায়।

GitHub-এর গুরুত্বপূর্ণ Features

  • Repository: Project files এবং Git history online-এ সংরক্ষণ করা।
  • Pull Request (PR): Code changes review এবং merge করার জন্য proposal তৈরি করা।
  • Issues: Bug, task এবং feature request track করা।
  • GitHub Actions: CI/CD এবং automation workflow তৈরি করা।
  • Code Review: Merge করার আগে code পরীক্ষা করা।
  • Collaboration: Team member-দের সঙ্গে একই project-এ কাজ করা।

GitLab কী? (What Is GitLab?)

GitLab হলো একটি Git repository hosting এবং DevSecOps platform। GitLab-এর মাধ্যমে source code manage, code review, CI/CD pipeline, issue tracking এবং software delivery workflow পরিচালনা করা যায়।

GitLab.com ব্যবহার করে online-এ repository তৈরি করতে পারবেন। আবার organization-এর প্রয়োজন অনুযায়ী নিজের server বা infrastructure-এ GitLab self-managed version চালানোও সম্ভব।

GitLab-এর গুরুত্বপূর্ণ Features

  • Git Repository: Source code এবং commit history manage করা।
  • Merge Request (MR): Code review এবং branch merge করার proposal তৈরি করা।
  • GitLab CI/CD: Automated testing, build এবং deployment করা।
  • Issue Tracking: Bug এবং development task manage করা।
  • Security Testing: Supported security scanning tools development workflow-এ যুক্ত করা।
  • Self-Managed Hosting: নিজের infrastructure-এ GitLab পরিচালনা করা।

Git vs GitHub vs GitLab – Difference কী?

অনেক beginner মনে করেন Git, GitHub এবং GitLab একই জিনিস। আসলে এদের কাজ আলাদা। Git হলো version control tool, আর GitHub ও GitLab হলো Git repository hosting এবং collaboration platform।

Feature Git GitHub GitLab
Type Version Control System Git Hosting Platform DevSecOps Platform
Local Repository Yes Git-এর মাধ্যমে ব্যবহার করা হয় Git-এর মাধ্যমে ব্যবহার করা হয়
Online Repository Hosting নিজে থেকে hosting service নয় Yes Yes
Code Review Built-in hosting review interface নেই Pull Request Merge Request
CI/CD Built-in CI/CD platform নয় GitHub Actions GitLab CI/CD
Self-Managed Option নিজস্ব server-এ Git repository host করা যায় GitHub Enterprise Server available GitLab Self-Managed available
Primary Use Code changes track করা Code collaboration এবং hosting Repository এবং integrated DevSecOps workflow

সহজভাবে মনে রাখুন: Git হলো tool, আর GitHub ও GitLab হলো platform। আপনি Git-এর command ব্যবহার করে GitHub, GitLab অথবা অন্য Git-compatible hosting service-এর সঙ্গে কাজ করতে পারবেন।

Step 1: Windows-এ Git Install করুন

প্রথমে আপনার computer-এ Git install করতে হবে। আপনি যেহেতু Windows ব্যবহার করছেন, তাই Git for Windows install করতে পারেন।

Download Git for Windows

  1. উপরের official website-এ প্রবেশ করুন।
  2. Windows-এর জন্য Git installer download করুন।
  3. Downloaded installer run করুন।
  4. Installation wizard-এর নির্দেশনা অনুসরণ করুন।
  5. Installation complete হলে PowerShell অথবা Git Bash open করুন।
  6. Git install হয়েছে কি না check করুন।

Git version check করার command:

git --version

Example output:

git version 2.x.x.windows.x

আপনার computer-এ version number আলাদা হতে পারে। যদি git is not recognized error আসে, তাহলে terminal বন্ধ করে আবার open করুন। সমস্যা থাকলে Git-এর PATH configuration check করুন।

Step 2: Git Username ও Email Configure করুন

Git commit তৈরি করার সময় author-এর name এবং email ব্যবহার করে। তাই প্রথমবার Git ব্যবহার করার আগে username ও email configure করা উচিত।

Username configure করুন:

git config --global user.name "Your Name"

Email configure করুন:

git config --global user.email "you@example.com"

এখানে Your Name এবং you@example.com-এর জায়গায় নিজের তথ্য ব্যবহার করবেন। GitHub বা GitLab account-এর সঙ্গে commit associate করতে চাইলে সেই account-এর উপযুক্ত email ব্যবহার করুন।

Git Configuration Check করার Command

git config --global --list

Username এবং email আলাদাভাবে check করতে পারবেন:

git config --global user.name
git config --global user.email

Default Branch Name main সেট করুন

বর্তমানে অনেক project-এ primary branch হিসেবে main ব্যবহার করা হয়। নতুন repository তৈরি করার সময় এই branch name default করতে চাইলে command দিন:

git config --global init.defaultBranch main

এই setting নতুন repository-তে প্রযোজ্য হবে। আগে থেকে তৈরি repository-এর branch name এটি স্বয়ংক্রিয়ভাবে পরিবর্তন করে না।

Step 3: Git-এর Working Directory, Staging Area ও Repository বুঝুন

Git-এর command ভালোভাবে শেখার জন্য এর basic workflow বুঝতে হবে। Git-এ file change সাধারণত কয়েকটি ধাপের মধ্য দিয়ে commit হয়।

1. Working Directory

এটি হলো আপনার project folder, যেখানে আপনি file create, edit অথবা delete করেন।

2. Staging Area

পরবর্তী commit-এ কোন পরিবর্তনগুলো অন্তর্ভুক্ত করবেন, সেগুলো staging area-তে রাখেন। এর জন্য git add command ব্যবহার করা হয়।

3. Local Repository

Commit তৈরি করার পর তার history আপনার local Git repository-তে সংরক্ষিত হয়।

4. Remote Repository

Remote repository হলো GitHub বা GitLab-এর মতো hosting service-এ থাকা repository। Local commit online-এ পাঠানোর জন্য সাধারণত git push ব্যবহার করা হয়।

Working Directory
        |
        | git add
        v
Staging Area
        |
        | git commit
        v
Local Repository
        |
        | git push
        v
Remote Repository

Remote থেকে update download করার জন্য git fetch এবং git pull ব্যবহার করা হয়। এদের কাজ এক নয়, তাই পরে আমরা বিস্তারিত দেখব।

Step 4: প্রথম Git Repository তৈরি করুন

এবার practical কাজ শুরু করা যাক। প্রথমে একটি project folder তৈরি করে সেটিকে Git repository হিসেবে initialize করব।

PowerShell-এ নিচের command দিন:

mkdir git-practice
cd git-practice
git init

এখানে:

  • mkdir git-practice একটি নতুন folder তৈরি করে।
  • cd git-practice folder-এর ভিতরে প্রবেশ করে।
  • git init folder-টিকে Git repository হিসেবে initialize করে।

README File তৈরি করুন

PowerShell-এ README file তৈরি করতে পারেন:

Set-Content README.md "# Git Practice"

এবার repository status check করুন:

git status

Git নতুন file-টিকে untracked হিসেবে দেখাবে। Untracked মানে Git এখনো file-টির version history track করা শুরু করেনি।

Step 5: git add, git commit ও git status শিখুন

এই তিনটি command Git-এর সবচেয়ে গুরুত্বপূর্ণ basic command-এর মধ্যে পড়ে।

1. git status – Repository Status Check

বর্তমান project-এ কোন file modified, staged বা untracked আছে তা দেখতে ব্যবহার করুন:

git status

2. git add – Changes Stage করা

একটি নির্দিষ্ট file stage করতে:

git add README.md

বর্তমান directory-র পরিবর্তনগুলো stage করতে:

git add .

Commit করার আগে staged changes review করুন:

git status
git diff --cached

3. git commit – Changes Save করা

Staging area-তে থাকা changes local repository-তে record করতে commit তৈরি করুন:

git commit -m "Add README file"

এখানে -m দিয়ে commit message দেওয়া হয়। Message এমন হওয়া উচিত, যাতে বোঝা যায় commit-এ কী পরিবর্তন করা হয়েছে।

4. git log – Commit History দেখা

Project-এর আগের commit দেখতে ব্যবহার করুন:

git log
git log --oneline
git log --oneline --graph --all

git log --oneline সংক্ষিপ্ত commit history দেখায়। আর --graph --all branch ও commit history-এর সম্পর্ক বুঝতে সাহায্য করে।

Step 6: গুরুত্বপূর্ণ Git Commands ও তাদের কাজ

নিচের table-এ daily development এবং DevOps workflow-এ ব্যবহৃত গুরুত্বপূর্ণ Git command-গুলো দেওয়া হলো।

Git Command কাজ
git init নতুন local repository তৈরি করে
git clone URL Remote repository local computer-এ copy করে
git status Repository-এর বর্তমান অবস্থা দেখায়
git add file.txt নির্দিষ্ট file stage করে
git add . বর্তমান directory-র changes stage করে
git commit -m "message" Staged changes-এর commit তৈরি করে
git diff Unstaged changes দেখায়
git diff --cached Staged changes দেখায়
git log --oneline সংক্ষিপ্ত commit history দেখায়
git branch Local branch list দেখায়
git switch main main branch-এ switch করে
git switch -c feature নতুন branch তৈরি করে switch করে
git merge feature বর্তমান branch-এ feature branch merge করে
git fetch Remote updates download করে, কিন্তু current branch-এ integrate করে না
git pull Remote updates fetch করে current branch-এ integrate করে
git push Local commits remote repository-তে পাঠায়
git remote -v Configured remote URL দেখায়
git stash Supported working changes সাময়িকভাবে save করে
git restore file.txt File-এর working-tree content restore করে
git revert HASH আগের commit-এর পরিবর্তন উল্টে নতুন commit তৈরি করে

Step 7: Git Branch কী এবং কেন ব্যবহার করা হয়?

Git branch হলো development-এর আলাদা একটি line। নতুন feature তৈরি, bug fix অথবা experiment করার সময় branch ব্যবহার করলে main code-কে সরাসরি পরিবর্তন না করেও কাজ করা যায়।

ধরুন, আপনার React project-এর main branch-এ working application আছে। এখন আপনি নতুন Login page তৈরি করতে চান। আপনি feature-login নামে branch তৈরি করে সেখানে কাজ করতে পারবেন।

Branch List দেখুন

git branch

Local এবং remote-tracking branch দেখতে:

git branch -a

নতুন Branch তৈরি করুন

git branch feature-login

এই command branch তৈরি করে, কিন্তু branch-এ switch করে না।

Branch-এ Switch করুন

git switch feature-login

একসঙ্গে Branch তৈরি ও Switch করুন

git switch -c feature-login

Merge হওয়া Branch Delete করুন

git branch -d feature-login

-d সাধারণত এমন branch delete করতে বাধা দেয়, যার changes এখনো merge হয়নি। Force delete করার আগে নিশ্চিত হোন যে branch-টির কোনো গুরুত্বপূর্ণ কাজ হারিয়ে যাবে না।

Step 8: Git Merge কী?

Feature branch-এর কাজ শেষ হলে সেটিকে main branch-এ integrate করতে merge ব্যবহার করা হয়।

উদাহরণ:

git switch main
git merge feature-login

প্রথম command main branch-এ switch করে। দ্বিতীয় command feature-login-এর changes বর্তমান branch-এ merge করে।

Git Merge vs Git Rebase

বিষয় Merge Rebase
কাজ দুটি branch-এর history integrate করে অন্য base-এর ওপর commit replay করে
History প্রয়োজনে merge commit তৈরি করে Replayed commit-এর নতুন ID তৈরি হয়
ব্যবহার Feature branch integrate করা Feature branch-কে নতুন base-এর ওপর update করা
সতর্কতা Merge conflict হতে পারে Shared commit rebase করলে team-এর history সমস্যা হতে পারে

Feature branch-কে remote main branch-এর সর্বশেষ commit-এর ওপর rebase করার example:

git switch feature-login
git fetch origin
git rebase origin/main

এখানে origin/main হলো remote main branch-এর tracking reference। Rebase চলাকালে conflict হলে সেটি resolve করে operation continue করতে হবে। Shared branch-এ rebase করার আগে team workflow বুঝে নিন।

Step 9: Git Remote কী?

Remote হলো অন্য একটি repository-এর reference। সাধারণত GitHub বা GitLab-এর online repository-কে local project-এর সঙ্গে connect করতে remote ব্যবহার করা হয়। প্রচলিত remote name হলো origin।

Remote URL Check করুন

git remote -v

নতুন Remote Add করুন

git remote add origin https://github.com/USERNAME/REPOSITORY.git

এখানে USERNAME এবং REPOSITORY পরিবর্তন করে আপনার actual repository URL ব্যবহার করবেন।

Existing Remote URL Change করুন

যদি origin আগে থেকেই থাকে, তাহলে আবার add করার প্রয়োজন নেই। URL update করতে ব্যবহার করুন:

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

Remote Remove করুন

git remote remove origin

এই command শুধু local Git configuration থেকে remote reference remove করে। GitHub বা GitLab-এর online repository delete করে না।

Step 10: GitHub-এ Project Upload করুন

এখন local Git project-কে GitHub-এর সঙ্গে connect করে online-এ push করব।

Method 1: Existing Local Repository Push করা

  1. GitHub account-এ login করুন।
  2. New repository তৈরি করুন।
  3. Repository name দিন।
  4. Local repository-তে আগে থেকেই commit থাকলে অপ্রয়োজনীয় history conflict এড়াতে remote repository-তে আলাদা README বা commit তৈরি না করাই সহজ হতে পারে।
  5. Repository URL copy করুন।
  6. Local project-এর সঙ্গে remote connect করুন।

Local repository-তে commit তৈরি করা থাকলে command দিন:

git remote add origin https://github.com/USERNAME/REPOSITORY.git
git branch -M main
git push -u origin main

যদি origin already exists error আসে, তাহলে প্রথম command আবার চালাবেন না। আগে git remote -v দিয়ে check করুন। প্রয়োজন হলে git remote set-url ব্যবহার করুন।

-u option local branch-এর সঙ্গে upstream tracking branch সেট করে। এরপর অনেক ক্ষেত্রে শুধু নিচের command ব্যবহার করলেই push হবে:

git push

Method 2: GitHub থেকে Project Clone করা

GitHub-এ আগে থেকেই repository থাকলে local computer-এ clone করতে ব্যবহার করুন:

git clone https://github.com/USERNAME/REPOSITORY.git

Project directory-তে প্রবেশ করুন:

cd REPOSITORY

Repository status এবং remote check করুন:

git status
git remote -v

Step 11: GitLab-এ Project Upload করুন

GitLab-এও Git-এর একই basic command ব্যবহার করা হয়। পার্থক্য মূলত platform-এর interface, collaboration features এবং CI/CD integration-এ।

GitLab Repository-তে Push করুন

  1. GitLab account-এ login করুন।
  2. একটি নতুন blank project তৈরি করুন।
  3. HTTPS অথবা SSH repository URL copy করুন।
  4. Local project-এর সঙ্গে GitLab remote connect করুন।
  5. আপনার branch push করুন।

নতুন GitLab remote হিসেবে origin ব্যবহার করলে:

git remote add origin https://gitlab.com/USERNAME/REPOSITORY.git
git branch -M main
git push -u origin main

যদি একই local project-এর origin ইতোমধ্যে GitHub-এর জন্য configure করা থাকে, তাহলে সেটি replace না করে আলাদা remote name ব্যবহার করতে পারেন।

git remote add gitlab https://gitlab.com/USERNAME/REPOSITORY.git
git push -u gitlab main

এখন origin দিয়ে GitHub এবং gitlab দিয়ে GitLab-এ push করতে পারবেন। তবে দুই repository-এর history synchronized রাখার জন্য আপনার workflow পরিষ্কার থাকা জরুরি।

GitLab Repository Clone করুন

git clone https://gitlab.com/USERNAME/REPOSITORY.git

Step 12: git fetch vs git pull vs git push

Remote repository-এর সঙ্গে কাজ করার সময় এই তিনটি command খুব গুরুত্বপূর্ণ। এদের কাজ এক নয়।

Command কাজ কখন ব্যবহার করবেন?
git fetch Remote updates download করে, কিন্তু current branch-এ integrate করে না Update review করার আগে
git pull Remote updates fetch করে এবং current branch-এ integrate করে Local branch update করার সময়
git push Local commits remote branch-এ upload করে নিজের commit team-এর সঙ্গে share করার সময়

Remote Changes Fetch করুন

git fetch origin
git log --oneline HEAD..origin/main

দ্বিতীয় command current HEAD-এ নেই, কিন্তু origin/main-এ আছে এমন commit দেখায়, যদি remote-tracking branch উপস্থিত থাকে।

Latest Changes Pull করুন

git pull origin main

এই command remote main branch থেকে changes fetch করে এবং current branch-এ integrate করে। Integration-এর পদ্ধতি Git configuration ও command options-এর ওপর নির্ভর করতে পারে।

নিজের Changes Push করুন

git push origin main

আপনার permission থাকলে এবং remote branch update গ্রহণ করলে local commit-গুলো remote main branch-এ পাঠানো হবে।

Step 13: Pull Request (PR) ও Merge Request (MR)

GitHub-এ code review-এর জন্য Pull Request (PR) ব্যবহার করা হয়। GitLab-এ একই ধরনের কাজের জন্য Merge Request (MR) ব্যবহৃত হয়।

এগুলোর মাধ্যমে developer নতুন code review-এর জন্য প্রস্তাব করতে পারেন। Team member-রা code review, comment এবং automated test check করার পর changes merge করতে পারেন।

Typical Team Workflow

  1. Main branch-এর latest changes update করুন।
  2. নতুন feature branch তৈরি করুন।
  3. Code লিখুন এবং local test করুন।
  4. Changes stage ও commit করুন।
  5. Feature branch remote repository-তে push করুন।
  6. GitHub-এ Pull Request অথবা GitLab-এ Merge Request তৈরি করুন।
  7. Review feedback এবং test failure থাকলে fix করুন।
  8. Team-এর review ও branch protection rules অনুযায়ী merge করুন।

Example commands:

git switch main
git pull origin main
git switch -c feature-navbar

# Code changes করুন এবং test করুন
git add .
git commit -m "Add navigation component"
git push -u origin feature-navbar

এরপর browser-এ repository open করে Pull Request অথবা Merge Request তৈরি করুন।

Step 14: GitHub ও GitLab-এর জন্য SSH Setup

SSH authentication ব্যবহার করলে supported Git hosting service-এর সঙ্গে secure connection তৈরি করা যায়। আপনার SSH public key account-এ add করা থাকলে প্রতিবার Git operation-এর সময় account password দিতে হয় না।

1. Existing SSH Key Check করুন

Windows PowerShell-এ run করুন:

Get-ChildItem ~/.ssh

আগে থেকেই suitable SSH key থাকলে সেটি ব্যবহার করা যেতে পারে। Existing private key অন্য service-এ ব্যবহৃত হচ্ছে কি না check না করে সেটি overwrite করবেন না।

2. SSH Key Generate করুন

ssh-keygen -t ed25519 -C "you@example.com"

Command চালানোর পর file location এবং passphrase-এর prompt আসতে পারে। Prompt অনুসরণ করে setup সম্পন্ন করুন।

3. SSH Agent-এ Key Add করুন

Git Bash-এর compatible environment-এ নিচের command ব্যবহার করা যায়:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

PowerShell-এ Windows OpenSSH ব্যবহার করলে আপনার environment অনুযায়ী Authentication Agent service configure করতে হতে পারে।

4. Public Key Copy করুন

PowerShell-এ public key দেখতে:

Get-Content ~/.ssh/id_ed25519.pub

সম্পূর্ণ public key copy করে GitHub বা GitLab account-এর SSH key settings-এ add করুন।

Warning!
Private key কখনো অন্য কাউকে share করবেন না। সাধারণত id_ed25519 হলো private key এবং id_ed25519.pub হলো public key। Hosting account-এ শুধু public key add করবেন।

5. SSH Connection Test করুন

GitHub-এর জন্য:

ssh -T git@github.com

GitLab.com-এর জন্য:

ssh -T git@gitlab.com

Connection সফল হলে hosting service সাধারণত authentication সফল হওয়ার message দেখাবে। Self-managed GitLab ব্যবহার করলে hostname পরিবর্তন করতে হবে।

6. Remote URL SSH-তে পরিবর্তন করুন

GitHub example:

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

GitLab.com example:

git remote set-url origin git@gitlab.com:USERNAME/REPOSITORY.git

Repository-এর actual SSH URL ব্যবহার করবেন। SSH key সঠিকভাবে configured থাকতে হবে এবং আপনার repository access permission থাকতে হবে।

Step 15: Git Merge Conflict কীভাবে Resolve করবেন?

দুটি branch-এর changes Git স্বয়ংক্রিয়ভাবে মিলিয়ে দিতে না পারলে merge conflict হতে পারে। সাধারণত একই file-এর একই অংশে আলাদা changes হলে এই সমস্যা দেখা দেয়।

Conflict Markers-এর Example

<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-login

এখানে HEAD অংশে current branch-এর content এবং separator-এর পর incoming changes দেখানো হয়েছে। File edit করে সঠিক content রাখতে হবে এবং conflict markers সরিয়ে ফেলতে হবে।

Merge Conflict Resolve করার Steps

  1. git status চালিয়ে conflicted file identify করুন।
  2. Code editor-এ conflicted file open করুন।
  3. দুই পক্ষের changes review করুন।
  4. সঠিক final content রেখে conflict markers remove করুন।
  5. Code test করুন।
  6. Resolved file stage করুন।
  7. Merge অথবা rebase operation সম্পন্ন করুন।

Merge conflict resolve করার পর:

git add .
git status
git commit

Rebase conflict হলে resolved changes stage করার পর:

git add .
git rebase --continue

Operation cancel করতে হলে যে operation চলছে, সেটির উপযুক্ত command ব্যবহার করুন:

git merge --abort

অথবা:

git rebase --abort

Merge এবং rebase একসঙ্গে abort করার command নয়। বর্তমানে কোন operation চলছে, সেটি বুঝে উপযুক্ত command ব্যবহার করবেন।

Step 16: Git-এ Changes Undo করার Commands

ভুল code change হয়ে গেলে Git দিয়ে changes restore অথবা reverse করা যায়। তবে কোন command ব্যবহার করবেন তা নির্ভর করে changes staged, unstaged, committed নাকি remote-এ shared হয়েছে তার ওপর।

Command কাজ সতর্কতা
git restore file.txt Working-tree file restore করে Uncommitted edit হারিয়ে যেতে পারে
git restore --staged file.txt Staging area থেকে file সরায় Working-tree edit থাকে
git revert HASH আগের commit-এর পরিবর্তন reverse করে নতুন commit তৈরি করে Shared history-তে অনেক ক্ষেত্রে উপযোগী
git reset --soft HEAD~1 বর্তমান branch এক commit পিছিয়ে দেয় Changes staged থাকে
git reset --mixed HEAD~1 বর্তমান branch এক commit পিছিয়ে দেয় Changes unstaged working tree-তে থাকে

Warning!
git reset --hard-সহ destructive command uncommitted changes হারিয়ে দিতে পারে। Shared branch-এর history rewrite করলেও team-এর সমস্যা হতে পারে। Command চালানোর আগে git status এবং commit history check করুন।

Step 17: Git Stash – Changes Temporarily Save করুন

কোনো changes commit না করেই অন্য branch-এ switch করতে হলে Git stash কাজে লাগে। এটি supported working changes সাময়িকভাবে save করে রাখে।

Changes Stash করুন

git stash

Untracked file-সহ stash করতে:

git stash -u

Stash List দেখুন

git stash list

Stash Apply করুন

git stash apply

Latest stash apply করে সফল হলে stash entry remove করতে:

git stash pop

Stash apply করার সময় conflict হতে পারে। তাই apply করার পর file ও repository status check করুন।

Step 18: Common Git Errors এবং Solutions

Error 1: remote origin already exists

এই error-এর অর্থ হলো আপনার local repository-তে origin নামে remote আগে থেকেই configure করা আছে।

প্রথমে check করুন:

git remote -v

Existing URL পরিবর্তন করতে:

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

অথবা GitHub ও GitLab-এর জন্য আলাদা remote রাখতে চাইলে নতুন নাম ব্যবহার করুন।

Error 2: Permission denied when pushing

সঠিক account দিয়ে authentication না হওয়া অথবা repository-তে write permission না থাকলে এই error হতে পারে।

Remote URL check করুন:

git remote -v

তারপর নিচের বিষয়গুলো verify করুন:

  • সঠিক GitHub বা GitLab account ব্যবহার করছেন কি না।
  • Repository-তে push করার permission আছে কি না।
  • SSH key অথবা HTTPS authentication সঠিকভাবে configure করা আছে কি না।
  • Remote URL সঠিক repository-কে নির্দেশ করছে কি না।
  • Team repository হলে আপনার account-এর access role যথেষ্ট কি না।

মনে রাখবেন, Git username ও email পরিবর্তন করলে push authentication account পরিবর্তন হয় না। Commit identity এবং authentication আলাদা বিষয়।

Error 3: failed to push some refs

Remote branch-এ আপনার local branch-এ না থাকা commit থাকলে অথবা server update reject করলে এই error হতে পারে।

প্রথমে repository status এবং history check করুন:

git status
git fetch origin
git log --oneline --graph --decorate --all

Remote-এ নতুন changes থাকলে team workflow অনুযায়ী সেগুলো integrate করে আবার push করুন। কারণ না বুঝে force push করবেন না।

Error 4: fatal: not a git repository

আপনি যে directory-তে command চালাচ্ছেন, সেখানে Git repository নেই অথবা Git repository-এর ভিতরে প্রবেশ করেননি।

বর্তমান directory check করুন:

pwd
git status

PowerShell-এ location দেখতে:

Get-Location

সঠিক project directory-তে প্রবেশ করুন। নতুন project হলে ইচ্ছাকৃতভাবে git init ব্যবহার করতে পারেন।

Error 5: nothing to commit, working tree clean

এই message দেখালে Git-এর কাছে বর্তমানে commit করার মতো staged changes নেই।

git status
git diff
git diff --cached

File edit করে save করেছেন কি না check করুন। নতুন changes commit করতে চাইলে আগে সংশ্লিষ্ট file stage করুন।

Step 19: GitHub Actions vs GitLab CI/CD

GitHub এবং GitLab দুটোতেই automated testing, build এবং deployment করা যায়। তবে configuration file এবং pipeline syntax আলাদা।

Feature GitHub Actions GitLab CI/CD
Configuration File .github/workflows/ folder-এর YAML file Repository root-এ .gitlab-ci.yml
Structure Workflow, jobs এবং steps Pipeline, stages এবং jobs
Runner GitHub-hosted বা self-hosted runner GitLab-hosted বা self-managed runner, setup অনুযায়ী
Triggers Push, Pull Request, schedule এবং supported events Push, Merge Request, schedule এবং supported pipeline rules
Purpose Repository workflow ও delivery automation Software lifecycle ও delivery automation

GitHub Actions-এর Example

Project-এ .github/workflows/ci.yml নামে file তৈরি করুন:

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        run: npm test

এই example একটি compatible Node.js project-এর জন্য। Project-এ lockfile এবং npm test script থাকতে হবে। আপনার application অনুযায়ী Node.js version ও command পরিবর্তন করুন।

GitLab CI/CD-এর Example

Repository root-এ .gitlab-ci.yml file তৈরি করুন:

stages:
  - test

node_tests:
  stage: test
  image: node:22
  script:
    - npm ci
    - npm test

এই pipeline Node.js container image ব্যবহার করে dependency install ও test চালায়। Job execute করার জন্য উপযুক্ত GitLab Runner configuration থাকতে হবে।

Info!
এই YAML configuration-গুলো starter example। Production project-এ ব্যবহারের আগে runtime, dependency, test command, security এবং runner configuration অনুযায়ী update করবেন।

Step 20: Real Project-এ Recommended Git Workflow

Team project-এ একটি consistent Git workflow follow করলে code review সহজ হয় এবং একই codebase-এ একাধিক developer কাজ করার সময় সমস্যা কমে।

  1. Remote repository clone করুন।
  2. Main branch-এ switch করুন।
  3. Latest changes fetch ও integrate করুন।
  4. Feature branch তৈরি করুন।
  5. Code লিখুন এবং local test করুন।
  6. git diff দিয়ে changes review করুন।
  7. Relevant file stage ও commit করুন।
  8. Feature branch remote-এ push করুন।
  9. Pull Request অথবা Merge Request তৈরি করুন।
  10. Review feedback এবং automated test failure fix করুন।
  11. Team-এর rules অনুযায়ী merge করুন।

Example command sequence:

git clone https://github.com/USERNAME/REPOSITORY.git
cd REPOSITORY

git switch main
git pull --ff-only origin main

git switch -c feature-profile

# Code changes করুন এবং test করুন
git status
git diff

git add .
git commit -m "Add profile feature"
git push -u origin feature-profile

--ff-only ব্যবহার করলে local branch fast-forward করা সম্ভব না হলে pull fail করবে, merge commit তৈরি করবে না। আপনার team অন্য workflow ব্যবহার করলে সেটি follow করুন।

Hands-on Practice: DevOps Day 04 Tasks

এখন পর্যন্ত শেখা বিষয়গুলো practice করার জন্য নিচের task-গুলো complete করুন।

  1. Windows-এ Git install করুন এবং git --version দিয়ে verify করুন।
  2. Git username ও email configure করুন।
  3. devops-day-04 নামে folder তৈরি করুন।
  4. Folder-টিকে Git repository হিসেবে initialize করুন।
  5. README.md file তৈরি করুন।
  6. File stage করে প্রথম commit তৈরি করুন।
  7. feature-practice নামে branch তৈরি করুন।
  8. Branch-এ README update করে নতুন commit তৈরি করুন।
  9. Feature branch-কে main-এ merge করুন।
  10. GitHub-এ repository তৈরি করে local project push করুন।
  11. GitLab-এ আলাদা repository তৈরি করে second remote হিসেবে configure করার practice করুন।
  12. Clone, fetch, pull এবং push command practice করুন।
  13. SSH key তৈরি করে GitHub অথবা GitLab account-এ configure করুন।
  14. Feature branch থেকে Pull Request অথবা Merge Request তৈরি করুন।
  15. git log --oneline --graph --all দিয়ে commit history review করুন।

Git Commands Cheat Sheet

Task Command
Git Version Check git --version
Username Set git config --global user.name "Your Name"
Email Set git config --global user.email "you@example.com"
Repository Initialize git init
Repository Clone git clone URL
Status Check git status
File Stage git add file.txt
Commit Changes git commit -m "message"
Commit History git log --oneline
New Branch git switch -c feature
Switch Branch git switch main
Merge Branch git merge feature
Remote List git remote -v
Fetch Updates git fetch origin
Pull Changes git pull
Push Commits git push
Stash Changes git stash
Restore File git restore file.txt
Reverse Commit git revert HASH

Frequently Asked Questions (FAQ)

Git এবং GitHub কি একই জিনিস?

না। Git হলো Distributed Version Control System। GitHub হলো online platform, যেখানে Git repository host করা এবং Pull Request, Issues ও GitHub Actions-এর মতো collaboration feature ব্যবহার করা যায়।

GitHub ও GitLab-এর মধ্যে পার্থক্য কী?

দুটো platform-ই Git repository hosting এবং collaboration support করে। GitHub-এ Pull Request ও GitHub Actions ব্যবহৃত হয়। GitLab-এ Merge Request এবং GitLab CI/CD ব্যবহৃত হয়। কোনটি ভালো হবে তা আপনার project, team workflow, integration এবং hosting requirement-এর ওপর নির্ভর করে।

GitHub বা GitLab ছাড়া Git ব্যবহার করা যাবে?

হ্যাঁ। Git local computer-এ কাজ করে। Online hosting ছাড়াই repository initialize, commit, branch তৈরি এবং commit history review করতে পারবেন।

git fetch এবং git pull-এর পার্থক্য কী?

git fetch remote update download করে কিন্তু current branch-এ integrate করে না। git pull fetch করার পর current branch-এ changes integrate করে।

Pull Request এবং Merge Request কি একই?

দুটোর উদ্দেশ্য প্রায় একই। GitHub-এ Pull Request এবং GitLab-এ Merge Request বলা হয়। এগুলোর মাধ্যমে code review, discussion, automated check এবং branch merge করা যায়।

GitHub ও GitLab শেখার আগে Git শেখা জরুরি?

হ্যাঁ। Git command, commit, branch, merge, remote এবং conflict resolution বুঝলে GitHub ও GitLab ব্যবহার করা অনেক সহজ হয়।

Git username পরিবর্তন করলে push permission error ঠিক হবে?

সাধারণত না। Git username ও email commit author identify করে। Push authentication নির্ভর করে HTTPS credentials অথবা SSH key এবং repository permission-এর ওপর।

একই project GitHub ও GitLab-এ push করা যায়?

হ্যাঁ। Local repository-তে আলাদা remote configure করে দুই platform-এ branch push করতে পারবেন। তবে দুই repository-এর history synchronized রাখার জন্য পরিষ্কার workflow অনুসরণ করা উচিত।

Conclusion

Git, GitHub এবং GitLab হলো modern software development ও DevOps-এর গুরুত্বপূর্ণ tools। Git-এর মাধ্যমে code changes track এবং version history manage করা যায়। GitHub ও GitLab repository hosting, code review, team collaboration এবং CI/CD automation-এর সুবিধা দেয়।

প্রথমে git init, git add, git commit, git branch, git switch, git merge, git fetch, git pull এবং git push command ভালোভাবে practice করুন।

এরপর feature branch, merge conflict, SSH authentication এবং Pull Request বা Merge Request নিয়ে practical কাজ করুন। এই skills আপনাকে team-based development এবং পরবর্তী DevOps topics যেমন CI/CD, Docker ও automated deployment শেখার জন্য প্রস্তুত করবে।

Official Resources

Related Posts

About the author

Daud
Hey! I'm Daud, Currently Working in IT Company BD. I always like to learn something new and teach others.

Post a Comment

To avoid SPAM, all comments will be moderated before being displayed.
Don't share any personal or sensitive information.