Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การแก้ไขปัญหา >> ข้อผิดพลาด Windows

แก้ไข: 'git' is not recognized as an internal or external command

ผู้ใช้หลายรายรายงานว่าได้ข้อผิดพลาด 「『git』 is not recognized as an internal or external command」 เมื่อพยายามรันคำสั่ง git ใน Command Prompt บางรายเจอหลังติดตั้ง Git for Windows สักพัก บางรายเจอทันทีหลังติดตั้งเสร็จ

แก้ไข:  git  is not recognized as an internal or external command

สาเหตุของข้อผิดพลาด『git』 is not recognized as an internal or external command

เราสืบสวนจากรายงานผู้ใช้ พบว่ามีสถานการณ์หลายอย่างที่ทำให้เกิดข้อผิดพลาดนี้:

  • Git PATH ไม่ได้ตั้งค่า (หรือตั้งค่าผิด) ใน Variables — ซอฟต์แวร์ล่าสุดหรือข้อผิดพลาดของผู้ใช้อาจทำให้ Git PATH ใน Variables ตั้งค่าผิด
  • CMD เปิดอยู่ระหว่างติดตั้ง GIT — หากคุณติดตั้ง Git for Windows ขณะที่หน้าต่าง Command Prompt เปิดอยู่ ปัญหาอาจแก้ได้ทันทีเมื่อเปิด Command Prompt ใหม่

หากคุณกำลังพยายามแก้ไขปัญหานี้ บทความนี้จะให้คู่มือตรวจสอบและแก้ไข ด้านล่างมีคอลเลกชันวิธีที่ผู้ใช้คนอื่นใช้แก้ไขได้ สำหรับผลลัพธ์ที่ดีที่สุด ให้ทำตามวิธีตามลำดับจนกว่าจะพบวิธีที่แก้ปัญหาในสถานการณ์ของคุณได้จริง

วิธีที่ 1: เปิด Command Prompt ใหม่ (Re-open Command Prompt)

หากคุณเป็นคนชอบใช้ terminal และเปิด CMD ไว้ตลอด (แม้ระหว่างติดตั้ง Git) ปัญหาอาจเกิดเพราะ Command Prompt ไม่ได้รับการอัปเดตกับการเปลี่ยนแปลง Variables ล่าสุด

หากเป็นกรณีนี้ วิธีแก้ก็คือปิดหน้าต่าง CMD แล้วเปิดใหม่ หากพาธตั้งค่าถูกต้อง คุณจะสามารถใช้คำสั่ง Git ได้โดยไม่เจอข้อผิดพลาด 「『git』 is not recognized as an internal or external command」

หากไม่ใช่กรณีนี้ ให้ไปวิธีถัดไป

วิธีที่ 2: ใช้วิธีอัตโนมัติในการเพิ่ม GIT path ลง Variables

หากคุณอยากหลีกเลี่ยงการแก้ไข PATH variables เอง สามารถแก้ไขข้อผิดพลาด 「『git』 is not recognized as an internal or external command」 โดยใช้ GUI ติดตั้ง Git เพื่อสร้าง Path variables ให้อัตโนมัติ ซึ่งจะทำให้คุณใช้ Git ได้จากทั้ง Git Bash และ Windows Command Prompt

คู่มือเร็วในการถอนติดตั้ง Git แล้วตั้งค่าการติดตั้งครั้งแรกให้เพิ่ม path variables อัตโนมัติ:

  1. กด Windows key + R พิมพ์ 「appwiz.cpl」 กด Enter เปิดหน้าต่าง Programs and Featuresแก้ไข:  git  is not recognized as an internal or external command
  2. ใน Programs and Features หา entry ของ Git คลิกขวาเลือก Uninstall ตามพรอมป์ถอนติดตั้ง Git ปัจจุบันแก้ไข:  git  is not recognized as an internal or external command
  3. รีสตาร์ทคอมพิวเตอร์เพื่อเสร็จสิ้นการถอนติดตั้ง
  4. ไปที่ลิงก์นี้ (ที่นี่) ดาวน์โหลด Git for Windows ล่าสุด การดาวน์โหลดควรเริ่มอัตโนมัติ หากไม่เริ่ม คลิกเวอร์ชันที่ตรงกับ bit architecture ของ OSแก้ไข:  git  is not recognized as an internal or external command
  5. เปิดไฟล์ติดตั้ง ตามพรอมป์การติดตั้ง สามารถปล่อยค่าเริ่มต้นได้ เมื่อถึงขั้น Adjusting your PATH environment ให้เลือก toggle Use Git from Windows Command Promptแก้ไข:  git  is not recognized as an internal or external command
  6. ดำเนินการตั้งค่าต่อไปด้วยค่าเริ่มต้น (หรือเลือกเอง) แล้วกด Installแก้ไข:  git  is not recognized as an internal or external command
  7. ติดตั้งเสร็จ รีสตาร์ทคอมพิวเตอร์ ที่บูทครั้งต่อไป คุณจะรันคำสั่งได้โดยตรงจาก Windows Command Prompt

หากมองหาวิธีแก้โดยไม่ต้องถอนติดตั้ง Git client ให้ไปวิธีถัดไป

วิธีที่ 3: เพิ่มตัวแปร PATH ด้วยตนเอง (Adding the variable PATH manually)

หากมาถึงขั้นนี้ยังไม่ได้ผล น่าจะเป็นเพราะตัวแปร Git ไม่ได้ตั้งค่า (หรือตั้งค่าผิด) ใน Environment Variables

โชคดีที่คุณสามารถตั้งค่าตัวแปรด้วยตนเองได้โดยทำตามขั้นตอน ข้อต้องทำ:

  1. เปิด File Explorer ไปที่โฟลเดอร์ cmd ภายในการติดตั้ง Git พาธเริ่มต้นสำหรับ x86 และ x64:
    My Computer (This PC) > Local Disk(C:) > Program Files (x86) > Git > cmd My Computer (This PC) > Local Disk(C:) > Program Files > Git > cmd
  2. คลิกขวา git.exe เลือก Properties ในแท็บ General ของ git.exe Properties คัดลอกตำแหน่งของ executable (จะใช้ทีหลัง)แก้ไข:  git  is not recognized as an internal or external command
  3. กด Windows key + R พิมพ์ 「sysdm.cpl」 กด Enter เปิดเมนู System Propertiesแก้ไข:  git  is not recognized as an internal or external command
  4. ในเมนู System Properties ไปที่แท็บ Advanced คลิก Environment Variablesแก้ไข:  git  is not recognized as an internal or external command
  5. ในเมนู Environment Variables ไปที่ submenu System variables เลือก Path คลิกปุ่ม Editแก้ไข:  git  is not recognized as an internal or external command
  6. ในหน้าต่าง Edit environment variables กดปุ่ม New วางตำแหน่งที่คัดลอกขั้นตอนที่ 2 แล้วกด Enter สร้างตัวแปรแก้ไข:  git  is not recognized as an internal or external command
  7. กด OK ทุกพรอมป์ที่เปิดอยู่ เพื่อให้แน่ใจว่าการเปลี่ยนแปลงถูกบันทึก
  8. เปิด CMD พิมพ์ 「git」 ควรไม่เจอข้อผิดพลาด 「『git』 is not recognized as an internal or external command」 อีกต่อไปแก้ไข:  git  is not recognized as an internal or external command