ত্রুটি“fatal: refusing to merge unrelated histories” appears when you try to merge or pull from a Git repository that has no common ancestor with your current branch. এটি প্রায়শই ঘটে যখন আপনি একটি README দিয়ে GitHub-এ একটি নতুন রেপো তৈরি করেন এবং একটি স্থানীয় প্রজেক্ট পুশ করার চেষ্টা করেন, অথবা যখন আপনি দুটি সম্পূর্ণ আলাদা সংগ্রহস্থল একত্রিত করেন। এখানে প্রতিটি দৃশ্যকল্প এবং এর সঠিক সমাধান।
📋 Table of Contents
- কেন এই ত্রুটি ঘটে
- দৃশ্যকল্প 1: README এর সাথে GitHub-এ রেপো তৈরি করা হয়েছে, স্থানীয় ইতিমধ্যেই প্রতিশ্রুতি দিয়েছে
- দৃশ্যকল্প 2: দুটি পৃথক ভাণ্ডার একত্রিত করা
- দৃশ্য 3: রেপো পুনরায় তৈরি করার পরে গিট টান
- The Right Way: Don't Create README on GitHub When You Have Local Code
- কখন –অনুমতি-সম্পর্কিত-ইতিহাস নিরাপদ?
- রিপোজিটরি সম্পর্ক পরীক্ষা করা হচ্ছে
- প্রায়শই জিজ্ঞাসিত প্রশ্ন
- উপসংহার
কেন এই ত্রুটি ঘটে
গিট কমিটের একটি চেইন এর মাধ্যমে ইতিহাস ট্র্যাক করে। যখন দুটি রেপো কখনই একটি সাধারণ প্রতিশ্রুতি (অসংলগ্ন ইতিহাস) ভাগ করেনি, তখন গিট 2.9 থেকে ডিফল্টভাবে সেগুলিকে একত্রিত করতে অস্বীকার করে — এটি ধরে নেওয়া হয় আপনি একটি ভুল করেছেন। ত্রুটিটি এমন দেখাচ্ছে:
$ git pull origin main
fatal: refusing to merge unrelated histories
অথবা যখন একত্রিত হয়:
$ git merge origin/main
fatal: refusing to merge unrelated histories
দৃশ্যকল্প 1: README এর সাথে GitHub-এ রেপো তৈরি করা হয়েছে, স্থানীয় ইতিমধ্যেই প্রতিশ্রুতি দিয়েছে
এটি সবচেয়ে সাধারণ কারণ। You initialized a GitHub repo with a README (or license), then try to push your local project. আপনার স্থানীয় প্রতিশ্রুতি আছে; গিটহাবের একটি প্রতিশ্রুতি রয়েছে (README) — কোনো শেয়ার করা ইতিহাস নেই।
# ✅ Option 1: Force push (destroys the README commit — simplest)
git push -u origin main --force
# WARNING: Only use if nobody else has cloned the repo
# and you don't mind losing the GitHub-created README
# ✅ Option 2: Allow merge (keeps both, creates merge commit)
git pull origin main --allow-unrelated-histories
# Resolve any conflicts (often just README vs your code)
git push origin main
# ✅ Option 3: Rebase (cleanest linear history)
git fetch origin
git rebase origin/main
# Resolve any conflicts
git push origin main
দৃশ্যকল্প 2: দুটি পৃথক ভাণ্ডার একত্রিত করা
আপনার দুটি সংগ্রহস্থল রয়েছে এবং একটিকে অন্যটিতে একত্রিত করতে চান (যেমন, একটি ফ্রন্টএন্ড রেপো এবং ব্যাকএন্ড রেপোকে মনোরেপোতে একত্রিত করা)।
# Add the second repo as a remote
git remote add other-repo https://github.com/user/other-repo.git
git fetch other-repo
# Merge allowing unrelated histories
git merge other-repo/main --allow-unrelated-histories
# All files from both repos are now in working tree
# Commit the merge
git commit -m "Merge other-repo into monorepo"
দ্বিতীয় রেপোর ফাইলগুলিকে একটি সাবডিরেক্টরিতে রাখতে (মনোরপোসের জন্য ক্লিনার):
# Better approach: move the second repo's files to a subdirectory first
git fetch other-repo
git checkout -b temp-branch other-repo/main
# Move all files to a subdirectory
mkdir backend
git ls-files | xargs -I{} git mv {} backend/{}
git commit -m "Move to backend/ subdirectory"
# Merge back into main
git checkout main
git merge temp-branch --allow-unrelated-histories
দৃশ্য 3: রেপো পুনরায় তৈরি করার পরে গিট টান
একটি দলের সদস্য দূরবর্তী সংগ্রহস্থল মুছে ফেলেছে এবং পুনরায় তৈরি করেছে, এর ইতিহাস মুছে দিয়েছে। আপনার স্থানীয় অনুলিপি নতুন রিমোট তা করে না।
# Check what happened
git log --oneline -5 # your local history
git log --oneline origin/main -5 # remote history
# If you want to keep your local history (usually correct)
git push origin main --force-with-lease
# --force-with-lease is safer than --force: fails if remote has
# commits you haven't fetched (prevents accidental overwriting)
# If you want the new remote's history (abandon local)
git fetch origin
git reset --hard origin/main
The Right Way: Don’t Create README on GitHub When You Have Local Code
আসল সমাধান হল প্রতিরোধ। যখন আপনার একটি বিদ্যমান স্থানীয় প্রকল্প থাকে:
# 1. Create repo on GitHub WITHOUT initializing (no README, no .gitignore)
# 2. Add remote and push
git init # if not already a repo
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/user/repo.git
git push -u origin main # no conflicts because remote is empty
কখন –অনুমতি-সম্পর্কিত-ইতিহাস নিরাপদ?
# Safe scenarios:
# - Initializing a project that was created with a README on GitHub
# - Combining unrelated repos intentionally into a monorepo
# - Recovering from an accidental repo recreation
# Unsafe scenarios:
# - You don't understand why the histories are unrelated
# - The remote was supposed to be a fork of your local (shouldn't be unrelated)
# - Two branches of the same project (investigate root cause instead)
git merge other-branch --allow-unrelated-histories
রিপোজিটরি সম্পর্ক পরীক্ষা করা হচ্ছে
# See if two commits share ancestry
git merge-base --is-ancestor commit1 commit2
echo $? # 0 = ancestor, 1 = not ancestor
# See all commit ancestry graph
git log --oneline --graph --all
# Find common ancestor (if any)
git merge-base branch1 branch2
প্রায়শই জিজ্ঞাসিত প্রশ্ন
প্রশ্নঃ –অনুমতি-অসংলগ্ন-ইতিহাস কি বিপজ্জনক?
A: It can create confusing history but isn’t destructive — you can always undo a merge. বড় ঝুঁকি হল এটি ব্যবহার করা যখন আপনার উচিত নয় (যখন repos সম্পর্কিত হওয়া উচিত এবং কিছু ভুল হয়েছে)। পতাকা ব্যবহার করার আগে কেন তারা সম্পর্কহীন তা বুঝে নিন।
প্রশ্ন: –অনুমতি-অসংলগ্ন-ইতিহাস এবং –ফোর্সের মধ্যে পার্থক্য কী?
A: --allow-unrelated-histories দুটি ইতিহাসকে একত্রিত করে (উভয়টিই গিট লগে থাকে)। --force একটি ইতিহাস অন্যটির সাথে ওভাররাইট করে (ওভাররাইট করা কমিট রিমোট থেকে অদৃশ্য হয়ে যায়)।
প্রশ্ন: সর্বদা সম্পর্কহীন ইতিহাসের অনুমতি দেওয়ার জন্য আমি কীভাবে গিট কনফিগার করব?
উত্তর: করবেন না — সঙ্গত কারণেই ডিফল্ট প্রত্যাখ্যান বিদ্যমান। পরিবর্তে মূল কারণ ঠিকানা. আপনি যদি নিয়মিত রেপোগুলি একত্রিত করেন তবে গিট সাবমডিউল বা টার্বোরেপোর মতো মনোরেপো টুল ব্যবহার করুন।
Q: Can I undo an accidental –allow-unrelated-histories merge?
উঃ হ্যাঁঃgit revert -m 1 MERGE_COMMIT_HASH মার্জ প্রত্যাবর্তন করতে, অথবাgit reset --hard HEAD~1 যদি আপনি এখনও ধাক্কা না.
প্রশ্ন: এটি একটি টিম রিপোজিটরিতে ঘটেছে — অন্যদের না ভেঙে আমি কীভাবে এটি ঠিক করব?
উত্তর: ভাগ করা শাখাগুলিকে জোর করবেন না। ব্যবহার করুনgit merge origin/main --allow-unrelated-histories একটি মার্জ কমিট তৈরি করতে, দ্বন্দ্ব সমাধান করতে এবং স্বাভাবিকভাবে পুশ করতে। বাকি সবাই পারেgit pull স্বাভাবিকভাবে
উপসংহার
“অসম্পর্কিত ইতিহাসগুলিকে একত্রিত করতে অস্বীকার করা” ত্রুটিটি প্রায় সর্বদাই আসে আপনার স্থানীয় প্রকল্পকে ঠেলে দেওয়ার আগে একটি প্রাথমিক README সহ একটি GitHub রেপো তৈরি করা থেকে। সবচেয়ে পরিষ্কার সমাধান: পুশ দিয়ে--force যদি অন্য কারো কাছে রিমোট না থাকে, বা ব্যবহার করে--allow-unrelated-histories উভয় ইতিহাস একত্রিত করতে. এগিয়ে যাওয়ার সর্বোত্তম অনুশীলন: আপনার কাছে ইতিমধ্যেই স্থানীয় প্রতিশ্রুতি থাকলে GitHub-এ রিপো শুরু করার পরিবর্তে খালি GitHub রেপো তৈরি করুন এবং স্থানীয় কোড পুশ করুন।
🔗 Share this article
✍️ Leave a Comment