user2167634
user2167634

Reputation: 33

Git: remove commits for replaced files from history

I'm quite new to git and so some cruft and errors have accumulated in my repository. Now I wanted to clean it up again.

My situation is the following. I have added pictures to my repository but in a too high resolution. I replaced them later on with smaller versions, but they are still there in my history. I'm the only one who ever worked on that repository, so the problems of rewriting the history can be tolerated. My problem is that I have done other commits between adding the pictures and replacing them. Is this possible? If yes then how?

To illustrate it a little bit more what I have:

04f9ac3 replace big pictures with small pictures
503c2b6 make a useful commit
8ab463d add big picutres
3d2a5a5 initial commit

And what I'd like to achieve:

XXXXXXX combined commit of the "add big pictures" and "replace with small pictures"
503c2b6 make a useful commit
3d2a5a5 initial commit

I found a lot about "squashing" commits but if I understood it correctly it combines "neighbouring" commits together. What I want to do is more to select certain commits and combine them. I hope that makes sense to anyone. :)

Upvotes: 3

Views: 57

Answers (2)

jnovack
jnovack

Reputation: 8827

You want to squash, you were right. HOWEVER, you cannot squash the first commit in the rebase. :(

$ git log
04f9ac3 replace big pictures with small pictures
503c2b6 make a useful commit
8ab463d add big picutres
3d2a5a5 initial commit

$ git rebase -i 3d2a5a5    <--- Choose a commit BEFORE the one you don't want
squash 8ab463d             <--- First commit in rebase cannot be squashed :(
pick 503c2b6
pick 04f9ac3

Upvotes: 0

Lily Ballard
Lily Ballard

Reputation: 185681

You can use git rebase --interactive to get an interactive rebase, where you can reorder commits and squash them. You'd invoke it like git rebase -i 3d2a5a5 and you'll probably see something like

pick 8ab463d add big picutres
pick 503c2b6 make a useful commit
pick 04f9ac3 replace big pictures with small pictures

You can now edit this list, so it looks like

pick 8ab463d add big picutres
squash 04f9ac3 replace big pictures with small pictures
pick 503c2b6 make a useful commit

When you save this file and let the rebase start, it will add the "big picutres" commit, then squash in the "replace" commit, and give you an editor to rewrite the commit message of this squashed commit. After you save that message, it will then add the "make a useful commit" on top of this.

Upvotes: 3

Related Questions