DEV Community

Jimmy Klein
Jimmy Klein

Posted on

9 tips for git

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

$ git config --global user.name "Darth Vader"
$ git config --global user.email "darth-vader@deathstar.space"
Enter fullscreen mode Exit fullscreen mode

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:

$ cd ~/dev/my_project
$ git config user.name "Luke Skywalker"
$ git config user.email "luke-skywalker@jedi.force"
Enter fullscreen mode Exit fullscreen mode

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.

$ vi ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

Example with PHPStorm / Intellij directory

.idea
Enter fullscreen mode Exit fullscreen mode

Configuring git

$ git config --global core.excludesfile ~/.gitignore
Enter fullscreen mode Exit fullscreen mode

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 !

# 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"
Enter fullscreen mode Exit fullscreen mode

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

# 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
Enter fullscreen mode Exit fullscreen mode

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 theautosquash 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

$ git log
Enter fullscreen mode Exit fullscreen mode
b58faee269c Add contribution in README
18a11d43eb2 Add deployment in README
8385fc0cd1a Initial commit
Enter fullscreen mode Exit fullscreen mode

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

$ git commit --fixup 18a11d43eb2
94ae1b02f4a fixup! Add deployment in README
Enter fullscreen mode Exit fullscreen mode

The git log now looks like:

$ git log
94ae1b02f4a fixup! Add deployment in README
b58faee269c Add contribution in README
18a11d43eb2 Add deployment in README
8385fc0cd1a Initial commit
Enter fullscreen mode Exit fullscreen mode

We can then merge the original commit with the fixup commit

$ git rebase -i --autosquash 8385fc0cd1a
Enter fullscreen mode Exit fullscreen mode

The fixup commit was squashed in the deployment section commit. We can see here the modification of the commits references.

$ git log
fc5c36d4183 Add contribution in README
0a94be69d2c Add deployment in README
8385fc0cd1a Initial commit
Enter fullscreen mode Exit fullscreen mode

Standardize commit messages

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

$ vi ~/.gitmessage
Enter fullscreen mode Exit fullscreen mode
Short Title

IssueNumber

Why :
Enter fullscreen mode Exit fullscreen mode
$ git config --global commit.template "~/.gitmessage"
Enter fullscreen mode Exit fullscreen mode

New command git switch

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

Usually we use checkout

$ git checkout my_branch
Switched to branch 'my_branch'
Enter fullscreen mode Exit fullscreen mode

We can now use the switch command

$ git switch my_branch
Switched to branch 'my_branch'
Enter fullscreen mode Exit fullscreen mode

And if we want to create a new branch and switch on it

$ git switch -c my_new_branch # equivalent to git checkout -b my_new_branch
Switched to a new branch 'my_new_branch'
Enter fullscreen mode Exit fullscreen mode

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

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

(my_branch) $ git switch -
Switched to branch 'master'
Enter fullscreen mode Exit fullscreen mode

It also works by doing git checkout -

Bonus (thanks to Julien Deniau for the tips):
You can also rebase or merge in the same way:

(master) $ git switch my_branch
(my_branch) $ git rebase -
Enter fullscreen mode Exit fullscreen mode

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

# 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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (2)

Collapse
 
michaelcurrin profile image
Michael Currin • Edited

Thanks for sharing. It is explained well and I learnt some new commands.

In the fix up section, make sure the end of a codeblock is on it's own line

Also I can see the raw and endraw tag - if you are not using Jekyll and you aren't using liquid syntax then you could take it out.

Also in the git switch section you introduced how to use the command as alternative to git checkout but it would be nice to know why git switch is worth using instead

Thanks

Collapse
 
klnjmm profile image
Jimmy Klein

Thank you ! I correct my post.

There is no particular advantage to using the switch command. It's a attempt to reduce all the functionalities of checkout.