DeskTux

Linux on Desktops

User Tools

Site Tools


apps:git

git

A (very) basic git server and repository can be handy for a couple of reasons. I personally use it for Puppet code and some scripting.

Installation & Configuration

To install git, just run a basic apt install git and you are good to go. No configuration needed.

Set up a repository

  1. Create a directory for your repositories, e.g. /srv/git and make sure your git users will be able to write into it. There are basically two approaches to accomplish that, one is to create a 'git' user who will receive the public SSH key of all your users and will be used by all of them and the other is to create a 'git' group that will grant write access to that directory. Preferably you should use ACL's for that to make sure local changes or umasks cannot get into your way, but that is of course completely up to you.
  2. Next, create a project directory. I will use Puppet as an example here, so create a directory for that:
    mkdir -p /srv/git/puppet.git
  3. Change to that directory and initialize your git repository:
    cd /srv/git/puppet.git; git init
  4. Give your project a meaningful description:1)
    vi .git/description
  5. Configure the basics of the git client:
    git config --global user.name "Your Name"
    git config --global user.email you@example.com
    git config --global credential.helper store
    git config --global push.default simple
    git config --global color.ui true
  6. Create a README and make the initial commit:
    touch README.md; git add .; git commit -m "Initialize Repository"

Now you are good to go to use this repo from a remote computer that has SSH access to this system.

Usage from a client

One time only steps on the client

  1. First, install git on your client as well:
    apt install git
  2. Make the basic configuration to also set your name and e-mail address (see above, “Configure the basics of the git client”).
  3. Clone the Repository:
    git clone ssh://user@server.tld/srv/git/puppet.git
  4. Switch to the newly created directory containing the project data:
    cd puppet

Client steps for every change

  1. Update the local repository:
    git pull
  2. Create a new branch:
    git checkout -b mybranch
  3. Make your changes, creating and editing files, creating directories.
  4. Parse and lint your code.
  5. Add your changes to git (do this from the top directory, or add all files/directories individually):
    git add .
  6. Commit your changes and supply a meaningful commit message:
    git commit -m "My meaningful change description"
  7. Upload the changes to a new branch in the git server:
    git push -u origin mybranch

Server steps for every change

  1. Log in to your server as either root or git user (depending on the setup you chose above)
  2. Switch to the project directory:
    cd /srv/git/puppet.git
  3. Show all branches:
    git branch
  4. Identify the branch you want to merge into the master branch and merge it:
    git merge mybranch
  5. Delete that branch:
    git branch -D mybranch

Final steps on the client (clean up)

  1. Switch back to the master branch:
    git checkout master
  2. Delete your development branch:
    git branch -D mybranch
  3. Update all local data:
    git pull; git fetch --prune

Troubleshooting/Q&A

  • If you ever wonder what branch you are on, you can just run a git status to check that, or just use my ZSH configuration which will show the current branch.
  • If you use this for Puppet code, you could just create a symlink from the Puppet code directory to your git project:2)
    ln -sf /srv/git/puppet.git /etc/puppet/code
  • In case you forgot to delete your branch on the server, or want to just delete it remotely, you can do that:
    git push -u origin mybranch --delete
  • If you already started editing but forgot to switch to a new development branch, in many cases it is still possible to just switch to a new branch after you made changes already and just follow the workflow.
  • In case the local (master) branch is no longer usable, use these steps to reset it:
    git fetch origin master
    git reset --hard FETCH_HEAD
    git clean -df

What next?

Actually, stuff like parsing & linting, merging on the server and deploying your code where it belongs (e.g. Puppet) should be automated. A basic git setup like this doesn't allow for this, unless you throw some serious scripting at it. If you want pipelines and e.g. Jenkins, you need tools like GitHub.

Unfortunately, they do not offer an open-source version of their software that can be self-hosted, just hosted by them or in an Enterprise Edition. Luckily, there is GitLab that provides all of this functionality. And: it will run on Debian :-) So if you want or need pipelines and user/privilege management etc, you should check out GitLab.

1)
Use your preferred editor, I just like vi
2)
Providing you used the same structure as mandatory in the Puppet code directory. Don't forget to backup first!

Discussion

Enter your comment:
K H H X P
 
apps/git.txt · Last modified: 2024-05-20 13:50 by jens