Tag Archives: code

GIT tricks when you mess up a bit

Sometimes, especially at the beginning, a Developer makes loads of mistakes and here there are some workaround that can help. 😉

Rewrite master’s history (TO AVOID)

git log --pretty=oneline

git rebase -i --root => s/pick/s => choose what to keep ‘pick‘ and what to remove ‘s

-> for each ‘pick’ you need to write a summary in the comments

Rewrite history of the current branch (squash)

Firstly, check how many ahead/behind is your branch comparing to the reference one (e.g. master/main)


git rebase -i HEAD~_number of commits_ (e.g. git rebase -i HEAD~3) => use the number of “ahead’s” that you found.

Make the required changes (pick/s) and save.

git push origin +mybranch


git rebase
Any conflicts?

YES -> fix them, git add . , git rebase --continue
NO -> git push + (forced push using +)

Fix a wrong merge on master

git revert -m

NOTE: merge commit does NOT count.

A -\
B -/

Here an example.
You want to go back to commit B (master).
A, B, K, L are commit ID – you can find them using git log command.

From B to A (A is the last merge that we want to delete), there are 2 commits, K and L.

If you want to go back to B:

git revert -m2 A
which means – go back of 2 commits from A

Happy fixing! 🙂

Auto deploy from BitBucket repository via Cron

Setup the environment

Create the environment for Apache

mkdir /home/deployment
chown apache:apache /home/deployment

Create ssh key in /home/deployment/.ssh/id_rsa

Create wrapper for git to auth via SSH keys

cat > /home/deployment/git.sh <<EOF

if [ $# -eq 0 ]; then
    echo "Git wrapper script that can specify an ssh-key file
    git.sh -i ssh-key-file git-command
    exit 1

# remove temporary file on exit
trap 'rm -f /home/deployment/.git_ssh.$$' 0

if [ "$1" = "-i" ]; then
    SSH_KEY=$2; shift; shift
    echo "ssh -o StrictHostKeyChecking=no -i $SSH_KEY \$@" > /home/deployment/.git_ssh.$$
    chmod +x /home/deployment/.git_ssh.$$
    export GIT_SSH=/home/deployment/.git_ssh.$$

# in case the git command is repeated
[ "$1" = "git" ] && shift

# Run the git command
git "$@"

Run the fist clone

~# su - apache -s /bin/bash
Last login: Tue Apr  5 16:03:53 BST 2016 on pts/0
-bash-4.2$ cd /var/www/vhosts/

-bash-4.2$ /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa clone [email protected]:repository01.git

Test the Re-Base

Make sure to get into the folder with .git subfolder
(in this case we did the clone into /var/www/vhosts/ BUT the code is stored into a new subfolder pulled with the domain name. So we cd into that one)

-bash-4.2$ cd /var/www/vhosts/blog.com && /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa pull --rebase
Could not create directory '/usr/share/httpd/.ssh'.
Failed to add the host to the list of known hosts (/usr/share/httpd/.ssh/known_hosts).
Current branch master is up to date.

Install the CRON

echo "*/5 * * * * apache cd /var/www/vhosts/blog.com && /home/deployment/git.sh -i /home/deployment/.ssh/id_rsa pull --rebase > /dev/null 2>&1" >> /etc/crontab