Git — היגיינה לפני commit
לבדוק רווחים בסוף שורה ו-CRLF לפני commit
Section titled “לבדוק רווחים בסוף שורה ו-CRLF לפני commit”הרווח שאחרי \ בשורת המשך שבר סקריפטים שלוש פעמים באותה ישיבה. git diff --check מוצא אותם לפני שהם נכנסים.
git diff --checkgit diff --cached --checkgrep -rnP ' +$' <FILE>cat -A <FILE> | grep -n ' \$$'--check בודק שינויים לא-staged, --cached --check את מה שב-staging. קובץ עם CRLF יסומן גם הוא כ-trailing whitespace. ב-cat -A סוף שורה הוא $, ו- $ (רווח לפניו) הוא הבעיה.
איך מוודאים: הפקודה לא מדפיסה כלום ו-exit code 0 (echo $?).
לתקן רווחים בסוף שורה בקובץ:
sed -i 's/[ \t]*$//' <FILE>קובץ עם CRLF (^M) שובר סקריפט
Section titled “קובץ עם CRLF (^M) שובר סקריפט”הסימפטום ב-bash: \r: command not found או bad interpreter: /bin/bash^M. הסיבה: הקובץ נשמר עם \r\n.
git ls-files --eolgrep -rlP '\r$' . --include='*.sh' --exclude-dir=.gitsed -i 's/\r$//' <FILE>ב-git ls-files --eol העמודה w/crlf מראה שהעותק בדיסק CRLF. sed מסיר את \r.
איך מוודאים: cat -A <FILE> מציג $ ללא ^M, ו-bash -n <FILE> שותק. קישור: debugging env-cards.
למנוע CRLF קבוע עם .gitattributes
Section titled “למנוע CRLF קבוע עם .gitattributes”* text=auto*.sh text eol=lf*.yaml text eol=lf*.yml text eol=lfאחרי יצירת הקובץ, לנרמל קבצים קיימים:
git add --renormalize .git status -sgit commit -m "normalize line endings"אפשר גם הגדרה ברמת המכונה: git config --global core.autocrlf input (ממיר CRLF ל-LF ב-commit, לא נוגע בהמשך).
איך מוודאים: git ls-files --eol מציג i/lf לכל הקבצים, ו-attr/text eol=lf בקבצים שהוגדרו.
לבדוק git status לפני reset --hard או clean
Section titled “לבדוק git status לפני reset --hard או clean”git statusgit stashgit clean -ndהמצב הבטוח: git status נקי, או git stash (ואפשר לשחזר), ו-clean -n לפני -f.
איך מוודאים: git status מציג nothing to commit, working tree clean.
לא לכלול קבצי זבל: .gitignore
Section titled “לא לכלול קבצי זבל: .gitignore”*.tmp*.log.env*.keyקובץ שכבר נכנס ל-commit לא נעלם מ-.gitignore. להוציא מהמעקב (בלי למחוק מהדיסק):
git rm --cached <FILE>git commit -m "stop tracking <FILE>"git check-ignore -v <FILE>איך מוודאים: git check-ignore -v <FILE> מדפיס את הכלל שמתאים.
להוסיף [skip ci] להודעת commit
Section titled “להוסיף [skip ci] להודעת commit”git commit -m "update docs [skip ci]"GitLab לא מריץ pipeline ל-commit שבהודעה שלו [skip ci]. שימושי בעיקר ל-commit שנכתב על ידי אוטומציה שדוחפת ל-repo שה-CI עצמו מאזין לו, כדי למנוע לולאה (CI דוחף, מפעיל CI, דוחף...).
איך מוודאים: git log -1 --format=%s מציג את ההודעה עם התג, ובעמוד Pipelines ב-GitLab לא נוצר pipeline חדש ל-commit הזה.
לבדוק שהזהות נכונה על ה-commit
Section titled “לבדוק שהזהות נכונה על ה-commit”git config user.namegit config user.emailgit log -1 --format='%an <%ae>'git commit --amend --reset-author --no-editהשורה האחרונה מתקנת את המחבר ב-commit האחרון (אם עוד לא נדחף).
צ'קליסט לפני push
Section titled “צ'קליסט לפני push”git status -sbgit diff --cached --checkgit log --oneline origin/main..HEADgit pushשורה 1: אין קבצים לא צפויים. שורה 2: אין רווחים או CRLF. שורה 3: בדיוק ה-commits שאתה מתכוון לדחוף. אם ה-push נדחה: git pull --rebase ושוב git push (sync).
איך מוודאים: אחרי push, git status -sb ללא ahead.