2

So I'm currently making a site with HTML5 and and I'm quite a ways in when I realize that I forgot to put in the HTML5 resets in to my css file.

Currently I'm about 10 commits in and what I would like to do is to add the reset code in to the css file into my very first commit and have it present in the rest of the commits after.

I read around a bit and I think git rebase is the way to go but Im not really sure how to use it correctly.

This SO question seems to be close to what I want to do, but Im not well versed in Git yet to adapt this.

Thanks in advance.

Community
  • 1
  • 1

2 Answers2

2

Checkout git rebase -i where -i stands for "interactive". There you can "squash" commits together.

tobiasbayer
  • 10,269
  • 4
  • 46
  • 64
  • Thanks, but can I retain my commit history as is? – Jay Dropout Nov 12 '11 at 13:07
  • No, because you are changing your commits. – tobiasbayer Nov 12 '11 at 13:59
  • 2
    @JayDropout: What do you mean by retaining your history? A commit represents a snapshot of the tree, so by definition, subsequent commits will be changed - their contents are different. But you can still retain commits corresponding to the original ones, plus the change you snuck in. So yes, you're rewriting history, but no, the commits won't disappear, they'll just be changed. – Cascabel Nov 13 '11 at 00:37
1

Try this:

1) Check out the past commit you want to change with git checkout.

2) Tag this past commit using git tag PastCommit.

3) Use git checkout -b FixBranch to create a new branch called FixBranch.

4) Add the new file in and make a new commit on FixBranch.

5) Switch to your master branch using git checkout master.

6) Tag using git tag BACKUP in case you screw something up - just to be safe before a rebase :)

7) Use git rebase --onto FixBranch PastCommit - this will rebase the stuff between PastCommit and the head of master onto FixBranch.

8) Tidy up using git tag -d PastCommit, git tag -d BACKUP and git branch -d FixBranch.

9) Use git gc to garbage collect and get rid of any dangling commits.

Note that you can (and should!) visualise all this in gitk so that you can see what you're doing (and make sure I haven't missed anything - this is a bit off the top of my head).

Addendum: I just tried this on a test repository and it works fine.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • Thanks for your answer Stuart, but I get a bunch of merge errors at every few commits. It also adds a new blank line after each line in the css file. Just to clarify, what I'm doing is not adding a new file but editing my existing css file and adding the code in to it. – Jay Dropout Nov 12 '11 at 14:54
  • @Jay: You'll probably get merge errors at every commit in your history where you changed the .css file - that's normal with a rebase. You have to manually merge the changes in, which is a bit of a pain, but it's the only way I know (it makes sense that you'd need to as well if you think about what you're actually trying to achieve here). A good merge tool like WinDiff will help - you can set Git up to use it. Not sure why you're getting blank lines, unless it's a cross-platform line ending issue. – Stuart Golodetz Nov 12 '11 at 18:55
  • Thanks Stuart, I see, I guess I was expecting it be a clean merge hehe.. And I'm sure its a line-ending issue coz I noticed that my document is in CRLF and I'm currently on a Mac. Its probably too late to change that now. – Jay Dropout Nov 13 '11 at 02:21