त्रुटि“घातक: असंबंधित इतिहास को मर्ज करने से इनकार” तब प्रकट होता है जब आप किसी Git रिपॉजिटरी से विलय या खींचने का प्रयास करते हैं जिसका आपकी वर्तमान शाखा के साथ कोई सामान्य पूर्वज नहीं है। ऐसा अक्सर तब होता है जब आप रीडमी के साथ गिटहब पर एक नया रेपो बनाते हैं और एक स्थानीय प्रोजेक्ट को आगे बढ़ाने का प्रयास करते हैं, या जब आप दो पूरी तरह से अलग रिपॉजिटरी का संयोजन कर रहे होते हैं। यहां हर परिदृश्य और उसका सही समाधान है।
📋 Table of Contents
- यह त्रुटि क्यों होती है
- परिदृश्य 1: रीडमी के साथ गिटहब पर रेपो बनाया गया, स्थानीय ने पहले से ही प्रतिबद्धताएं रखी हैं
- परिदृश्य 2: दो अलग-अलग रिपॉजिटरी का संयोजन
- परिदृश्य 3: रेपो को दोबारा बनाए जाने के बाद गिट पुल
- सही तरीका: जब आपके पास स्थानीय कोड हो तो GitHub पर README न बनाएं
- –अनुमति-असंबंधित-इतिहास कब सुरक्षित है?
- रिपॉजिटरी संबंधों की जाँच करना
- अक्सर पूछे जाने वाले प्रश्न
- निष्कर्ष
यह त्रुटि क्यों होती है
Git प्रतिबद्धताओं की एक श्रृंखला के माध्यम से इतिहास को ट्रैक करता है। जब दो रेपो ने कभी भी एक सामान्य प्रतिबद्धता (असंबंधित इतिहास) साझा नहीं की है, तो Git उन्हें Git 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 रेपो आरंभ किया है, फिर अपने स्थानीय प्रोजेक्ट को आगे बढ़ाने का प्रयास करें। आपके स्थानीय लोगों के पास प्रतिबद्धताएं हैं; GitHub की एक प्रतिबद्धता (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
सही तरीका: जब आपके पास स्थानीय कोड हो तो GitHub पर README न बनाएं
असली समाधान रोकथाम है. जब आपके पास कोई मौजूदा स्थानीय प्रोजेक्ट हो:
# 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: --allow-unrelated-histories दो इतिहासों को मर्ज करता है (दोनों git लॉग में रहते हैं)। --force एक इतिहास को दूसरे के साथ अधिलेखित कर देता है (अधिलेखित कमिट रिमोट से गायब हो जाते हैं)।
प्रश्न: मैं असंबंधित इतिहास को हमेशा अनुमति देने के लिए गिट को कैसे कॉन्फ़िगर करूं?
उत्तर: ऐसा न करें – डिफ़ॉल्ट इनकार अच्छे कारण के लिए मौजूद है। इसके बजाय मूल कारण का समाधान करें। यदि आप नियमित रूप से रेपो का संयोजन कर रहे हैं, तो गिट सबमॉड्यूल या टर्बोरेपो जैसे मोनोरेपो टूल का उपयोग करें।
प्रश्न: क्या मैं आकस्मिक–अनुमति-असंबंधित-इतिहास मर्ज को पूर्ववत कर सकता हूं?
उत्तर: हाँ: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