How to get to the next level with Git
Disclaimer: This tutorial is ideal for z-shell and vim users that are already familiar with Git and want to get to the next level.
As a professional software engineer with the experience of migrating my company’s codebase to git from SVN I will walk you through the bare essentials I needed to get to the next level with git.
I went from only using git push, pull and commit to doing interactive rebase and debugging my co-workers’ git problems on a daily basis. Now, I feel I can do almost anything I want and I never run into panic situations were I am not sure what to do.
Outcomes of reading this tutorial:
Your temrinal will look pretty like this:
You will be able to do interactive rebase like a pro.
You will really understand what a ref is, what Detached HEAD is.
You will use shortcuts instead of the full git commands.
1. Go through the Best Online Tutorial
There are many tutorials out there, but this is the best and only one I would recommend:
https://learngitbranching.js.org/
. If you are using git and have not gone through this tutorial yet, do yourself a favor and try it out. This is the most visual and interactive way to understand git concepts. It comes in two parts. Part 1 goes over basic concepts and should take you 1-2 hours and Part 2 goes over concepts related to interacting with remotes, aka the part that stores your code and should take 2-3 hours.
Concrete learning outcomes
After you are done with this tutorial you should be able to answer the following questions:
What is the difference between local and remote?
How does rebase really work?
It takes a set of commits, copies them and plops them down somewhere else. It is very similar to cherry-picking, in the sense that you can reproduce the results of rebase using cherry-picking.
What is HEAD?
HEAD Is a type of ref. A ref is an indirect way of referring to a commit.
In other words it translates to a git hash, which is a commit. HEAD is just lingo for the current commit in your checked out branch. Sometime the HEAD becomes detached. That is the case if HEAD is pointing to a commit and not a branch.
Type git rev-parse HEAD to see the commit. If things are normal then git rev-parse HEAD and git log should return the same hash as their top commit.
Before you start, take a not of these useful (tutorial) commands that you can type in the cell
reset
undo – really wish git had that command
show/hide goal
objective
hint
show solution
2. Enable Git Shortcuts
This section is customized for z-shell users, but bash users can also benefit. Replacing bash with Z shell has been a major productivity booster for my daily work. Z shell makes it very easy to install various plugins through the oh-my-zsh framework, which if you have zshell you should definitely install. If you are not using oh-my-zsh aready perhaps you want to check it out first. [https://github.com/robbyrussell/oh-my-zsh]
The plugin I am using is the git-plugin hosted here [https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git.plugin.zsh]. It comes with tons of advantages.
First, it enables use of established shortcuts. You don’t have to come up with your own shortcuts and you don’t have to pollute your rc file with alias definitions. Instead you can use the ones established by the developers and the community. There are a lot of shortcuts, but I only use the ones below on a daily basis.
Original Command
Shortcut
git add
ga
git status
gst
git cherry pick
gcp
git fetch
gf
git checkout branch_name
gco branch_name
git checkout -b new_branch_name
gco -b
For a full list take a look at the read me page of the plugin. It is quite nice that I can only type three characters and express what I want and given how much time I spend using git, this is a great booster.
Second, it is very easy to install once you have the zsh terminal actually setup. For that all you need to do is add git to the plugins array of your .zshrc file. Here is mine:
3. Optimize your Terminal
As I said above I use z-shell. I use the milshadzic theme. This makes my terminal look pretty and also shows:
The name of the current directory instead of the whole path
The name of the branch I am modifying color coded and separated from the path with a vertical split line
To learn more about oh-my-zsh and the git plugin here are some useful links:
4. Vim plugins
Optimize your editor’s plugins. Assuming you already have some vim plugins. To learn more about vim plugins please read here (TODO future post). Fugitive is a git wrapper. It comes with a lot of features. The main and only feature I use is :Gblame. After installing fugitive from here: https://github.com/tpope/vim-fugitive, just open a vim window inside any git repository you have and type :Gblame. Every line will have blame and the author on it. Pretty neat hah? I used to have to go to my terminal and type git blame filename before and it is so much easier now.
5. Incorporate rebase on a daily basis
I used to be terrifed of rebase and merge conflicts. The though of seeing a detached head was scary, but I now use them daily and it has changed my productivity a lot. I don’t have to spend any effort figuring out how these tools work and I can focus on my actual work.
6. Cheatsheets
Print this cheatsheet out and post it next to your desk at work so that you can look at it during the day: https://www.git-tower.com/blog/git-cheat-sheet