9 tips for git
Tags: git, github, tips
Published: 2020-06-22 10:23:20

If you use Git on a daily basis, you know that this tool is full of commands and different configuration options. I suggest here to list some tips that I use.

## Configure your username and e-mail

```bash
$ git config --global user.name "Darth Vader"
$ git config --global user.email "darth-vader@deathstar.space"
```

I voluntarily start simple but it is a manipulation that happens to me quite often, my computer being used to manage several projects with different identities.
So if you have different information for a project, you can override this declaration at the level of your project:

```bash
$ cd ~/dev/my_project
$ git config user.name "Luke Skywalker"
$ git config user.email "luke-skywalker@jedi.force"
```

---

## Ignore certain files globally

I recommend that you set up a global `.gitignore` file for your development environment in order to ignore the files related to your way of working (IDE used for example).

This avoids finding all the IDEs in the `.gitignore` files of your projects.

```bash
$ vi ~/.gitignore
```

Example with PHPStorm / Intellij directory
```bash
.idea
```

Configuring git
```bash
$ git config --global core.excludesfile ~/.gitignore
```

Click the following link to find a collection of `gitignore` file templates: https://github.com/github/gitignore

---

## Choose the commit editor

If you don't want to use `vi`, ask git to use another editor for your commits !

```bash
# With nano
$ git config --global core.editor nano

# You can also choose a graphical editor
# Visual Studio Code
$ git config --global core.editor "code --wait"

# Sublime text
$ git config --global core.editor "subl -n -w"

# Atom
$ git config --global core.editor "atom --wait"
```

---

## Change the default log display format


If the format of the `git log` command does not suit you, you can change it or define a custom one.

There are several predefined formats: `oneline` | `short` | `medium` | `full` | `fuller` | `reference` | `email` | `raw`

```bash
# One line display by default
$ git config --global format.pretty oneline

# You can also define your own log display format
$ git config --global format.pretty "format:%h%x09%an"

# And save it
$ git config --global pretty.my-custom-log-format "format:%h%x09%an"


# Which allows either to use it on demand
$ git log --pretty=my-custom-log-format

# Or define it as the default configuration
$ git config --global format.pretty my-custom-log-format
```

The list of formats and placeholders is available : https://git-scm.com/docs/pretty-formats

---

## Use `fixup` to fix a previous commit and get a cleaner history

You probably know the `--amend` option in order to modify the last commit. But do you know the option `--fixup` and the` autosquash` to modify any previous commit.

**Warning :** the `autosquash` will modify the references of the commits in your branch. Not recommended if you are not comfortable with rebases and what it means.
You can do it serenely if you don't haven't pushed anything on remote yet.

Take the example where you are going to add a README file to your project.

List of commits on your branch where you are writing the file
```bash
$ git log
```

```bash
b58faee269c Add contribution in README
18a11d43eb2 Add deployment in README
8385fc0cd1a Initial commit
```

And there you see an error in the commit to add the deployment part (spelling error for example) (ref commit: 18a11d43eb2).

We fix the error and commit with the `--fixup` option

```bash
$ git commit --fixup 18a11d43eb2
94ae1b02f4a fixup! Add deployment in README
```

The `git log` now looks like:
```bash
$ git log
94ae1b02f4a fixup! Add deployment in README
b58faee269c Add contribution in README
18a11d43eb2 Add deployment in README
8385fc0cd1a Initial commit
```

We can then merge the original commit with the fixup commit
```bash
$ git rebase -i --autosquash 8385fc0cd1a
```

The fixup commit was squashed in the deployment section commit. We can see here the modification of the commits references.
```bash
$ git log
fc5c36d4183 Add contribution in README
0a94be69d2c Add deployment in README
8385fc0cd1a Initial commit
```

---

## Standardize commit messages

Git lets you pre-fill commit messages from a template.

```bash
$ vi ~/.gitmessage
```

```bash
Short Title

IssueNumber

Why :
```

```bash
$ git config --global commit.template "~/.gitmessage"
```

---

## New command git switch

Since version 2.23, a new command is available to switch branches.

Usually we use `checkout`
```bash
$ git checkout my_branch
Switched to branch 'my_branch'
```

We can now use the `switch` command
```bash
$ git switch my_branch
Switched to branch 'my_branch'
```

And if we want to create a new branch and `switch` on it
```bash
$ git switch -c my_new_branch # equivalent to git checkout -b my_new_branch
Switched to a new branch 'my_new_branch'
```

---

## Go back to the previous branch

If you are a bit lazy like me, you can go back to the previous branch using the `git switch -` command

```bash
(master) $ git switch my_branch
Switched to branch 'my_branch'

(my_branch) $ git switch -
Switched to branch 'master'
```

It also works by doing `git checkout -`

Bonus (thanks to [Julien Deniau for the tips](https://twitter.com/j_deniau/status/1259209903077494792)): 
You can also `rebase` or `merge` in the same way:
```bash
(master) $ git switch my_branch
(my_branch) $ git rebase -
```

---

## New git restore command
If you want to restore a modified file to its original state, a new command has been added since version 2.23 of git: `restore`

```bash
# Usually we use `git checkout -- <file>`
(master) $ git status
Changes not staged for commit:
    modified:   README.md
    
(master) $ git checkout -- README.md
(master) $ git status
nothing to commit, working tree clean

# We can now use the command `git restore`
(master) $ git status
Changes not staged for commit:
    modified:   README.md
    
(master) $ git restore README.md
(master) $ git status
nothing to commit, working tree clean
```

---

## Thank you for reading, and let's stay in touch !
If you liked this article, please share. I'm also writing [a dev letter that you might like](https://www.subscribepage.com/enjimmyklein).