Git is by far the most popular Version Control System (VCS) used in web development.
According to the latest StackOverflow survey over 90% of respondents use it on a regular basis.
The question one would ask is how to use it effectively?
This post will guide you through the most best practices, important git commands and do’s and don’ts.
- git – is a version control system, don’t confuse it with Github or Gitlab which are hosting providers
- repository – home to all your branches and commits, everything that we do with git is done in the context of a repository
- stage – in order to commit something you’ll need to say what things you actually want to commit, that’s staging
- commit – making your changes ready for a push
- branch – all your commits will be done on a branch
- origin – remote repository
- push – uploading changes to origin
#1 Use Git GUI
This post’s title says it all – it might be a controversial thing, especially with this being the first point, but it’s nothing but the truth. Once you acknowledge this you’ll be able to boost your productivity by a serious amount.
Obviously, this isn’t true for all people and in all cases. If you prefer using
git CLI good for you, but most people will find using a
git client much more straightforward.
My personal recommendations would be GitKraken and Fork, although there are many other alternatives available.
There might be some cases in which there’s a necessity to use git from the command line and I’ll cover that later.
#2 You won’t break anything if you don’t push to the origin
It might be a trivial thing however it’s crucial. If you don’t have full confidence behind your commit just don’t push it. I wouldn’t recommend using force push for master or feature branches but sooner than later you’ll have to resolve merge conflicts and that process may not be that straightforward.
#3 Merge but also rebase, squash and reset
The most common approach when we want to connect two branches together is doing a merge, which is fine for adding feature branch’s changes to the master branch. However, working with other people will force you sometimes to do the opposite. In order to change your branch’s root use rebase. Apart from the obvious, your git history will be clean so a clear win-win situation here.
git rebase <base>
Squashing is another helpful technique that allows us to reduce two or more commits into one. The best way to do this in the CLI is to reset –soft your local to one of the previous commits and then create a new commit and use
force-push to keep git history clean.
git reset --soft HEAD~n && git commit
There are 3 types of git resets:
reset --soft– uncommit changes, changes are left staged,
reset --mixed– uncommit + unstage changes,
reset --hard– uncommit + unstage + delete
#4 Use conventional commits (really)
Conventional commits are a great way to make your commit’s message more meaningful. There’s a really comprehensive Github gist that documents how to use them properly.
#5 Cherrypick for the win
Let’s imagine a situation where you find yourself in need of changes done on another branch and usually merging or rebasing might not be doable (and we don’t just copy files, period). The best thing that you can do here is make a cherrypick – it picks a specific commit and adds it to your branch.
git cherry-pick commitSha
#6 Amend for a cleaner win
Developers make typos. Everyone makes them and sometimes we just need to correct them. Fortunately, there’s a git concept called
git commit --amend -m "an updated commit message"
#7 Delete unnecessary branches and stashes
A very trivial thing that helps to keep your repo clean is to remove unused branches and stashes, you’ll be grateful later.
I want to dedicate a separate section to GitKraken, which is by far my favorite tool to interact with git repositories is GitKraken. We’re quite fond of it at NubiSoft because of its ability to align our workflows. It’s not free but it massively streamlines your basic and not-so-basic git tasks.
With just a right click you’ll be presented with pretty much all the git commands that you need.
In this post, I’ve described how to use some of the most important and useful git concepts. Obviously, there is more to it like tags, dropping commits and so on, but this should get you properly started.