Blog Tags: 

Git - Fixing commit mistakes

I use Git. I use it a lot. I basically use it for everything I do, from code revision control to revisioning my notes, my journal, even my email archive (don't ask, it's a long story).
As with anything you do, you are bound to make mistakes. When it comes to coding and revision control, mistakes can range from bad commit messages, to buggy code, to code that shouldn't have been committed together with other code, or even at all.
Here are a couple of tips I find myself using quite often.

Correcting mistakes in the last commit

This is very easy with git-commit --amend, For example:
I decide I don't like the commit description I used.
git-commit --amend # just edit the description
I want to edit buggy code I just committed to path/to/file.
editor path/to/file
git-add path/to/file
git-commit -v --amend # note your recent changes are part of the commit
I want to remove a file I included accidentally in the last commit (e.g., because I want to commit changes to that file separately).
git-reset HEAD^1 path/to/file # this doesn't delete your changes to path/to/file
git-commit --amend -v
git-commit -v path/to/file # make a new commit with changes to path/to/file

Correcting mistakes in previous commits

This also uses git-commit --amend, but involves two extra steps:
1) git-checkout <bad-commit>
   ... make your changes ...
2) git-commit --amend -v
3) git-rebase --onto HEAD <bad-commit> <checked-out-branch>
If a conflict occurs while re-applying your commits, you may have to manually resolve the conflict and then git-rebase --continue. See the manual page for further details.
For example, I notice I made a mistake in commit 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab several revisions ago and I'm checked out to branch master.
To correct the commit history:
git-checkout 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab
editor path/to/file
git-add path/to/file
git-commit --amend -v
git-rebase --onto HEAD 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab master


Note that by editing your commit history you are effectively re-applying your changes, and thus creating new commits with new commit ids. For example, this means you'll have to recreate existing tags.


Alon Swartz's picture

The new TurnKey 11.0 release (soon to be announced, but the beans have already been spilled) includes git pre-installed as a side effect of including the awesome etckeeper.

Also, if you're in need of a code repository appliance for collaboration, take a look at TurnKey Redmine, TurnKey Trac, and TurnKey Revision Control.

Nikos's picture

Hi Alon

Firstly let me start off by saying congratulations on being one of the forefathers of the Turnkey linux project - I am new to TKL but the more I get into it the more I see just how inovative the idea is.

We have also recently signed up for the Turnkey Hub services and feel that you guys have done a great job there too - in fact after getting my first TKL application up and running locally, I was thinking how it would sell like hot cakes if something similar could be done in the cloud and a within the same day I came across the registration form for the invitation to the service and I was delighted.

I am an IT enthusiast with a bit of a ranging background - from hardware to sys admin to web developing. Although this gives me a wide scope it has not allowed me enough time to hone my skills on in a particullar area. For the last 3 years we've been trying to get our startup web development company here in Athens Greece off the ground and I have taken a more technical role while my partner is involved with markup (html css) and design.

Needless to say we've had to work over some hurdles and currently we are working on a Joomla-VirtueMart eshop project within the the TKL Joomla application (locally for development and in the cloud as production). I have a couple of questions which I would like your insight on - again please excuse any low-level questions as my background in Linux generally is quite basic.

  • Is there an easy way for directly porting a local TKL Application into the Turnkey Hub?

As we have multiple coders working on our current project we would like to incorporate a code repository solution that is coupled with project management. I came across your post regarding Git being included in TK 11.0 and how you mention Redmine, Trac and Revision Control - reading up a bit I see the first two also include project management and Redmine offers gantt charts - a helpful visual aid in project management. I have a couple of questions on this topic:

  1. Could tell me which one (out of the ones mentioned before) you feel is more beneficial? I am aiming to implement a solution that will be easy to setup both from a server perspective as well as from a user.
  2. We already use the Joomla TKL Application where our code resides - is there an easy way to deploy Redmine, Trac into that same application instance?
  3. Which version control system would you suggest we use if we are to be sharing between Windows and Mac - I have had some experience with Subversion in the past however I have been reading great feedback regarding Git.?
  4. Which IDE do you think is best to go for cross-platform (to ensure uniform setup procedures when linking to the version control system) - I have worked with eclipse in the past do you feel this is a good option?

sorry for the long reply, I just would really appreciate your insight on a couple of things.


many thanks for your time


nick koutakis

Jeremy Davis's picture

The best/easiest way to trasfer content between TKL instances (whether on hardware, in VMs, in the cloud, etc) is using TKLBAM. Have a read: announcement here and docs here. As for your other questions, sorry I'm not much use to you. Good luck though! :)

Alon Swartz's picture

I second Jedmeister's comment on using TKLBAM. That is one of the main issues it was designed to solve - I love this quote Liraz made a while ago:

Imagine being able to develop your site on a locally running appliance (e.g., running in VirtualBox or VMWare). Then, when you're ready you can automatically migrate your appliance, with all your customizations to a cloud hosting provider of your choice.

Regarding your other questions, it depends on your needs. Trac and Redmine are competing projects. I'd recommend testing both and choosing the one you like best. If you don't need an issue tracker, the revision-control appliance might be a good fit.

I personally prefer git over all other VCS. I've been using it for years and it rocks! It is also supported on windows/mac - github has installation info here.

Regarding my preferred IDE. I don't have one, I use VIM as my exclusive editor - I find IDE's provide more distraction and annoyance than benefits (but thats me). There is a learning curve involved, but it's well worth it IMHO. It's also cross platform. BTW, I use VIM for all my editing, from code, my work journal and even my email.

BTW, Liraz wrote a nice post you might find interesting: Using git and rsync to synchronize changes on a staging box to a live server.

Nikos's picture

Thanks for the tips!

Is there a way in combine TKL apps - like getting Redmine into the same instalation as TKL-Joomla?

Jeremy Davis's picture

Yes it can be done, but TKL can't do it for you. You'll need to manually install it on top and deal with any issues that may arise. TKLBAM should easily be able to take care of backing it up (and migrating it to a new instance should you need to) but you may have to double check that it is getting all your customisations (ie that you don't put things in 'naughty' places).

Jeremy Davis's picture

Given my recent headaches after thinking that it was a good idea to delete all my old code without fully testing the end product, I think I need to have a go at git!

etckeeper sounds like a winner too, a brilliant inclusion to TKL server appliances!


Add new comment