Branching
Vytvoření nové branche
git checkout -b new_branch # vytvoří ji a přesune se na ni
git br new_branch # vytvoří ji, ale zůstane na původní
Práce s branchemi na master serveru
První push nové lokální branche na server:
git push -u origin my_branch
Výpis všech branchí na serveru
git branch -r
Checkout branche, které je zatím jen na serveru (-t je jako track)
git fetch origin
git checkout -t origin/remote_branch_name
Smazání branche
# smazani na originu
git push origin --delete branchName
# smazani lokalni vetve
git branch -d branchName
# pak je tu jeste jedna varianta, ma smazat remote, ale nejak to nevali
git branch -rd origin branchName
Přejmenování loklání branche
git branch -m old_branch new_branch
Merge
- zrušení nedokončeného merge: git reset
- nalezení společného předka dvou branchí: git merge-base branch1 branch2
- git checkout --ours, git checkout --theirs when you have two different new files with the same name in both branches
Rebase
- aktuální větev nastaví na aktuálního mastera a postupně aplikuje místní commity jako patche
Merge vs. rebase
- výsledek je stejný
- rebase jde postupně, takže je pracnější, ale přehlednější
- rebase mění commity v aktuální branchi
- Merging cons: If the need to merge arises simply because multiple people are working on the same branch in parallel, the merges don’t serve any useful historic purpose and create clutter. (source: blog.sourcetreeapp.com)
- merge znepřehledňuje výpisy commitů (dá se ale pomocí --first-parent odfitrovat; "lokální" merge pokud máme nepushnuté commity a nějaké commity jsou i v originu tam můžou také zaneřáďovat historii, ale to už není čistě problém merge). Rebase zase znepřehledňuej historii tím, že může přidat několik souvisejících commitů přímo do mastera
- Řídím se pravidlem:
- je v branchi málo commitů (1 -3) => rebase aby byla čistější a jednodušší historie
- je v branchi více commitů nebo chceme podtrhnout to že patří k sobě => merge
Another tips
- prefer rebase on your private branches, but merge on shared
- don't rebase already pushed public branches!
- use git stash
- use git reset --hard and git reset for change your not-pushed-yet commits
Articles about Git workflow
Which branches contains certain commit?
git branch --contains <commit>
git diff branch1..branch2 # diff between two branches
git diff branch1...branch2 # diff between A-B ancestor and B (same as git diff $(git-merge-base A B) B)