A Docker Image to Sync Git Repositories
When you work on the same repository on multiple computers it’s easy to forget to push code and leave some changes local. To avoid that I decided to develop a tiny project that loops through all the folders and pulls remote changes then commits and pushes all the local changes.
The script itself it very simple. It first pulls the latest and pushes the local changes:
Function Sync-Git {
git fetch --all
git pull
git add .
git commit -m 'git-sync'
git push
}
$rootFolder = '/rootFolder'
Write-Host "Changing directory to root folder: $rootFolder"
Set-Location $rootFolder
Get-Location | Write-Host
Get-ChildItem -Directory | ForEach-Object {
Set-Location -Path $_
Write-Host $_
Sync-Git
Set-Location $rootFolder
}
Write-Host "Done"
Dockerfile is based Microsoft’s Powershell image and it only needs to install Git on it:
FROM mcr.microsoft.com/powershell
RUN apt-get update && apt-get install -y git
RUN git config --global user.email "you@example.com"
RUN echo " IdentityFile ~/.ssh/id_rsa" >> /etc/ssh/ssh_config
RUN echo " StrictHostKeyChecking no" >> /etc/ssh/ssh_config
RUN mkdir /home/git-sync
WORKDIR /home/git-sync
COPY ./git-sync.ps1 .
ENTRYPOINT ["pwsh", "git-sync.ps1"]
Build it at the root folder:
docker build . -t {image name}
And run it as:
docker run --rm -d -v /host/path/to/root/git/folder:/rootFolder -v ~/.ssh:/root/.ssh:ro {image name}
Or pull my image from Docker Hub:
docker pull volkanpaksoy/git-sync
Issues
fatal: unable to auto-detect email address
GitHub didn’t allow me to push my changes without an email address so I had to add the command below to Dockerfile:
git config --global user.email "you@example.com"
It doesn’t look good maybe but I don’t care too much about email addresses in commits anyway so I’m fine with it.
“WARNING: UNPROTECTED PRIVATE KEY FILE!” error
This might also be an issue in which case the solution is simple (assuming the name of the private key file is id_rsa):
chmod 600 ~/.ssh/id_rsa
Bad configuration option
Make sure to have the following options in the config file in your .ssh directory:
Host *
StrictHostKeyChecking no
AddKeysToAgent yes
IgnoreUnknown UseKeychain
UseKeychain yes
IdentityFile ~/.ssh/id_rsa