Effective Git Practices for Collaborative Teamwork
Tags: git, devops, development
Published: 2023-06-02 12:21:31

Git best practices are essential, although they can differ across environments. In this context, I am presenting a set of Git best practices that you can adopt for your upcoming project.

## 1. Configure the commit authorship

Always set your name and email address correctly in order to use Git. This information will be attached to each commit you make.

```
git config --global user.name "Chris Markas Doe"
git config --global user.email "chris.markas@devoteam.com"
```

It is the only way for other developers to find you in case they have any questions regarding your changes.

## 2. Don’t git push straight to master. Branch it out

To facilitate independent work and minimize impact on the main source code, it is recommended for each team member to utilize individual feature branches.

This approach enables seamless tracking of changes made in these branches. Once the final code is deemed ready, it can be merged into the master branch. This ensures a controlled and organized integration of changes into the main codebase.

Run this command to create a new branch from master branch.

```
git checkout master
git checkout -b <branch_name>

```

When creating new branches, it is advisable to consider following Git branch best practices.

```
git branch <category/short-form-of-desc>
```

A git branch should start with a category. Pick one of these: `feature`, `bugfix`, `hotfix`, or `test`.

`feature` is for adding, refactoring or removing a feature

`bugfix` is for fixing a bug

`hotfix` is for changing code with a temporary solution and/or without following the usual process (usually because of an emergency)

`test` is for experimenting outside of an issue/ticket

## 3. Write short, detailed commit messages

Writing clear and meaningful commit messages is of utmost importance to comprehend the changes made in a specific commit. Thus, it is crucial to allocate sufficient time to craft concise, detailed, and purposeful commit messages.

Make sure that you provide enough detail to answer:

- Describe why a change is being made.
- How does it address the issue?
- What effects does the patch have?

## 4. Rebase your working branch frequently

To prevent bugs, unnecessary rework, and the laborious task of resolving conflicts with the upstream branch, it is advisable to regularly rebase your working branch. This can be easily achieved by executing the following commands:

```
git checkout <upstream_branch>
git pull
git checkout -
git rebase <upstream_branch>
```

In summary, this sequence of commands switches to a specified upstream branch, pulls the latest changes from the remote repository, switches back to the previous branch, and performs a rebase operation to incorporate the changes from the upstream branch into the current branch.

## 5. Squash commits before merging

When working on your feature branch, it's fine to add a commit for even minor changes. However, if every feature branch produced 50 commits, the resulting number of commits in the master branch could grow unnecessarily large as features are added. 

In general, there should only be one or a few commits added to the master from each feature branch.

```
git rebase -i HEAD~20 # look at up to 20 commits to consider squashing
git commit --amend
git push -f
```

## 6. Code reviews

Requesting feedback from others is a valuable approach for maintaining high code quality. Conducting code reviews serves as an effective means to evaluate whether a proposed solution addresses a problem in the most efficient manner. 

It is essential to involve individuals from different teams in code reviews, as certain sections of the codebase may require specialized domain knowledge or involve security considerations that go beyond the scope of an individual contributor's expertise.

### Frequently used git commands

```
git clone <repo_url>
git checkout master
git checkout -b <branch_name>
# Make the necessary code changes
git add <updated-file1> <updated-file2>
git commit -m "A good commit message"
git push origin <branch_name>
```