Solving Problems with Git

Chiamaka Nwolisa
2 min readOct 4, 2018

--

Photo by John Jason on Unsplash

I came into work and my colleague said he needed my help with something, I asked what and it was about Git*. I had not had my morning tea yet so I didn’t know what to make of this but I had to help, it was urgent.

The problem

We had pushed to production from our master branch after a specific commit (This commit was like 6 commits back from the HEAD of the branch). And there was something happening in production that users were complaining about and the previous day we had committed that fix making it at the HEAD of the master branch (which is the most recent commit).

Now, we wanted to push the latest changes we made the previous day to production so our awesome users can enjoy but how??? We have about 5 commits in between the commit that was pushed to production and the latest commit.

We solved this problem in a very easy way. Only 2 git commands were used: git checkout -b branchName <hash-commit> and git cherry-pick <hash-commit>

The solution

First, we have to create a branch from the last commit we have in production

git checkout -b branchName <hash-commit>

All this does is it creates a new branch with the branchName you specified and the commit hash of the last commit that is in production (let's assume this hash is 754490b16105c98cc50712f500d111d7f26e1ee3)

git checkout -n new-branch 754490b16105c98cc50712f500d111d7f26e1ee3

Now, you’ll have a new branch from the commit hash that you have supplied. Remember this has all the commits in production.

Second, we wanted to make the changes in the last commit available in production so we move to the new branch we created previously and we cherry pick the commit that we want (let’s assume the hash is ae1ff4523f949043ae01dd37266d1a7d51b9e725)

git cherry-pick <hash-commit>

git cherry-pick ae1ff4523f949043ae01dd37266d1a7d51b9e725

After doing both steps, we now have the code we want from the last commit in a new branch that contains everything currently in production. Now, we can build and push to production and everyone is happy.

*I’m assuming that you already know about Git. There are lots of resources online if you don’t know about it

--

--