Woovi Git Best Practices
Tags: git, github, management, webdev
Published: 2022-11-10 17:04:35

We seek to follow a culture of good practices related to our development process with git.

Below you will find some of the most used practices.

## Git message

Follow these 7 rules:

1. Separate the head from the body with a blank line
1. Limit the headline to 50 characters
1. Do not end the headline with a period
1. Use the imperative mood in the headline (example: "Create" instead of "Created")
1. Wrap the body at 72 characters
1. Use the body to explain what and why vs. how

Really good article about those rules: https://chris.beams.io/posts/git-commit/

### Commitlint
Some of our projects uses [commitlint](https://github.com/marionebl/commitlint) with the [conventional](https://github.com/marionebl/commitlint/tree/master/@commitlint/config-conventional) config.

This means you need to create commit messages using the following format:

```
type(subject): commit message
```

Keep in mind this will go to the changelog, so create a concise commit message related to your changes, remember that you can always use the commit message body to say more details about your changes:

```
type(subject): commit message

some long commit body
```

Usually to see available types, check the `commitlint.config.js` file on the root of your project, the `type-enum` key.

Some scenarios:
Fixed some UI on User Evolution on Woovi

```
style(user-evolution): fix the wrong color on feedback received chart
```

Fixed some wrong code on groups that were causing form submissions to always return errors:

```
fix(groups): add new Group form checking from the invalid return from mutation

Was checking for mutation result BlaBlaResult, instead of BlaResult.
```

Since `fix(subject)` already says it's a fix, you don't need to start the commit message with `fix blah`.

Another scenario is updating schemas, trying to update schemas as a separate commit, and using the commit message:

```
chore(schemas): update schemas
```

## Branch naming

Try to stick to the format `[kind-of-change]/[what-is-being-changed]` whenever possible.

Example: `fix/user-name-during-login`, `improvement/login-screen`, `feature/charge-customer-screen`.

## Pull Requests

To modify anything on `main` or `production`, always open a PR first.

Many approvals, from Woovi devs, are required to merge a PR:

| Branch       | Approvals            |
| ------------ | -------------------- |
| `main`     | 1 :white_check_mark: |
| `production` | 2 :white_check_mark: |

## Useful git commands

### Remove all merged branches from a local repo

```
git branch --merged | grep -v "\*" | grep -v master | xargs -n 1 git branch -d
```

Or if you want to just remove the references to remote when they are deleted

```
git config --global fetch.prune true
```

#### Remove all tags that are not on remote

```
git tag -l | xargs git tag -d
git fetch --tags
```

#### Autocorrect git

```
git config --global help.autocorrect 1
```

#### Remove multiple files after deleting them from the disk

```
git ls-files --deleted -z | xargs -0 git rm
```

#### Git aliases

```
[alias]
    lg1 = log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
    lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
	lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
    co = checkout
    ec = config --global -e
    cob = checkout -b
    cm = !git add -A && git commit -m
    st = status
```

To add this alias to your git command line, copy and paste it to `~/.gitconfig`.

#### Apply the changes introduced by some existing commits: git cherry-pick

The main idea is to transfer a commit from branch A to branch B.

**When to use?**
Let's say you have committed to the wrong branch. A good way to copy and paste this commit to the right branch is to use `git cherry-pick`.

**How to use?**
First of all, run `git log` in the source branch to see the _commit sha1_. Then, switch to the destination branch and run the following:

```
git cherry-pick commit_sha1
```

Where `commit_sha1` is the sha1 string of the desired commit. If there aren't any conflicts, the changes will be applied.

---

[Woovi](https://woovi.com/) is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.

If you want to work with us, we are [hiring](https://woovi.com/jobs/)!

---

Photo by <a href="https://unsplash.com/@synkevych?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Roman Synkevych 🇺🇦</a> on <a href="https://unsplash.com/s/photos/git?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>