visit
Git popularity over the yearsDue to the fact git is meant to work in a decentralized way, it’s a bit more complicated. However, once you understand how git works, it’s crucial to follow good practices. Otherwise, it’s easy to make a mess or misuse it.Remember, with great power comes great responsibilities. That’s why adopting best practices is crucial. Here’s a list of 7 git best practices you can start using in your next commit.
git push
straight to master
. Branch it out!One of the features of decentralization is cheap branching. Pushing code straight to the
master
branch doesn’t promote collaboration. Git made simple comparing code between two branches, which can ignite healthy discussions, improve codebase quality, and spread the knowledge among developers. This practice has the name of .To create a branch, run
git branch -b <branch_name>
. The command git branch -a
lists all the available branches, and you can navigate among them using git checkout <branch_name>
. A cool trick is to use git checkout -
to switch back to the previous branch.Run
git blame <file_name>
to list line by line:Through the command
git log
, you can navigate through codebase’s history. They should tell you a story. Committing vague and abstract messages like “Bugfix”, or “Refactoring auth” may be a problem sooner than you think.I enjoy writing a headline for the commit in the first line. Then, like it is an article, I would write essential pieces of information. I strongly recommend Chris Beams’s article on .This practice helps when digging into the codebase, like when using
git log
or git blame
.One of the magic tricks git performs is the ability to rewrite log history. You can do it in many ways, but
git rebase -i
is the one I most use. With this command, It’s possible to switch commits order, remove a commit, squash two or more commits, or edit, for instance.It’s particularly useful to run it before opening a pull request. It allows developers to “clean up” the mess and organize commits before submitting to review. If you follow the practice 3 and 4, then the list of commits should look very similar to a task list. It should reveal the rationale you had, telling the story of how you end up with that final code.However, using git rebase in published branches, like the
master
, may generate lots of conflicts for the other contributors, which wastes lots of work can and hours of debugging when rebasing is frequent.When you rewrite the history, you need to push with
git push --force
. To prevent a disaster, I always make sure to explicit the target branch. It avoids rewriting incorrect target branch’s history and an unnecessary headache to recover it. Do it to any published branches. Don’t hesitate to do it in your local branch, though.git checkout <upstream_branch>
git pull
git checkout -
git rebase <upstream_branch>
A cool trick is using
git fetch --prune
. It doesn’t require you to checkout to the upstream branch to update it. I love it.As I said, git is powerful. There are tons of commands. You can learn new tricks reading on the web or using the man pages. By running
git help -a
, you can check the most useful ones.Here is a list of the ones I use very often:git cherry-pick
git diff
and git apply
git stash
git bisect
References
Previously published at