คำสั่ง git push Upload Local Version ของ Repository ไป Remote Repository Pushing เป็น Mechanism ที่คุณ Upload Changes ไป Remote Repository เมื่อ Push Changes แล้ว Collaborators ทั้งหมดบนโปรเจกต์สามารถ Download ได้
Push Code ไป Remote Repository เป็น Final Stage ของ "Saving" การเปลี่ยนแปลงที่คุณทำไปที่ Git Repository
Pushing Process โอน Code จาก Local Repository—คอมพิวเตอร์ของคุณ—ไป Remote Repository ที่ Local Code ของคุณเชื่อมโยงอยู่ นี่ให้คุณ Store Changes ที่ทำไปที่ Codebase ใน Main Repository ของโปรเจกต์
บทช่วยสอนนี้อภิปรายพร้อมตัวอย่าง Basics of Pushing Code และวิธีใช้ git push command จบบทความคุณจะเป็น Expert Push Code ด้วย git push command
Pushing Code
ใน Git "Saving" Changes ไม่ได้ง่ายเท่ากับ Save File เวลา Save File ใน Git Repository Changes จะ Stored บน Computer แต่ Changes จะไม่ Tracked โดย Git Repository คุณต้องบอก Git ว่า Changes ของคุณควร Tracked
เพื่อ Track Changes คุณต้องใช้ git add command ก่อน ซึ่ง Add Code เข้า Staging Area แล้วคุณใช้ git commit command Save Changes ที่ทำไป Commit
เมื่อ Commit Code เข้า Repository แล้ว Record ของ Changes ที่ทำจะ Tracked คุณต้อง Push Code หลังสร้าง Commit หากต้องการ Commits โชว์บน Remote Repository
Pushing Code ให้คุณส่ง Commits ที่ทำไปที่ Local Version ของ Repository ไป Remote Repository เช่น ทำงาน Team Project คุณสร้าง Commit บน Local Machine ก่อน พร้อมแล้ว Push ไป Remote Repository เพื่อทุก Collaborator เห็น Code
วิธีใช้ git push Command
คำสั่ง git push Upload Local Changes ของ Repository ไป Remote Repository Uploaded Changes จะ Accessible ให้ Project Collaborators ทั้งหมด View และ Download
ในความหมาย git push คือตรงข้ามของ git fetch คำสั่ง git fetch ใช้ Retrieve Changes ที่ทำไป Remote Repository Fetch Command Apply Changes เหล่านั้นไป Local Copy ของ Repository
Syntax สำหรับ git push command:
git push <remote name> <branch name>
Parameter "remote name" หมายถึง Repository ที่ Code ควร Push หาก Configured Repository แล้ว จะเป็น "origin" หากต้องการ Commit ไป Repository อื่น ระบุได้ด้วย Parameter "remote name"
Parameter "branch name" หมายถึง Branch ของ Remote Repository ที่ต้องการ Push Changes
สมมติเราต้องการ Push Changes จาก Local Repository ไป Branch "master" ของ Remote Repository ทำได้ด้วย:
git push origin master
คำสั่งคืนค่า:
Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (4/4), 363 bytes | 363.00 KiB/s, done. Total 4 (delta 0), reused 0 (delta 0) To https://github.com/jamesgallagher432/demo-repository.git 3b16026..b53b22d master -> master
ในตัวอย่างนี้ Remote Repository เก็บบน GitHub Changes ที่ทำไป Local Repository ถูก Push ไป Remote GitHub Repository ที่เชื่อมโยงกับโปรเจกต์
ตอนนี้ Code บน Local Machine เหมือน Code ใน Remote Repository Code ของเรา Available ใน Remote Repository Team Members จะเห็น Changes ที่เราทำ
ทางเลือก หากต้องการ Push Code ไป Branch "v1.9" สามารถระบุ Branch Name "v1.9" แทน "master" ได้
Git Push Force
ตอนใช้ git push command line operation คุณอาจต้องการ Force Changes Push ไป Remote Repository
Git ป้องกัน Push Code ไป Repository เมื่อมี Conflict ระหว่าง Remote และ Local Histories ของ Repository หาก Remote Repository มี 10 Commits ที่ไม่สะท้อนบน Local Machine คุณจะ Push Code ไม่ได้
Flag --force ให้คุณ "Force Push" Changes ที่ทำไป Repository Force Flag ลบ Changes ที่อาจเกิดขึ้นนับจากครั้งล่าสุดที่ Pull Code จาก Repository
ควรใช้ Flag --force ก็ต่อเมื่อ Notice ว่าทำ Error ใน Push ที่ Fix แล้ว มิฉะนั้น ควรหลีกเลี่ยงคำสั่งนี้ จะช่วย Ensure ไม่ทำ Unintended Changes ไป Git Repository
Syntax สำหรับ Flag --force:
git push <remote name> <branch name> --force
คำสั่งนี้ Force Push Code ของคุณ Errors ที่อาจขึ้นจะถูก Ignore
Git Push to Origin Master Example
ลอง Walk Through Common Scenario ที่อาจใช้ Git Push Command
สมมติคุณเพิ่งทำ Changes บน Local Code ที่ต้องการ Push ไป Remote Repository คุณสร้าง Commit ด้วย git commit แล้ว ซึ่ง Changes เก็บอยู่ เพื่อ Push Code มี Steps ที่ควรทำ
First คุณควร Navigate ไป Master Branch และแน่ใจว่า Up to Date Local Master Branch คือ Branch ที่เราทำ Changes คุณ Navigate ไป Master Branch:
git checkout master git fetch origin master
git checkout command ทำให้เรา Navigate ไป Branch "master" git fetch ให้เรา Retrieve Latest Version ของ Remote Repository
เราสามารถสร้าง Commit ด้วย Changes ที่ทำไป Master Branch ใน Original Repository:
git add README.md git commit -m "feat: Make changes to README"
เราเพิ่ม README.md เข้า Staging Area แล้วสร้าง Commit Commit Message คือ "feat: Make changes to README" Commit นี้เก็บอยู่ Local Branch ปัจจุบัน
จากนั้น รัน git push เพื่อ Push Code:
git push origin master
เวลารันคำสั่งนี้ Code ใน Local Repository จะ Push ไป Remote Repository เพราะเราเช็ค Code Up-to-date ก่อน Push git push command ไม่ควร Return Errors
สรุป
คำสั่ง git push ใช้ "Push" Changes จาก Local Git Repository ไป Remote Repository ทุกคนทำงาน Codebase จะเห็น Contributions ที่คุณทำเมื่อ Push แล้ว
บทช่วยสอนนี้อภิปราย Basics of Pushing Code และวิธีใช้ git push command ตอนนี้คุณพร้อม Push Code ไป Remote Git Repository อย่าง Git Master แล้ว!