I have been tasks today for the first time in a good 6+ months to just sit and skill myself up, the aim today is reach a higher more enlightened stage of git mastery. For this training I’m going to be using Learn Git Branching a little interactive game for going through git conventions via command line. Here is a brief write up of the training plan.
I have used git in many forms for my entire career in development. At first I used GUI’s like Sourcetree & Git Extensions but in the last year I switched and decided to finally learn the full mental model that is required for git I would use command line only. Drill the command into my brain!
Its been pretty successful, I now rarely use the GUI’s and despair if I’m asked to help someone with a git issue and they don’t have a CLI open (yeah I’m becoming one of those twats). Even though I can do more or less whatever I need to do on command line there are some things I forget and some times I haven’t gotten around to learning yet! So this refresher is an opportunity to up skill my git knowledge. This wont include navigating in nano/vim so don’t worry, its strictly git commands!
Git cherry pick during a rebase
git checkout master
git merge bugFix
git rebase -i HEAD~3
The most useful part of the tutorial I have come across and something I am writing about now for future Alex to reference is the cherry-pick/interactive rebase method for merging branches.
The problem presented us with 3 commits after Master each with their own branch “debug”, “printf”, “bugFix”. The task was to merge “bugFix” with master but leave out the “debug” and “printf”. This was accomplished by first merging debug and master, then rebasing to
HEAD~3 with the
-i flag allowing me to cherry pick and remove the two commits I did not need.
This is super smart as I would have probably done a merge then manually removed the debugging code in the 2 unnecessary commits (for production) and created a new commit with them removed. So using this method is likely to save me more time in the future and allow me to confidently commit more often even if I am in the middle of debugging something!
Something else I never do, knowing full well git has the capacity to do so (I’m just lazy) is amend my commits, very often if im asked to make an on the fly amend to something thats broken live, I will branch off of master, make the code change, commit and merge with the development branch for a deploy. I then jump on our testing domain and check the issue if its something I cant replicate locally (usually the case if the db locally isn’t up to date and I don’t feel like syncing large db’s) at this point I notice there is a slight error, so I jump back into my branch and make another code change, and another commit. Another pointless commit.
There is no excuse, I shouldn’t be doing it and this method will help to get around the issue of creating multiple commits for what is effectively the same body of work.
So the challenge presented was to juggle some commits around, we have a “caption”, “newImage” and “master” branch. We want to make an amend to “newImage” without messing up our git flow. There are no doubt numerous ways to do this, however the challenge specifically states we cannot use an interactive rebase.
So my solution for this was pretty straight forward, without needing to reset HEAD at anypoint, although I did accomplish the same using rebase -i!
git checkout master
git cherry-pick C2
git commit --amend
git cherry-pick caption
C2 has been amended and merged into master along with caption and no unnecessary commit has been generated as a result of our amend.
So this part is super short and its something I didnt know you could do to navigate commit tree’s, specifically that you can chain commands using the “~” and “^” modifiers.
git checkout HEAD~^2~2
This command would move us to the second parent commit to where ever we are with HEAD, then 2 up, all in one command!
Use this for reference, or just google/SO:
Do the rest of the Getting Started, Collaborating, and Advanced Tips sections of the Atlassian tutorial.
- Optional. If you want to go deeper:
GUI: Git Extensions
- Pretty easy to use.
- Does 3 way merges.
- Good keyboard shortcuts.
Alternatively, if you use IntelliJ IDEs then use the built in source control. It’s pretty good.
- Does 3 way.
- Simple to use but doesn’t support all necessary operations.
- Looks good.
Command line: Cmder
- CMD/Bash/PowerShell environment.
- Includes git.
- Looks nice.
Line endings and autocrlf
- Windows users set
- Mac/Linux users set
Command line vs GUI
Learn on the command line but keep a GUI open for a better visualisation of what’s happening.
Use the GUI for dealing with merge/rebase conflicts as that’s just awkward in a terminal.
Its always important to keep up to date, this refresher helped reinforce my mental models concerning git flow. I fully recommend the first course mentioned Learn Git Branching, should only take a few hours from start to finish!