บทเรียนที่ 2 : การใช้ Git ตามคำแนะนำของ Github.com บน Bash on Ubuntu on Windows


เริ่มต้นกันเลย กับการสมัครใช้บริการพื้นที่ฟรี สำหรับเก็บ code ของ Github.com แล้วพบว่าหน้าแรกของ code ตอนที่ยังไม่มีแฟ้มใดเลย เค้าแนะนำคำสั่งมา 6 บรรทัดว่าจะสร้าง README.md อย่างไร ก็ลองพิมพ์ตามนั้นดูสักหน่อย เพราะ Nimit Suwannagate แนะนำคำสั่ง git ไว้ใน กลุ่ม ไทย PHP ที่เป็นการทำ backup_master เข้ามาเกี่ยวข้อง  มีคำสั่งน่าสนใจหลายคำสั่ง แต่ถ้าเริ่มต้นต้องคำสั่งบน Github.com นี่หละ สำหรับผมใช้ระบบปฏิบัติการ Windows 10 และมี Bash on Ubuntu on Windows ก็ถือโอกาสใช้ git บน Linux ที่อยู่ใน Windows

ขั้นตอนการใช้ git ตามที่ github.com แนะนำ ดังนี้

1. สร้าง folder ชื่อ git ที่ drive d
และสร้าง folder ชื่อ mygitfriends ใน git
จะได้ folder d:/git/mygitfriends เตรียมพื้นที่ไว้ทำงาน และอ้างอิงง่าย ๆ หน่อย
ที่ตั้งชื่อว่า mygitfriends จะลองชวนเพื่อนที่สนใจ
มาเข้ากลุ่ม collaborators ของ repository จึงตั้ง folder เป็นชื่อนี้

2. เข้า bash on ubuntu on windows
แล้วติดตั้ง git เพื่อทำงานใน ubuntu on windows

$sudo apt-get install git

3. นั่งมองคำสั่งที่ github.com แนะนำแป๊ปนึง มีความหมายทุกบรรทัด

echo "# mygitfriends" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:thaiall/mygitfriends.git
git push -u origin master

4. ใน bash on ubuntu on windows
ทำตามขั้นตอนดังนี้
4.1 $cd /mnt/d/git/mygitfriends

4.2 $echo “# Hello world! v.1” >> README.md

4.3 $git init จะสร้าง folder .git แล้วแฟ้มอีกเพียบขึ้นมาให้

4.4 $git add README.md

4.5 $git commit -m “first commit” ได้รับแจ้งว่าต้อง config email กับ name ก่อนสั่งงาน ก็ต้องสั่ง 2 บรรทัดนี้ก่อน
– git config –global user.email “you@example.com”
– git config –global user.name “Your Name”
หลัง config ก็สั่งใหม่ ก็เรียบร้อยด้วยดี
[master (root-commit) 2ff254f] first commit
1 file changed, 1 insertion(+)
create mode 100644 README.md

4.6 $git remote add origin https://github.com/thaiall/mygitfriends.git
หลังจากสั่ง $git remote ทำให้แฟ้ม config เพิ่มมา 3 บรรทัด
[remote “origin”]
url = https://github.com/thaiall/mygitfriends.git
fetch = +refs/heads/*:refs/remotes/origin/*

4.7 $git push -u origin master
เค้าแนะนำว่าให้ git pull ก่อน แล้วค่อย pushing again
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://github.com/thaiall/mygitfriends.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push –help’ for details.

4.8 $git pull origin master
warning: no common commits
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 8 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
From https://github.com/thaiall/mygitfriends
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Merge made by the ‘recursive’ strategy.
_config.yml | 1 +
collaborators.htm | 14 ++++++++++++++
index.md | 37 +++++++++++++++++++++++++++++++++++++
3 files changed, 52 insertions(+)
create mode 100644 _config.yml
create mode 100644 collaborators.htm
create mode 100644 index.md
จากนั้นก็เปิดแฟ้ม .git/MERGE_MSG มาให้แก้ไข ก็ไม่ได้แก้ไขครับกด Ctrl+X ออกไป

4.9 $git push -u origin master

Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (5/5), 503 bytes | 0 bytes/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/thaiall/mygitfriends.git
5f749af..353f84d master -> master

5. ตรวจสอบผลงานตามขั้นตอนข้างต้น
หลังจากสั่ง $git push .. พบแฟ้ม README.md เข้าไปอยู่ใน github.com
มีเพียงแฟ้มเดียวที่วันที่เปลี่ยนอย่างถูกต้อง แฟ้มที่เหลือยังอยู่กันตามปกติ

5.1 ถ้า pull ใหม่ เค้าบอกว่า up-to-date แล้ว
From https://github.com/thaiall/mygitfriends
* branch master -> FETCH_HEAD
Already up-to-date.

5.2 ถ้า push ใหม่ เค้าบอกว่า up-to-date แล้วเช่นกัน
Username for ‘https://github.com’: [yourusername]
Password for ‘https://[yourusername]@github.com’:
Everything up-to-date

5.3 สรุปว่า ถ้า push หรือ pull โดยไม่เปลี่ยนแปลงแฟ้มใน origin
ก็จะไม่ส่งผลถืง master ที่อยู่ใน github.com เช่นกัน เพราะ git จะตรวจสอบก่อน

Author: บุรินทร์ รุจจนพันธุ์

I am Lecturer, Developer, Researcher, Columnist, Writer, Photographer, and Webmaster - L@mpang man

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.