#TIL : Running git command using another ssh key

Sometimes you want to use another private key to authorize to remote repository.

Just add an environment variable before the command you wanna run : GIT_SSH_COMMAND='ssh -i [your-private-key]

Example :

1
$ GIT_SSH_COMMAND='ssh -i ~/keys/key1' git pull

#TIL : Put .git data outside project directory

Sometimes you want to put .git data into outside directory (to use another disk partition or to protect your git data). Use --separate-git-dir= option to get that.

Example :

1
$ git init --separate-git-dir=/var/gitstorage/myproject

Bonus : to protect .git data from other users, use this option --shared within octal value (same to chmod)

Example : this will protect git file from writing by group and reading/writing by others

1
$ git init --separate-git-dir=/var/gitstorage/myproject --shared=0640

#TIL : Fastly conflict detector script

Last month, I built a CI solution for our project and adding a conflict detector to our build commands. This script runned so slow because it will check all application files (and our application codebase has many of css, js files).

This was the script

1
2
3
4
5
6
7
8
9
#!/bin/bash

grep -rli --exclude=conflict_detector.sh --exclude-dir={.git,vendor,node_modules} "<<<<<<< HEAD" .

if [ $? -eq 0 ]; then
exit 1
else
exit 0
fi

Today, I think why don’t we just check recently updated files (in the latest commit) ??? Then I have this new script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash

# New way :D
CHANGED_FILES=$(git log --pretty=format: --name-only HEAD^..HEAD | sort | uniq)

for f in $CHANGED_FILES
do
if grep --exclude=conflict_detector.sh -q "<<<<<<< HEAD" $f
then
exit 1
fi
done

exit 0

conflict_detector.sh is the filename of script, we exclude it from check to make sure changing this file doesn’t make it failed.

You can use this approach to check linter, coding standard or run preprocessor ;)

Result (in my context) :

  • Old script : 12 seconds
  • New script : ~ 50ms (200 times faster)

be Automated, be Fast, but be Careful !

#TIL : using git hooks to improve working flow

We can improve our team workflow by defining some git hooks that trigger on specified events.
You can read all events and their usecases here : https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks

This is what I implemented to my today-i-learned repo. I used pre-commit to update Table of Contents in the README.md file, so every content in my repo will be updated on Github repo page.

1
$ ln pre-commit .git/hooks/pre-commit

pre-commit file :

1
2
3
4
5
6
#!/bin/sh

echo 'Running pre-commit hook'

python til_update_readme.py
git add README.md

So it will run a Python script that update new TOC and then add the file to git.

Automation ! Automation ! AND …. Automation !!! 🤖

#TIL : Resolving conflict like a boss

When using git merge new branch to old branch, you just want use all ours or theirs version but be lazy to update every conflicted file.

1
grep -lr '<<<<<<<' . | xargs git checkout --ours

Or

1
grep -lr '<<<<<<<' . | xargs git checkout --theirs

Explain : these commands will find any file contains <<<<<<< string (conflicted file) and run git checkout --[side]