יש פיצ’ר יעיל מאוד בGit שיוצא לי להשתמש בו הרבה כשאני צריך להעביר קומיטים ממקום למקום.
Cherry pick
git cherry-pick
מאפשרת להעביר שינויים שהתרחשו בקומיטים, ייתכן שבטעות נעשה קומיט מסויים בטעות על branch A
כשבעצם התכוונתי לעשות אותו על branch B
:
1 2 3 4 5 6 7 8 9 10 11 |
git checkout -b A // checkout into branch A echo 'my new file!' > new-file.txt git add . git commit -m 'this is my exciting new file' // OOPS! I wanted to commit that on my existing branch B!! // Let's fix this, get the commit hash by running the command `git show`: git checkout B git cherry-pick <commit-hash-to-pick> // now let's remove the commit from the A branch git checkout B git reset HEAD~1 --hard // delete the latest commit |
עוד אפשרות שיוצא לי להשתמש בה הרבה זה ללקוט טווח של קומיטים, קורה למשל כשאני יוצא לfeature branch חדש מdev עם כל מיני קומיטים בהיסטוריה שלי שעדיין לא קיימים בייצור ולפתע הלקוח מודיע שהוא רוצה רק את הפיצ’ר הזה בגרסה לייצור, אני בבעיה משום שהפיצ’ר שלי צריך להתבסס על master שמשקף את המצב בייצור.
במצב כזה הגיוני להשתמש בcherry-pick
כדי להתבסס על הmaster בצורה הבאה:
1 2 3 4 5 |
// create new feature branch based on master git branch feature/based-on-master master git checkout feature/based-on-master master // now cherry pick all the commits that exist on the dev-based feature branch git cherry-pick dev..feature/based-on-dev // branch1..branch2 is a way to get a range of commits that differ between two branches |
מאוד שימושי, תודה על ההסבר הברור