Git DOs and DONTs
Git DOs and DONTsHints and Common Pitfalls
Matthias Mnnich, July 19th 2011Hint 1: Separate Local Branchesrecommended: One Change, One Brancheasy checkout your isolated changesamend / rebase if necessary and push again
delete branches that got obsoletefetch fresh branch informationbranches can be deleted with d if commits are publicHint 1: Separate Local Branchesname your branches to remember the source and the intentionmergeHanaISCrashFix_f4dB1234_hanaHint 2: Switching the target branchyou intended to work on feature1 for hana=> branch : feature1_hana
now it should go to orange git checkout b feature1_orange origin/orange git log feature1_hana to get the hash(es) git cherry-pick Hint 3: Do not switch branches during build!switching branches with checkout changes working directory
use hdbenvinit to set up a second workspace
e.g. for git only tasksHint 4:When to merge, when to rebase?Merge public branches for integrationsdone by topic owners
$ git fetch$ git checkout b mergeIntoHana origin/hana$ git merge --no-ff --no-commit origin/dev$ git commit- adjust the commit message
Do not rebase merge commits.Always begin with the first command to redo a merge.Hint 4:When to merge, when to rebase?Rebase local branches containing YOUR UNPUBLISHED changes
$ git checkout b feature-1origin/test$ hack hack hack$ git commit$ git fetch$ git rebase origin/testABCorigin/testDEFfeature-112GH12feature-1Hint 4:When to merge, when to rebase?Unpublished means:not yet merged to any public branch
Your means:your local changes that come up in your history
if you think this is always obvious, wait for the next slideHint 5:What is a rebase of public commits?What exactly does a rebase? $ git checkout mybranch $ git rebase origin/dev
Go to the latest commit in origin/devPut on top everything that mybranch has, but origin/dev doesnt
Right questions: What exactly gets rebased?Hint 5:What is a rebase of public commits?Simple case: mybranch has been based on origin/dev -> OK
ABCorigin/devDEFmybranchHint 5:What is a rebase of public commits?First bad case: mybranch has been based on origin/fixes-for-dev
B is rebased on origin/dev but is already publicABorigin/fixes-for-devCDmybranchorigin/devEHint 5:What is a rebase of public commits?Second bad case : Rebase after merge
simplified graphB is again put on dev (but B is already public on f4d)ABorigin/fixes-for-devDmybranchorigin/devCHint 6:Recover from unintended situationsuse local branches as markersif you are not sure about the upcoming operationgit branch save_me
to easily get back to states
mark important commitsHint 6:Recover from unintended situationsgit reflog: history of your local actions e.g. with commit hashes
fc2180e HEAD@{0}: checkout: moving from secondbranch to master
28e2275 HEAD@{1}: commit: second commit
fc2180e HEAD@{2}: checkout: moving from master to secondbranch
Hint 6:Recover from unintended situationsgit reflog g
history of local actions for a certain local branchHint 6:Recover from unintended situationsIf you think you did something wrongsave console log to have the command history and outputfind important commits (gitk / reflog / cons. log)mark important commitstemporary commit dirty filesthink about the next actionsin most situations a fresh branch and cherry-picking your marked local commits is the most efficient way
This usually only takes some minutes.
Hint 7:Cherry-Picking done rightAvoid cross-branch cherry-picking of public commits
Potential conflicts have to be solved upstream
Only push one commit when doing local cherry pickingnot the original one AND the picked oneHint 8:Getting your changes in quicklyfetch and rebase before pushthis reduces the merge pathavoids conflicts while merging in changes
use commands provided by Gerrit Webinterface (checkout, cherry-pick)
build and test locally before pushingHint 9:What is local, what is remote?remote (porcelain) commands:clonefetchpushpulllocal (porcelain) commands:everything else (commit, add, rebase, merge, status, checkout, )Hint 10:Do not use pull!Pull isfetch and merge
If you rebase afterwards:potential rebase of public commits
Just dont use it in our infrastructureQ & A
Recommended