Table of Contents
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
- Create a directory for your repositories, e.g.
/srv/gitand 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.
- 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
- Change to that directory and initialize your git repository:
cd /srv/git/puppet.git; git init
- Give your project a meaningful description:1)
- Configure the basics of the git client:
git config --global user.name "Your Name" git config --global user.email firstname.lastname@example.org git config --global credential.helper store git config --global push.default simple git config --global color.ui true
- 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
- First, install git on your client as well:
apt install git
- Make the basic configuration to also set your name and e-mail address (see above, “Configure the basics of the git client”).
- Clone the Repository:
git clone ssh://email@example.com/srv/git/puppet.git
- Switch to the newly created directory containing the project data:
Client steps for every change
- Update the local repository:
- Create a new branch:
git checkout -b mybranch
- Make your changes, creating and editing files, creating directories.
- Parse and lint your code.
- Add your changes to git (do this from the top directory, or add all files/directories individually):
git add .
- Commit your changes and supply a meaningful commit message:
git commit -m "My meaningful change description"
- Upload the changes to a new branch in the git server:
git push -u origin mybranch
Server steps for every change
- Log in to your server as either root or git user (depending on the setup you chose above)
- Switch to the project directory:
- Show all branches:
- Identify the branch you want to merge into the master branch and merge it:
git merge mybranch
- Delete that branch:
git branch -D mybranch
Final steps on the client (clean up)
- Switch back to the master branch:
git checkout master
- Delete your development branch:
git branch -D mybranch
- Update all local data:
git pull; git fetch --prune
- If you ever wonder what branch you are on, you can just run a
git statusto check that, or just use my ZSH configuration which will show the current branch.
- 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
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, 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.