4 Things No Git User Should Live Without

Git is a powerful tool to store your code. But it can be very tricky to master.

From the beginning, I’ve struggled a lot to understand the behaviour. Sometimes, I even had a hard time making easy operations as rebases.

Until I found a good teacher.

Here are 4 things I’ve learned I want to share with you that changed my day-to-day.

Pretty Logs

Reading the history of Git with the command git log is very useless: I can’t see all the branches, I can’t see the commits from merged branches, and I can only see 4 to 5 commits per screen…

This leads to me to search for a GUI to use Git. But I’m a terminal lover. I won’t give up.

The first thing I wanna share with you is an alias, git lg.

git config --global alias.lg 'log --graph --oneline --all'

This alias will display all the branches, the dependencies between them, and in a compact way.

There is no GUI that can display better information with little effort.

Conflicts Visualization

Solving conflicts is always a headache. Who did this? Why? I must check the reason, find the person who did the changes…

But, a simple git configuration can help me a lot.

GUIs help me display the modification from one branch and from the other.

git config --global merge.conflictStyle diff3

But with diff3, I can do it in the terminal. I can see the differences I’ve made, the differences made on the target branch and the state of the file before both made any modification.

This will ease the work to see the modifications added on one branch and on the other.

Avoid Solving Conflicts

Another one about solving conflicts.

When I work on long-duration branches, rebasing myself many times with the main branch, I’m always solving the same conflicts.

git config --global rerere.autoupdate true
git config --global rerere.enabled true

rerere stores the way I’ve solved a conflict. And when the same conflict appears again, it applies the same resolution as I did.

This saves me a lot of work.

Edit a Commit

Commits are immutable. That’s how Git work.

I can’t edit an existing commit.

But I only want to add a missing semi-colon, a code review feedback or a unit test.

Instead of creating another commit with a useless message “fix compilation”, “add unit test”, “code review feedback”, I can try to change my existing commit.
How?

git commit --amend --no-edit

In fact, this won’t edit my commit, it will create a new one which is a copy of the existing one. And it will add the modifications present in the stage.

No more branches with plenty of meaningfulness commits, but just a single one.

Still, when editing a pushed commit, I must then push force the commit to upload it to the remote repository. This means that I can’t edit a commit on a shared branch. I can only do that on commits from my own branches.

If you want to learn more about good quality code, make sure to follow me on Youtube.

My New ebook, How to Master Git With 20 Commands, is available now.

Leave a comment

A WordPress.com Website.