10+ things I always setup in git when I prepare a new environment
Tags: git, tips
Published: 2022-07-29 02:02:48

---
title: 10+ things I always setup in git when I prepare a new environment
published: true
description: When you buy a car or a bicycle, you first adjust the seat position and saddle height to suit your body size.　It is the same with git configuration.
tags: git, tips
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6j29uyhu1j891iaprlx4.jpg
# Use a ratio of 100:42 for best results.
---

When you buy a car or a bicycle, you first adjust the seat position and saddle height to suit your body size.　It is the same with git configuration.

In this article, I will share the git setup I use all the time.

#### User name & Mail address
```
git config --global user.name "<name>" && \
git config --global user.email "<email>"
```
- Replace `<name>` with my name and `<email>` with my mail address.

####Alias for existing command
```
git config --global alias.co checkout
git config --global alias.st status
git config --global alias.br branch
```

####Global ignore setting
```
git config --global core.excludesfile ~/.gitignore_global
```
- This allows you to put your own specific ignore settings in `~/.gitignore_global` in addition to the `.gitignore` for each project, which will be applied to all git operations across all projects. 

####Push only the branch you are now working on
```
git config --global push.default simple
```

####Make `--rebase` the default behavior during git pull
```
git config --global pull.rebase true
```
- This prevents unintentional creation of a merge commit if the branch being pulled has been modified locally.

####Make `--prune` the default behavior during git fetch
```
git config --global fetch.prune true
```
- This will remove local branches that were deleted remotely when `git fetch` or `git pull` was performed.

####Set the width of indentation for tab characters
```
git config --global core.pager 'less -x4'
```
- In this example, the pager (`less` command) option specifies tab indent width as 4

####Use `nvim` as the editor to be used when committing
```
git config --global core.editor 'nvim'
```
- I use several different text editors for different purposes, but I prefer to use `nvim` with `git commit`.

####Do not fast-forward when merging
```
git config --global --add merge.ff false
git config --global --add pull.ff only
```
- Fast-forward merging makes it difficult to follow the history of work on a branch. Therefore, avoid unintentional fast-forwards when merging.
- However, fast-forwarding is not a problem for `git pull` cases in most cases [^1], so we enforce fast-forwarding in the case of pulls.
- See also: [gitのmerge --no-ff のススメ - Qiita](https://qiita.com/nog/items/c79469afbf3e632f10a1) 

[^1]: In my experience, this happens when you are temporarily changing code while reviewing a PullRequest created by someone else in your local environment. In that case, fast-forward is better because local changes are gathered at the top of the history, making it easier to work with when you finally undo the changes.

####Output line numbers in the result of `git grep`
```
git config --global grep.lineNumber true
```

####Visualize differences in whitespace (including newline codes)
```
git config diff.wsErrorHighlight all
```

---
**EDIT: 2022-07-31**

## dotfile

As mentioned in the comments, these are stored in `.gitconfig`. And I have added these settings to the dotfiles repository.

https://github.com/snaka/my-dotfiles/blob/master/.gitconfig

Thanks to all who commented.

---

## See also

- [Alias to remove deleted branches in a git remote at once](https://dev.to/snaka/alias-to-remove-deleted-branches-in-a-git-remote-at-once-2h72)