git is not only good for crowds of developers having to organize and sync “status quo” of the project over various devices,

users also use it for version control of documents and various other files (anything where organized cooperation between multiple users/developers/designers/content creators is important).

git over ssh is THE IDEAL combination because

  • does DELTA transfer meaning: small changes to big files can be transferred/synced fast (faster internet/more bandwidth is always a plus) (so in theory even large movie.mp4 and sound.wav files should be possible)
  • ssh enables secure encrypted transfer of the files

given that the requirements:

free -m; # superbly efficient
              total        used        free      shared  buff/cache   available
Mem:            231          65          30           2         135         154
Swap:          1020           0        1020

are already in place (https://dwaves.org/2017/05/05/linux-ssh-generate-public-private-keys/)

one can setup one’s own git repository in 10min:

# tested on
hostnamectl; # client
   Static hostname: client
         Icon name: computer-desktop
           Chassis: desktop
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-8-amd64
      Architecture: x86-64

hostnamectl; # server
   Static hostname: git
         Icon name: computer-vm
           Chassis: vm
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-8-amd64
      Architecture: x86-64

# for conveniance an ll alias is recommended
# create ll alias to nicely display folder contents
alias ll='ls -lah --color --time-style=+%F'
# add this line to /etc/bashrc or /etc/bash.bashrc

# ===== on server =====
# create repository directory (where to store project on server)
mkdir -p ~/projects/test.git
cd ~/projects/test.git

# initialize the server folder as git repository
git init --bare

# ===== on client =====
# create repository directory (where to store project on client)
mkdir -p ~/projects/test.git
cd ~/projects/test.git

# initialize the client folder as git repository
git init

# create some test data
echo -e "this is a test git repository created on $(date '+%Y-%m-%d-%H:%M:%S')" > README.txt;

# make git realize there are new files
git add .

# create a commit
git commit -m "added new file README.txt" -a

# connect the server git repository to client
# 1234 is the port ssh is running on
git remote add origin ssh://user@server.com:1234/home/user/projects/test.git

# push/sync local changes to server
git push origin master

### HURRAY! :) ONE JUST CREATED A NEW GIT REPO!
# AND SYNCED CHANGES TO IT! :)
# CONGRATULATIONS ALL INVOLVED!

# now let's see if one can pull the changes
# (maybe on a different machine?
# but can also use same machine for testing)

# create new test dir
mkdir -p ~/projects/test.clone.git
cd ~/projects/test.clone.git

# pull repo to local folder (folder test will be automatically created)
git clone ssh://user@server.com:1234/home/user/projects/test.git

Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

# let's see what one got
ll

total 28K
drwxr-xr-x   3 user user 4.0K Apr 25 13:29 .
drwxr-xr-x 357 user user  20K Apr 25 13:27 ..
drwxr-xr-x   3 user user 4.0K Apr 25 13:29 test

ll test
total 16K
drwxr-xr-x 3 user user 4.0K 2020-04-25 .
drwxr-xr-x 3 user user 4.0K 2020-04-25 ..
drwxr-xr-x 8 user user 4.0K 2020-04-25 .git
-rw-r--r-- 1 user user   52 2020-04-25 README.txt

cat ./test/README.txt
this is a test git repository created on 2020-04-25

### HURRAY! one just successfully pulled the project from server to client :)

let’s script it:

its always more convenient, to just run /scripts/do_stuff.sh ProjectName

than to actually go through each and every necessary command

NOTE!

instead of ~/projects one has (unwisely) chosen /projects (subfolder of root) (please modify X-D)

server:

create new project script

vim /scripts/git_add_project.sh

#!/bin/bash
echo "===== create new dir for project ====="
mkdir -p ~/projects/$1

echo "===== change into that dir ====="
cd ~/projects/$1

echo "===== git init --bare ====="
git init --bare

client: (multiple scripts)

note: my project folder is /projects not /home/user/projects (which might be an better idea depending on how one runs backups)

add new project script

vim /scripts/git_add_project.sh

#!/bin/bash
echo "=== create repository directory (where to store project on client) ==="
mkdir -p /projects/$1
cd /projects/$1

echo "=== remove existing maybe old .git dir if present ==="
rm -rf .git

echo "=== initialize the client folder as git repository ==="
git init

echo "=== make git realize there are new files ==="
git add .

echo -e "this repository was created $(date '+%Y-%m-%d-%H:%M:%S')\n\r and this file is the first/initial commit" > README.txt

echo "=== create a commit ==="
git commit -m "first-initial commit, added README.txt" -a

echo "=== connect the server git repository to client ==="
git remote add origin ssh://user@server.com:sshport/home/user/projects/$1

echo "=== push/sync local changes to server ==="
git push origin master

upload/commit changes to server script:

# create some test changes
cd /projects/PROJECT_NAME
echo -e "\n\r this readme file was changed on: $(date '+%Y-%m-%d-%H:%M:%S')" >> README.txt;

# example usage:
/scripts/git_commit.sh PROJECT_NAME "commit message"

# contents:
vim /scripts/git_commit.sh 

#!/bin/bash
cd /projects/$1

echo "=== make git realize there are new files ==="
git add .

echo "=== add commit message ==="
git commit -m "$2" -a

echo "=== push/sync local changes to server ==="
git push origin master

git digital workflow in a nice chart:

Links:

https://www.linux.com/training-tutorials/how-run-your-own-git-server/

crash course – G.I.T – git the opensource sourcecode management system – by linus torvalds – tutorial getting started – FAST

GitHub.com cheat sheed

PS: as with all complex systems/tools, if not used on a daily basis, the usage could be forgotten and needs to be relearned.

but maybe one does not need ALL of it’s features from day one 😉

admin