How-to use Git integration

This article details how to enable Git version control for WATO within Checkmk.

LAST TESTED ON CHECKMK 2.3.0P1

Table of Contents

Step-by-step guide

  1. Go to Setup → General → Global Settings → Edit global setting

  2. Place an X to enable GIT version control
    Screenshot displaying the location of enable GIT version control within global settings
    .
  3. Click Save
    .
  4. Activate changes

Git ignore

By default, Checkmk creates the following .gitignore files:

  • ~/etc/check_mk/.gitignore

    ~/etc/check_mk/.gitignore
    OMD[mysite]:~/etc/check_mk$ cat .gitignore
    
    # This file is under control of Checkmk. Please don't modify it.
    # Your changes will be overwritten.
    
    *
    !*.d
    !.gitignore
    *swp
    *.mk.new


  • ~/etc/check_mk/conf.d/.gitignore

    ~/etc/check_mk/conf.d/.gitignore
    OMD[mysite]:~/etc/check_mk/conf.d$ cat .gitignore
    
    *
    !wato


  • ~/etc/check_mk/conf.d/wato/.gitignore

    ~/etc/check_mk/conf.d/wato/.gitignore
    OMD[mysite]:~/etc/check_mk/conf.d/wato$ cat .gitignore
    
    !*

Push to a remote repository


  1. Add repository

    OMD[mysite]:~/etc/check_mk$ git remote add origin https://github.com/mygituser/test.git

    .

  2. Setup the upstream branch

    OMD[mysite]:~/etc/check_mk$ git push --set-upstream origin mybranchname

    .

  3. Verify the git configuration

    OMD[mysite]:~/etc/check_mk$ cat ~/etc/check_mk/.git/config [core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    [user]
    email = check_mk
    name = check_mk
    
    [remote "origin"]
    url = https://github.com/mygituser/test.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    
    [branch "mybranchname"]
    remote = origin
    merge = refs/heads/mybranchname

    .

  4. Push local configuration to mybranchname

    OMD[mysite]:~/etc/check_mk$ git push --set-upstream origin mybranchnameUsername for 'https://github.com': mygituser
    Password for 'https://mygituser@github.com': Enumerating objects: 65, done.
    Counting objects: 100% (65/65), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (55/55), done.
    Writing objects: 100% (65/65), 30.69 KiB | 10.23 MiB/s, done.
    Total 65 (delta 6), reused 39 (delta 1), pack-reused 0
    remote: Resolving deltas: 100% (6/6), done.
    To https://github.com/mygituser/test.git
    * [new branch] mybranchname -> mybranchname
    Branch 'mybranchname' set up to track remote branch 'mybranchname' from 'origin'.

    .


    Screenshot of GIT repository for testing


Automate Git push

If you would like to automate Git push, you can do this with a Git post commit hook

  1. Create the post-commit file

    OMD[mysite]:~$ vi ~/etc/check_mk/.git/hooks/post-commit

    .

  2. Add the following

    ~/etc/check_mk/.git/hooks/post-commit
    #!/bin/sh
    git push -u origin mybranchname

    .

  3. Save the file
    .
  4. Make the file executable

    OMD[mysite]:~$ chmod +x ~/etc/check_mk/.git/hooks/post-commit

Useful commands

  • Show changes before commit

    git diff

    .

  • View system status

    git status

    .

  • Automatically record changed files

    git add . -A

    .

  • Transfer all files to the repository

    git commit -am "There is a comment here"

    .

  • This command shows what has happened so far

    git log

    .

  • Write all recent changes to a file
    git format-path -1

    .

  • Which files were changed?

    git diff --stat

    .

  • Apply patch files from other participants: First only in test mode, then really:

    git apply --check patchdate
    
    git apply patchdate

    .

  • Create a directory and fetch repository (e.g., via https):

    mkdir gitlocal ; cd gitlocal
    
    git clone 'https://user@server.de/repository'

    .

  • Create a local branch

    git branch mybranch

    .

  • Switch to a local branch

    git checkout mybranch

    .

  • Show Branches

    git branch

    .

  • Back to mybranchname and pick up changes

    git checkout mybranchname
    
    git pull

    .

  • Apply changes to the mybranchname branch

    git checkout mybranch
    
    git rebase master

    .

  • Transfer changes to the remote repository

    git push origin mybranch:master

    .

  • Customize your shell prompt always to show the branch you are working on. Add Git additional information

    GIT_PS1_SHOWDIRTYSTATE=1
    GIT_PS1_SHOWSTASHSTATE=1
    GIT_PS1_SHOWUNTRACKEDFILES=1
    GIT_PS1_SHOWUPSTREAM="legacy verbose git"
    export PS1='\u@\h:\w$(__git_ps1 " (%s)")\$ '

    .

  • If you get an error message saying that git_ps1 cannot be executed, add the following line to the .bashrc:

    . /etc/bash_completion.d/git