דלגו לתוכן

Git — הכנה וסדר עבודה בסיסי

מה Git עושה בפרויקט כמו TRIDENT, ואיפה מריצים אותו

Section titled “מה Git עושה בפרויקט כמו TRIDENT, ואיפה מריצים אותו”

בפרויקט יש כמה repos נפרדים, וכל אחד נכתב על ידי גורם אחר:

repoמי כותבמי קורא
trident-sourceאתה (קוד, Dockerfile, בדיקות)ה-runner (CI)
trident-ciאתה (YAML של ה-pipeline + promote.sh)include מתוך source
trident-gitopsאתה (values, Applications) וגם ה-CI (קובץ versions/<ENV>.yaml)Argo CD
trident-templatesאתה (charts)Argo CD

שלושה עותקים של כל repo: העותק ב-GitLab (האמת), העותק של Argo (קורא משם בלבד), והעותק המקומי שלך (נעשה ישן מהר). push שלא קרה = לא קיים בשביל Argo.

ביום המבחן סביר שהכול קורה על ה-VM עצמה: git, kubectl ו-helm באותו shell. תכין שם זהות ו-SSH key (ראה ssh) ולא תסתמך על Mac.

5 הדקות הראשונות על VM חדשה

Section titled “5 הדקות הראשונות על VM חדשה”
runs on: any shell
git config --global user.name "<USER>"
git config --global user.email "<EMAIL>"
git config --global pull.ff only
mkdir -p -m 700 ~/.ssh
ssh-keygen -t ed25519 -f ~/.ssh/exam_key -C "exam-vm" -N ""
cat ~/.ssh/exam_key.pub

הדבק את ה-.pub ב-GitLab (Avatar ← Edit profile ← SSH Keys), ואז:

runs on: any shell
ssh -F /dev/null -i ~/.ssh/exam_key -o IdentitiesOnly=yes -T git@<GITLAB_HOST>
printf 'Host <GITLAB_HOST>\n User git\n IdentityFile ~/.ssh/exam_key\n IdentitiesOnly yes\n' >> ~/.ssh/config
chmod 600 ~/.ssh/config
git clone git@<GITLAB_HOST>:<GROUP>/<REPO>.git

חזור על ה-clone לכל repo. הסבר ופתרון תקלות: ssh.

איך מוודאים: Welcome to GitLab, @<USER>!, ו-git -C <REPO> log --oneline -1 מראה commit.

להגדיר זהות פעם אחת (user.name / user.email)

Section titled “להגדיר זהות פעם אחת (user.name / user.email)”

בלי זהות git commit נכשל או חותם בשם לא רצוי. ההגדרה נשמרת ב-~/.gitconfig.

runs on: any shell
git config --global user.name "<USER>"
git config --global user.email "student@example.com"
git config --global init.defaultBranch main
git config --global pull.ff only

pull.ff only גורם ל-git pull להיכשל בקול במקום ליצור merge commit מיותר (ראה sync).

איך מוודאים: git config --global --list מציג את ארבע השורות.

השתמש ב-path של ה-group, לא בשם התצוגה. הכתובת המדויקת: ב-GitLab לחץ Code ← Clone with SSH.

runs on: any shell
git clone git@<GITLAB_HOST>:<GROUP>/<REPO>.git
# TRIDENT: git clone git@gitlab.com:trident-lab00/trident-gitops.git
cd <REPO>

איך מוודאים: git remote -v מציג את אותה כתובת פעמיים (fetch ו-push), ו-git log --oneline -3 מראה commits.

למשוך branch ספציפי כבר בשלב ה-clone (--single-branch מוריד רק אותו):

runs on: any shell
git clone -b <BRANCH> --single-branch git@<GITLAB_HOST>:<GROUP>/<REPO>.git

לראות איפה אני: status, branch, remotes

Section titled “לראות איפה אני: status, branch, remotes”
runs on: any shell
git status -sb
git branch --show-current
git remote -v
git branch -vv

git status -sb נותן שורה ראשונה כמו ## main...origin/main [behind 1] ואחריה קבצים ששונו. git branch -vv מראה לכל branch מול איזה remote branch הוא עוקב ואם הוא ahead/behind.

פלטמשמעות
לשנות כתובת של remote: git remote set-url origin <URL>, ואז git remote -v לאימות.

| M file | שונה, לא ב-staging | | M file | שונה וב-staging (יכנס ל-commit) | | ?? file | קובץ חדש שגיט לא עוקב אחריו | | [ahead 1] | יש commit מקומי שלא נדחף | | [behind 1] | יש commit ב-remote שלא משכת |

runs on: any shell
git add <FILE>
git commit -m "describe the change"
git push

git add -A מוסיף הכול (כולל קבצי זבל). עדיף קבצים בשמם, או git add -p כדי לבחור חלקים. git commit -am "msg" מדלג על add, אבל רק לקבצים שכבר במעקב.

איך מוודאים: git status -sb ללא ahead, ו-git log --oneline -1 מראה את ההודעה. ב-GitLab ה-commit מופיע ב-repo.

runs on: any shell
git log --oneline --graph --decorate --all -15
git diff
git diff --staged
git diff origin/main..HEAD
git show <SHA> --stat
git log -p -- <FILE>
פקודהמראה
git diffשינויים שעוד לא ב-staging
git diff --stagedמה ייכנס ל-commit הבא
git diff origin/main..HEADמה ה-commits שלך מוסיפים מעל origin/main (אחרי fetch)
git log --oneline HEAD..origin/maincommits שיש ב-remote ואין אצלך
git show <SHA> --statcommit אחד: הודעה + קבצים

מי שינה קובץ לאחרונה (למשל versions/dev.yaml)

Section titled “מי שינה קובץ לאחרונה (למשל versions/dev.yaml)”
runs on: any shell
git fetch
git log origin/main -1 --format='%h %an %ad %s' -- <FILE>
git blame -L 1,10 <FILE>

שורה ראשונה: ה-commit האחרון שנגע בקובץ (מחבר של בוט ה-CI = promote). blame -L 1,10 מראה לכל שורה בטווח את ה-commit שכתב אותה.

איך מוודאים: ה-<SHA> מה-log מופיע גם ב-git show <SHA> --stat עם הקובץ.

לבדוק מה קרה ל-repo לפני שאני עורך

Section titled “לבדוק מה קרה ל-repo לפני שאני עורך”

לפני כל עריכה ב-repo שה-CI כותב אליו (כמו gitops):

runs on: any shell
git pull
git status -sb

איך מוודאים: ## main...origin/main בלי ahead או behind. אם git pull נכשל, עבור ל-sync.