Git — branches ו-tags
ליצור branch חדש ולעבור אליו
Section titled “ליצור branch חדש ולעבור אליו”git switch -c <BRANCH>יוצר branch מה-commit הנוכחי ועובר אליו. מתחילים מנקודה אחרת (commit, tag, branch אחר):
git switch -c <BRANCH> <SHA>git switch -c <BRANCH> origin/maingit switch -c <BRANCH> origin/<BRANCH>git branch <BRANCH> <SHA>השורה לפני האחרונה יוצרת מקומי שעוקב אחרי branch שקיים ב-remote (אחרי fetch). האחרונה יוצרת branch בלי לעבור אליו.
איך מוודאים: git branch --show-current מדפיס את השם, git branch -vv מראה אותו.
לעבור בין branches
Section titled “לעבור בין branches”git switch <BRANCH>git switch -git switch - חוזר ל-branch הקודם. אם יש שינויים לא שמורים שמתנגשים, git מסרב: או commit, או git stash (undo).
להביא branch שקיים רק ב-remote:
git fetch origingit switch <BRANCH>אם השם קיים רק ב-origin, git switch יוצר מקומי שעוקב אחריו אוטומטית.
איך מוודאים: git status -sb מציג ## <BRANCH>...origin/<BRANCH>.
לדחוף branch חדש ל-GitLab (push -u)
Section titled “לדחוף branch חדש ל-GitLab (push -u)”git push -u origin <BRANCH>-u (--set-upstream) קושר את ה-branch המקומי לזה שב-remote, ומעכשיו git push ו-git pull פועלים בלי ארגומנטים.
איך מוודאים: ההודעה Branch '<BRANCH>' set up to track remote branch '<BRANCH>' from 'origin'. וב-GitLab ה-branch מופיע ברשימה.
לדחוף כמה branches בבת אחת
Section titled “לדחוף כמה branches בבת אחת”git push -u origin poc devכל השמות אחרי origin נדחפים. כל ה-branches המקומיים: git push --all origin (בדוק קודם עם git branch, זה דוחף גם ניסויים).
איך מוודאים: git ls-remote --heads origin מציג את כל ה-branches שב-GitLab.
לדחוף branch מקומי ל-branch אחר ב-remote
Section titled “לדחוף branch מקומי ל-branch אחר ב-remote”git push origin <BRANCH>:maingit push origin HEAD:<BRANCH>מבנה: <local>:<remote>. HEAD:<BRANCH> דוחף את ה-commit הנוכחי ל-branch בשם הזה (ויוצר אותו אם אינו קיים).
דחיפה ל-main תיכשל אם main כבר התקדם: ראה sync.
ליצור tag ולדחוף אותו
Section titled “ליצור tag ולדחוף אותו”git tag <TAG>git tag -a <TAG> -m "release message"git tag <TAG> <SHA>git push origin <TAG>git tag <TAG> = tag קל (lightweight): רק שם על ה-commit הנוכחי. -a = tag annotated: אובייקט עם הודעה, מחבר ותאריך (git show <TAG> מציג אותם). שניהם נדחפים באותה פקודה. הוספת <SHA> מסמנת commit ישן.
איך מוודאים: git ls-remote --heads --tags origin מציג refs/tags/<TAG> (ל-annotated יופיע גם <TAG>^{} עם ה-commit עצמו) ואת כל ה-branches.
לראות, למחוק ולעבור ל-tag
Section titled “לראות, למחוק ולעבור ל-tag”git tag -lgit tag -ngit show <TAG> --statgit tag -d <TAG>git push origin --delete <TAG>git tag -d מוחק מקומית בלבד. git push origin --delete <TAG> מוחק מ-GitLab. לבדוק את הקוד כפי שהיה ב-tag: git switch --detach <TAG> (ראה undo).
להבין branch מול tag בשלוש שורות
Section titled “להבין branch מול tag בשלוש שורות”| branch | tag | |
|---|---|---|
| זז עם commit חדש | כן | לא |
| שימוש ב-GitOps | targetRevision: main (values) | targetRevision: <TAG> (templates) |
נדחף עם git push | רק הנוכחי | אף פעם (git push origin <TAG>) |
לראות tracking: מי מקביל למי
Section titled “לראות tracking: מי מקביל למי”git branch -vvgit branch -u origin/<BRANCH>git remote show origingit branch -u origin/<BRANCH> מקשר branch קיים (כשדחפת בלי -u). git remote show origin מציג גם branches שנמחקו ב-remote. לנקות הפניות מתות: git fetch --prune.
למחוק ולשנות שם של branch
Section titled “למחוק ולשנות שם של branch”git branch -d <BRANCH>git branch -D <BRANCH>git branch -m <BRANCH> new-namegit push origin --delete <BRANCH>-d מסרב אם ה-branch לא מוזג (not fully merged). -D מוחק בכוח: הקומיטים נשארים ב-git reflog. אי אפשר למחוק את ה-branch שעומדים עליו.
למזג branch ל-main
Section titled “למזג branch ל-main”git switch maingit pullgit merge --ff-only <BRANCH>git push--ff-only מתקדם רק אם main הוא אב קדמון ישיר של <BRANCH>, כלומר main מקבל בדיוק את ה-commits שנבדקו ב-<BRANCH>. אחרת נכשל (ראה sync).
איך מוודאים: git log --oneline -3 על main מציג את אותו <SHA> כמו ב-<BRANCH>: git rev-parse main <BRANCH> מדפיס שורה זהה פעמיים.