These are some git tips that I’ve noticed that, while some seem obvious, a normal git user might easily not know them. There is so much to know about Git that is bond to happen. If you have any tip of your own that is extremely useful, reach out through Twitter, I’m always happy to learn new tricks.

Alias

Find most used commands and set an Git alias. I’m someone who mostly use Git through CLI and finding my most used commands and shortening is big a time saver. It was thanks to the blog post that I share as a reference and that I advice you to check it how, but I’ll leave here what I usually do.

First you check the top 10 most used Git commands with the following command:

history -n | grep git | cut -d' ' -f -3 | sort | uniq -c | sort -k1,1nr -k2 | head -n 10

With the commands identified, lets take the example `git checkout develop`.

git config --global alias.gcd checkout develop

Now instead of writing `git checkout develop` you just write `gcd`

reference:

–autostash

If you ever find yourself in a situation that you need to stash before pulling `–auto-stash` is going to be your favorite flag to make the process faster.

Before

git stash
git pull
git stash pop

After

git pull --autostash

reference:

Hooks

Using Git hooks isn’t as hard as one might think, all you need is a script to run and attach it to your Git hook. Let me tell you how I use it. Currently I work with Flutter, and even though my IDE does run a formatter, some files might not be caught and also, I use a lot of `logger.wtf(…)` that I never wish to commit to the repository. For that reason I created a pre-commit hook where I run dartfmt and check for any `logger.wtf` in the files. Everytime I do a commit.

# This isn't running 'make fmt' since we normally run "git commit" in other folder beside the project root
echo "🔍 Running formatting..."
FORMATTING=$(dartfmt -n .)
if [ ! -z $FORMATTING ]; then
	echo "🛑 COMMIT REJECTED found unformatted files. Please run 'make fmt' in project root"
	exit 1
fi

echo "🔍 Checking for 'wtf' logs..."
FILES_PATTERN='\.(dart)(\..+)?$'
FORBIDDEN='logger\.wtf'
git diff --cached --name-only | \
	grep -E $FILES_PATTERN | \
	GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $FORBIDDEN && echo '🛑 COMMIT REJECTED found "logger.wtf" references. Please remove them before commiting' && exit 1

echo "✅ Checks passed. Commiting..."
exit 0

With this script somewhere saved in your disk, lets assume `~/bin/your_pre_commit`, you now can create a symbolic link between your script file and a new file called `pre-commit` and add it to your project like so: `ln -s ~/bin/your_pre_commit <YOUR_PROJECT>/.git/hooks/pre-commit`

There are plenty of other Git hooks, you could for example only run this script before a Git push. But for that I recommend you check the Git online Documentation.

reference:

Worktree

One of my earliest discoveries and I confess it was thanks to a Guido’s Tweet that I got to know about it and start learning how to use it.

Working on a feature but they ask you for a quick fix in a different branch? Add a subtree! When before I would probably checkout to develop, then create a new branch, and work there, meanwhile I could forget that I had a stash and lose work, it happened more than once for me and it was infurianting.

Now I simply do something like following (example from git-scm):

$ git worktree add -b emergency-fix ../temp master
$ pushd ../temp
# ... hack hack hack ...
$ git commit -a -m 'emergency fix for boss'
$ popd
$ git worktree remove ../temp

When you get the hang of it, its a lifesaver for quick changes of the repository.

reference:

–force-with-lease

If you are like me, you commit often and small, you keep your branch updated at the repository, and your branch updated with develop, so that you can minimize any conflits resolution in the future. Now, after a rebase, did you know that you don’t need to do a `git push -f`? Well, you can but there is a safer way. Use the flag `–force-with-lease`. –force-with-lease It ignores the rule of Git to update a remote branch that is not an ancestor, while -f (force), will do the same but can make you lose commits and work.

references:

Last Updated: <2021-05-20 Thu>