Compiling and using libgit2

How to build libgit2 from source, install it on your computer, and use it in a project without linker errors.