Skip to content

Instantly share code, notes, and snippets.

@AdrienHorgnies
Last active February 11, 2024 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdrienHorgnies/23775549132721cfe92fc4dd60dadb50 to your computer and use it in GitHub Desktop.
Save AdrienHorgnies/23775549132721cfe92fc4dd60dadb50 to your computer and use it in GitHub Desktop.
Scripts to create basic branching cases any dev should learn to solve
#!/bin/bash
set -e
HERE=$(dirname $(realpath $0))
rm -rf local remote.git
git init --bare $HERE/remote.git
git clone $HERE/remote.git local
cd $HERE/local
git init
cat > txt <<EOF
1
2
3
EOF
git add txt
git commit -m "initial commit"
git push -u
sed -i 's/2/LOCAL-ONLY/g' txt
git add txt
git commit -m "2 becomes LOCAL-ONLY"
#!/bin/bash
set -e
HERE=$(dirname $(realpath $0))
rm -rf $HERE/local $HERE/remote.git
git init --bare $HERE/remote.git
git clone $HERE/remote.git local
cd $HERE/local
git init
cat > txt <<EOF
1
2
3
EOF
git add txt
git commit -m "initial commit"
git push -u
sed -i 's/2/REMOTE-ONLY/g' txt
git add txt
git commit -m "2 becomes REMOTE-ONLY"
git push
git reset --hard HEAD~
#!/bin/bash
set -e
HERE=$(dirname $(realpath $0))
rm -rf local remote.git
git init --bare $HERE/remote.git
git clone $HERE/remote.git local
cd $HERE/local
git init
cat > txt <<EOF
1
2
3
EOF
git add txt
git commit -m "initial commit"
git push -u
sed -i 's/2/REMOTE-ONLY/g' txt
git add txt
git commit -m "2 becomes REMOTE-ONLY"
git push
git reset --hard HEAD~
sed -i 's/2/LOCAL-ONLY/g' txt
git add txt
git commit -m "2 becomes LOCAL-ONLY"
#!/bin/bash
set -e
HERE=$(dirname $(realpath $0))
$HERE/status.sh
cd $HERE/local
ahead=$(git rev-list main ^origin/main --count)
late=$(git rev-list ^main origin/main --count)
if [ $ahead -gt 0 ] && [ $late -gt 0 ]; then
set +e
git merge origin/main --no-commit --no-ff
merge_status=$?
set -e
git merge --abort
if [ $merge_status -eq 0 ]; then
git rebase origin/main
git push origin main
fi
elif [ $ahead -gt 0 ]; then
git push origin main
elif [ $late -gt 0 ]; then
git rebase origin/main
fi
$HERE/status.sh
HERE=$(dirname $(realpath $0))
GD=$HERE/local/.git
ahead=$(git --git-dir=$GD rev-list main ^origin/main --count)
late=$(git --git-dir=$GD rev-list ^main origin/main --count)
if [ $ahead -gt 0 ] && [ $late -gt 0 ]; then
echo " out of sync"
elif [ $ahead -gt 0 ]; then
echo " local ahead of remote"
elif [ $late -gt 0 ]; then
echo " local behind remote"
else
echo " sync"
fi
#!/bin/bash
set -e
HERE=$(dirname $(realpath $0))
rm -rf local remote.git
git init --bare $HERE/remote.git
git clone $HERE/remote.git local
cd $HERE/local
git init
cat > txt <<EOF
1
2
3
EOF
git add txt
git commit -m "initial commit"
git push -u
#!/bin/bash
set -e
HERE=$(dirname $(realpath $0))
rm -rf local remote.git
git init --bare $HERE/remote.git
git clone $HERE/remote.git local
cd $HERE/local
git init
cat > txt <<EOF
1
2
3
EOF
git add txt
git commit -m "initial commit"
git push -u
sed -i 's/1/REMOTE-ONLY/g' txt
git add txt
git commit -m "1 becomes REMOTE-ONLY"
git push
git reset --hard HEAD~
sed -i 's/3/LOCAL-ONLY/g' txt
git add txt
git commit -m "3 becomes LOCAL-ONLY"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment