Craft with git - GradedJestRisk/git-training GitHub Wiki

Table of Contents

Checklist

A summary of Leo Jacquemin blog post on git proper use

Summary:

  • Green branches: all commits should pass the tests
  • Commit metadata: 50 char messages, description body to capture context
  • Commit boundaries: typology, order and atomicity
  • Branch status: always up to date with the development branch
  • Code churn: use interactive rebasing to eliminate the churn before sharing
  • More rebasing: useful tips during interactive rebases
  • Unresolved: code review commits

Green branch

Always commit with all tests passing.

To check afterwards, or after a rebase, use git --exec / -x: git rebase HEAD~3 --exec "npm run test"

Try it (and fix typo in commit message):

  • download this folder
  • install dependencies : npm install
  • run test to check last commit is green: npm test
  • run interactive rebase with tests: git rebase HEAD~3 -ix "npm run test"
Rebase will fail at second step with the following message:
Executing: npm run --prefix (..)  
> [email protected] test 
> mocha
  ✓ pass
  ✓ also pass
  2 passing (3ms)

Executing: npm run --prefix (..) 
> [email protected] test 
> mocha
  ✓ pass
  ✓ also pass

  1) fails ! red commit !:

      AssertionError: expected true to be false
      + expected - actual

      -true
      +false
      
      at Context.<anonymous> (test.js:15:18)
      at processImmediate (internal/timers.js:456:21)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test: `mocha`
npm ERR! Exit status 1
(..)
You can fix the problem, and then run
  git rebase --continue

Branch name

Enforce with hooks

A bash script triggered by a pre-commit hook can cover the most basic needs. You'll just need to write regular expressions hooks

In case you're having hard time with regular expressions, you can give them a try online before

A sandbox is available here

Commit message

Best practice

Compose with commitizen

2 npm modules:

Install:
  • install commitizen : npm install --save-dev commitizen
  • install conventional template npx commitizen init cz-conventional-changelog --save-dev --save-exact
  • make some changes: touch foo.bar
  • start commitizer npx cz-git or ./node_modules/.bin/git-cz
  • install customizable template: npm install --save-dev cz-customizable
  • in package.json, update commitizen config path section to match this one:
  "config": {
    "commitizen": {
      "path": "node_modules/cz-customizable"
    }
  },
  • copy official template in project root
  • start commitizer npx cz-git
  • make a copy: cp cz-config-EXAMPLE.js .cz-config.js (note the dotfile)
  • hack your changes in .cz-config.js, sample here
  • make a change echo "baz">> foo.bar && git add foo.bar
  • start commitizer npx cz-git
  • ask the questions till you get
###--------------------------------------------------------###
[feat](Front-end) add implicit user access renewal

According to Oauth2 guidelines
https://www.oauth.com/oauth2-servers/access-tokens/refreshing-access-tokens/
###--------------------------------------------------------###
Commit the changes now ? (Yneh) 
  • answer Y (Yes); e (edit) would redirect you to git's default editor
  • check the commit message: git log
  • if happy, add commitizen badge to you README:
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

A sandbox module can be found here

Enforce with commitlint

Install

A sandbox is available here

Custom commit enforcing

You can write your own hooks in js, see here.

You can then:

  • lint branch name
  • lint commit message
  • check for unusual committed files
    • IDE folders
    • package.json, package-lock.json
    • temporary files (.swp)
  • prevent pushing on reserved branches, eg master
  • warn about commit size
    • too much files
    • file are too big

Analyze repo

From Your code as a crime scene book

List:

Analyze code churn:
  • show log with relative HEAD
    • 10 entries only git log --oneline | nl -v0 | sed 's/^ \+/&HEAD~/'
    • N entries: git log --first-parent --pretty="%s" -20 | nl -v 0 | sed 's/\([0-9].*$\)/HEAD~\1/'
  • get all files changed in a range git diff --name-only HEAD~10 HEAD~5
  • get commit count on a file:
    • since repo origin git log --format=oneline <FILE> | wc -l
    • in a commit range git log REF1..REF2 --format=oneline <FILE> | wc -l
⚠️ **GitHub.com Fallback** ⚠️