מאגרים מרוחקים (Remote)

Remote הוא מאגר git שנמצא בשרת מרוחק (לרוב GitHub, GitLab או Bitbucket). הוא מאפשר לכל חברי הצוות לעבוד על אותו פרויקט ולשתף את השינויים ביניהם.

מהו remote?

באופן טכני, remote הוא רק קיצור (alias) לכתובת URL של מאגר אחר. כשמשכפלים מאגר עם git clone, נוצר אוטומטית remote בשם origin שמצביע על הכתובת ממנה שכפלת.

פקודות בסיסיות

צפייה ב-remotes

git remote                  # רשימת השמות
git remote -v               # רשימה עם הכתובות (verbose)
git remote show origin      # מידע מפורט על remote ספציפי

הפלט של -v:

origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

הוספת remote

git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original-owner/repo.git

הקוד הראשון מוסיף remote בשם origin. הקוד השני מוסיף remote נוסף בשם upstream - שימושי כשעובדים עם fork.

שינוי כתובת של remote קיים

git remote set-url origin https://github.com/user/new-repo.git

הסרה ושינוי שם

git remote remove origin
git remote rename origin upstream

שמות מקובלים

שם למה משמש
origin המאגר המרכזי שלכם - בדרך כלל מ-GitHub
upstream המאגר המקורי, כשעובדים מ-fork

המוסכמה: כשאתם מבצעים fork ב-GitHub, אתם משכפלים את ה-fork שלכם - אז origin הוא ה-fork. אתם מוסיפים ידנית upstream שמצביע על המאגר המקורי כדי לסנכרן ממנו עדכונים.

remote branches

remote branches הם branches שקיימים בשרת המרוחק. ב-git המקומי שלכם הם מופיעים בשם <remote>/<branch>:

git branch -r              # רק remote branches
# origin/main
# origin/develop
# origin/feature/login

git branch -a              # מקומיים + remote

חשוב: remote branches הם read-only ב-מקומי שלכם. אתם לא יכולים לעבוד ישירות עליהם - אתם צריכים ליצור branch מקומי שעוקב אחרי remote branch.

tracking branches

Branch מקומי יכול "לעקוב" אחרי remote branch - זה אומר ש-git pull ו-git push ידעו לאן לדחוף ומאיפה למשוך אוטומטית, בלי שתצטרך לציין בכל פעם.

# יצירת branch מקומי שעוקב אחרי remote
git checkout -b feature/login origin/feature/login

# או בתחביר חדש
git switch -c feature/login --track origin/feature/login

# קביעת tracking ל-branch קיים
git branch -u origin/feature/login

# ביטול tracking
git branch --unset-upstream

דוגמה: עבודה עם fork

# 1. עשית fork ב-GitHub והקלונת אותו
git clone https://github.com/your-username/repo.git
cd repo

# 2. הוספת את המאגר המקורי כ-upstream
git remote add upstream https://github.com/original-owner/repo.git

# 3. בודקים את הרשימה
git remote -v
# origin    https://github.com/your-username/repo.git (fetch)
# origin    https://github.com/your-username/repo.git (push)
# upstream  https://github.com/original-owner/repo.git (fetch)
# upstream  https://github.com/original-owner/repo.git (push)

# 4. סנכרון מ-upstream
git fetch upstream
git checkout main
git merge upstream/main
git push origin main

בדקו את עצמכם

נסו לענות לבד לפני שאתם פותחים את התשובה.

  1. מה זה origin?

    1. השם המקובל למאגר המרוחק שממנו שכפלתם
    2. הענף הראשי
    3. ה-commit הראשון
    4. משתמש
    הצגת התשובה

    תשובה א. git remote -v מציג את הכתובות.