Skip to content
Snippets Groups Projects
Commit fa084f73 authored by Michael Blaschek's avatar Michael Blaschek :bicyclist:
Browse files

added git sync

parent 556ce266
Branches
Tags
No related merge requests found
# Git Tipps and Tools
There is a lot of ways how to use git and sometimes a working solution can be very helpful for everybody. So please share you experience and working solutions.
[[_TOC_]]
## HowTo Sync a GitHub and a GitLab repository
It is easy to import a GitHub repo into GitLab and the otherway around. However, if you want to make sure you can have both repos at the same state, you need to syncronize them.
How to call: `./git-repos-sync [URL1] [URL2] [Branch]`
This means:
- `URL1` - Address of the first remote repository
- `URL2` - Address of the second remote repository
- The order of `URL1` or `URL2` does not matter.
- `Branch` is usually `master`
Different use cases:
1. You have **already** a local copy of either of the repositories (e.g. GitLab or GitHub)
2. You have no local copy of either repository.
### Case 1
```bash
cd dir-of-repo
# copy the script there
wget
# execute the script
./git-repos-sync [URL] [URL] [Branch]
```
### Case 2
```bash
# Create a new folder to do the sync, can be any name
mkdir sync-repos
# copy the script there
wget
# execute the script
./git-repos-sync [URL] [URL] [Branch]
```
\ No newline at end of file
#!/bin/bash
# author: https://github.com/adidik/git-repos-sync
# sync two git repositories (github and gitlab)
# modified: 23.4.2021 (MB)
# License: MIT
if [ "$#" -ne 3 ]; then
>&2 echo "Usage: git-repos-sync <repository URL> <repository URL> <branch-to-sync>"
exit 1
fi
if [ ! -d ".git" ]; then
echo "Git repo for syncronization is not found, creating one..."
git init
git fetch $1
git checkout -b master FETCH_HEAD
echo
fi
if ! git diff-index --quiet HEAD --; then
>&2 echo "Local modifications found, looks like your in the conflict resolution. Resolve a conflict and commit. Then rerun script."
exit 1
fi
echo "Left: $1"
echo "Right: $2"
echo
echo "Fetch latest commits from branch $3 in $1"
if ! git fetch -u $1 $3:left/$3; then
>&2 echo "Fatal: unable to fetch from $1, rerun the script as soon as connnection restored."
exit 1
fi
echo "Fetch latest commits from branch $3 in $2"
if ! git fetch -u $2 $3:right/$3; then
>&2 echo "Fatal: unable to fetch from $2, rerun the script as soon as connnection restored."
exit 1
fi
if git checkout --quiet -b sync-$3 right/$3; then
echo "Merge branches from left and right if necessary."
if ! git merge -m "Merge to sync between $1 and $2" --log left/$3; then
>&2 echo "Merge conflict. Solve is manually, commit and rerun script."
exit 1
fi
else
echo "Rerun after merge conflict resolution or restored connection."
git checkout --quiet sync-$3
echo "Try to merge with right first"
if ! git merge -m "Merge to sync between $1 and $2" --log right/$3; then
>&2 echo "Merge conflict. Solve is manually, commit and rerun script."
exit 1
fi
if ! git merge -m "Merge to sync between $1 and $2" --log left/$3; then
>&2 echo "Merge conflict. Solve is manually, commit and rerun script."
exit 1
fi
fi
echo "Push merged changes in $3 to $2"
if ! git push $2 HEAD:$3; then
>&2 echo "Fatal: unable to push to $2, rerun the script as soon as connection restored."
exit 1
fi
echo "Push merged changes in $3 to $1"
if ! git push $1 HEAD:$3; then
>&2 echo "Fatal: unable to push to $1, rerun the script as soon as connection restored."
exit 1
fi
git checkout --quiet master
git branch -D --quiet sync-$3
echo "Done."
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment