Git 105: remote, pull, push และ GitHubGit 105: remotes, pull, push, and GitHub
เชื่อม Git local กับ remote เข้าใจ origin, pull ก่อน push และใช้ GitHub เป็นพื้นที่สำหรับการ review ร่วมกันConnect local Git to a remote, understand origin, pull before push, and use GitHub as the place where shared review happens.
Git is local first, but collaboration needs a shared place.
That shared place is called a remote.
GitHub is one popular remote host. GitLab, Bitbucket, and private servers can also host Git repositories.
What origin means
When a repository has a remote, the default remote is often named origin.
Check remotes:
git remote -v
If nothing appears, your practice repository has no remote yet.
Two common starting paths
There are two common ways to start.
Path A: clone an existing remote
git clone <repository-url>
cd <repository-folder>
git clone downloads the repository and sets up the remote for you.
Path B: create locally, then add remote
git remote add origin <repository-url>
git branch -M main
git push -u origin main
Use this when you created the project on your computer first and then made an empty remote repository.
Pull before push
Before pushing shared work, check whether the remote has changes you do not have.
git pull
For a beginner, read git pull as:
Bring remote changes into my current branch.
If Git reports conflicts, pause. Do not guess through conflict resolution.
Push your branch
Push sends local commits to the remote:
git push
The first push of a new branch often needs:
git push -u origin docs-add-learning-goal
The -u connects your local branch to the remote branch so future git push and git pull can be shorter.
Where GitHub fits
After a branch is pushed to GitHub, you can open a pull request.
A pull request is not Git itself. It is GitHub's review workflow around Git branches and diffs.
Use it to:
- explain what changed
- ask for review
- show diffs in the browser
- run automated checks
- discuss before merge
This is why the class is Git 10x, not GitHub 10x. Git is the foundation. GitHub is one place where the foundation becomes team workflow.
Safer remote habits
Before push:
git status
git log --oneline --decorate -5
git diff origin/main...HEAD
Then ask:
- am I on the right branch?
- are these commits the ones I meant to share?
- did I accidentally commit secrets?
- is my branch based on recent remote work?
Avoid force push until you understand exactly why it is needed and who else may be affected.
Practice: remote map
For a real repository or a practice clone, fill this out:
Remote host:
Remote name:
Default branch:
Current branch:
Command to pull:
Command to push this branch:
Where review happens:
Git เริ่มจาก local แต่การทำงานร่วมกันต้องมีพื้นที่กลาง
พื้นที่กลางนี้เรียกว่า remote
GitHub เป็น remote host ที่นิยมมาก แต่ GitLab, Bitbucket หรือ server ภายในก็ host Git repository ได้เหมือนกัน
origin คืออะไร
เมื่อ repository มี remote แล้ว remote หลักมักชื่อ origin
เช็ก remote:
git remote -v
ถ้าไม่มีอะไรแสดง แปลว่า practice repository ของคุณยังไม่มี remote
วิธีเริ่มที่พบบ่อย 2 แบบ
มีสองทางที่เจอบ่อย
ทาง A: clone remote ที่มีอยู่
git clone <repository-url>
cd <repository-folder>
git clone download repository และตั้ง remote ให้เลย
ทาง B: สร้างในเครื่องก่อน แล้วค่อย add remote
git remote add origin <repository-url>
git branch -M main
git push -u origin main
ใช้เมื่อคุณสร้าง project ในเครื่องก่อน แล้วค่อยสร้าง remote repository เปล่า
Pull ก่อน push
ก่อน push งานร่วม ให้เช็กว่า remote มี change ที่คุณยังไม่มีหรือไม่
git pull
สำหรับมือใหม่ ให้อ่าน git pull ว่า:
เอา change จาก remote เข้ามาใน branch ปัจจุบันของฉัน
ถ้า Git บอกว่ามี conflict ให้หยุดก่อน อย่าเดาเพื่อแก้ conflict
Push branch
push ส่ง local commits ไป remote:
git push
push ครั้งแรกของ branch ใหม่มักต้องใช้:
git push -u origin docs-add-learning-goal
-u เชื่อม local branch กับ remote branch เพื่อให้ครั้งต่อไปใช้ git push และ git pull แบบสั้นได้
GitHub อยู่ตรงไหน
หลัง push branch ขึ้น GitHub คุณเปิด pull request ได้
pull request ไม่ใช่ Git โดยตรง แต่มันคือ workflow review ของ GitHub ที่อยู่รอบ branch และ diff ของ Git
ใช้ PR เพื่อ:
- อธิบายว่าเปลี่ยนอะไร
- ขอ review
- แสดง diff ใน browser
- รัน automated checks
- คุยก่อน merge
นี่คือเหตุผลที่ class นี้ควรเป็น Git 10x ไม่ใช่ GitHub 10x Git คือพื้นฐาน ส่วน GitHub เป็นพื้นที่หนึ่งที่ทำให้พื้นฐานนั้นกลายเป็น workflow ของทีม
นิสัย remote ที่ปลอดภัย
ก่อน push:
git status
git log --oneline --decorate -5
git diff origin/main...HEAD
จากนั้นถาม:
- อยู่ branch ถูกไหม?
- commit เหล่านี้คือสิ่งที่ตั้งใจ share หรือไม่?
- เผลอ commit secret ไหม?
- branch ของฉันตาม remote ล่าสุดพอไหม?
เลี่ยง force push จนกว่าจะเข้าใจชัดว่าทำไปทำไม และใครอาจได้รับผลกระทบ
แบบฝึก: remote map
สำหรับ repository จริงหรือ practice clone ให้เติม:
Remote host:
Remote name:
Default branch:
Current branch:
Command สำหรับ pull:
Command สำหรับ push branch นี้:
พื้นที่ review อยู่ที่:
เข้าสู่ระบบเพื่อบันทึกความคืบหน้าSign in to track your progress
เข้าสู่ระบบSign in→