git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Cancelling certain commits
@ 2006-01-16 13:57 Bahadir Balban
  2006-01-16 14:22 ` Andreas Ericsson
  0 siblings, 1 reply; 4+ messages in thread
From: Bahadir Balban @ 2006-01-16 13:57 UTC (permalink / raw)
  To: git

Hi,

If I'm not happy with the changes I made in certain commits during
development, how do I cancel those commits and remove them from git
records most cleanly?

For example if I did commits 1 to 10, and want to get rid of commit 3
and 7, such that, the other commits are irrelevant to changes made in
3 and 7, but may involve changes in the same file as 3 and 7 changed,
how do I handle it?

Furthermore, how would I handle it if there was a commit 4, that
depended partially on commit 3? (For example if it uses a type that
was changed in commit 3?) Would I hand-edit commit 4 to fix it?

Many thanks,
Bahadir

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Cancelling certain commits
  2006-01-16 13:57 Cancelling certain commits Bahadir Balban
@ 2006-01-16 14:22 ` Andreas Ericsson
  2006-01-16 23:43   ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Ericsson @ 2006-01-16 14:22 UTC (permalink / raw)
  To: Bahadir Balban; +Cc: git

Bahadir Balban wrote:
> Hi,
> 
> If I'm not happy with the changes I made in certain commits during
> development, how do I cancel those commits and remove them from git
> records most cleanly?
> 

As a general rule, it's desirable to do

	$ git revert --edit

if you've made some programmatical error which is non-obvious (not a 
typo, fe), as you can then make clear why that code wasn't proper for 
implementation. It will also hinder others from doing the same mistake.

Enough lessons though.

> For example if I did commits 1 to 10, and want to get rid of commit 3
> and 7, such that, the other commits are irrelevant to changes made in
> 3 and 7, but may involve changes in the same file as 3 and 7 changed,
> how do I handle it?
> 

# make sure "git status" doesn't show any changes

$ git branch anchor # to make a safe point to reset to if things go bad
$ git reset --hard HEAD~7 # undo commits 3 - 10
# replay commits 4, 5, 6, 8, 9
$ for i in 6 5 4 2 1; do git cherry-pick -r anchor~$i || break; done
# replay commit 10
$ git cherry-pick -r anchor

# done

Another variant:
$ git format-patch --mbox -k HEAD~7
$ git reset --hard HEAD~7
# remove undesired commits from the files created
$ git am -k 000*

# done

If some commits depend on other commits both of these will fail and 
you'll have to fix up by hand.


> Furthermore, how would I handle it if there was a commit 4, that
> depended partially on commit 3? (For example if it uses a type that
> was changed in commit 3?) Would I hand-edit commit 4 to fix it?
> 

Some more lecturing then;
You should have used a topic-branch to introduce such changes. This is 
always desirable when making a change that requires a series of patches.

If this didn't actually require several commits you should have done 
them as one (I always try to make sure my projects at least compile 
cleanly, even in topic-branches although they may contain warnings about 
static but unused functions and variables).

As for fixing up commits by hand when they're already written to the 
object database, you can't really do that (in an easy way, at least). 
What you can do is use "git format-patch" to get patches in mbox format 
for all the commits you eventually wish to keep, then do

$ git reset --hard <one-committish-*after*-undesired>
$ git cat-file commit HEAD > msg
$ git reset --soft HEAD~1
# edit changed files and msg
$ git commit -F msg changed_file1.c changed_file2.c changed_filen.c

Note that in all cases you should create an anchor branch to get you 
back to where you were in case something goes wrong, and never *ever* do

$ git prune

on a repo where you're juggling loose commits like this (although that 
should actually be safe when you create 'anchor' as a branch rather than 
as a tag, which Junio usually recommends).

-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Cancelling certain commits
  2006-01-16 14:22 ` Andreas Ericsson
@ 2006-01-16 23:43   ` Junio C Hamano
  2006-01-17 10:03     ` Andreas Ericsson
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2006-01-16 23:43 UTC (permalink / raw)
  To: Andreas Ericsson; +Cc: Bahadir Balban, git

Andreas Ericsson <ae@op5.se> writes:

 ... (all the good stuff not repeated).

As a general rule, first of all, make sure that this kind of
"cleaning up the history" can be done only before you make your
history available to others, and not after.

> # make sure "git status" doesn't show any changes
>
> $ git branch anchor # to make a safe point to reset to if things go bad
> $ git reset --hard HEAD~7 # undo commits 3 - 10
> # replay commits 4, 5, 6, 8, 9
> $ for i in 6 5 4 2 1; do git cherry-pick -r anchor~$i || break; done
> # replay commit 10
> $ git cherry-pick -r anchor

I think "6 5 4 2 1 0" would be sweeter.  rev~0 == rev

> ..., and never *ever*
> do
>
> $ git prune
>
> on a repo where you're juggling loose commits like this (although that
> should actually be safe when you create 'anchor' as a branch rather
> than as a tag, which Junio usually recommends).

Sorry, I do not understand this part.  tags and branch heads are
used in the same way to determine what objects to protect from
"prune", so I thought there was no difference from the safety
point of view between the anchor being a branch or a tag.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Cancelling certain commits
  2006-01-16 23:43   ` Junio C Hamano
@ 2006-01-17 10:03     ` Andreas Ericsson
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas Ericsson @ 2006-01-17 10:03 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Bahadir Balban, git

Junio C Hamano wrote:
> Andreas Ericsson <ae@op5.se> writes:
> 
>># make sure "git status" doesn't show any changes
>>
>>$ git branch anchor # to make a safe point to reset to if things go bad
>>$ git reset --hard HEAD~7 # undo commits 3 - 10
>># replay commits 4, 5, 6, 8, 9
>>$ for i in 6 5 4 2 1; do git cherry-pick -r anchor~$i || break; done
>># replay commit 10
>>$ git cherry-pick -r anchor
> 
> 
> I think "6 5 4 2 1 0" would be sweeter.  rev~0 == rev
> 

Didn't know that. The day isn't a complete loss already. :)

> 
>>..., and never *ever*
>>do
>>
>>$ git prune
>>
>>on a repo where you're juggling loose commits like this (although that
>>should actually be safe when you create 'anchor' as a branch rather
>>than as a tag, which Junio usually recommends).
> 
> 
> Sorry, I do not understand this part.  tags and branch heads are
> used in the same way to determine what objects to protect from
> "prune", so I thought there was no difference from the safety
> point of view between the anchor being a branch or a tag.
> 

You're right. That's weird though, because I distinctly remember being 
bitten by this when I was very new to git. OTOH, I probably did 
something stupid that broke the repo before, and my girlfriend provided 
plenty distraction so I didn't do the pruning and the reset'ing on the 
same day.

Would a tag with the same name in a central repo overwrite a local one 
when "git fetch --tags" is used? I think I did this over http, but I'm 
not sure. I know I had to clean up un-annotated tags that got pushed by 
accident after I had restored the lost commits though, so this could be 
a cause (there was an 'anchor' tag in the shared repo as well).

-- 
Andreas Ericsson                   andreas.ericsson@op5.se
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-01-17 10:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-01-16 13:57 Cancelling certain commits Bahadir Balban
2006-01-16 14:22 ` Andreas Ericsson
2006-01-16 23:43   ` Junio C Hamano
2006-01-17 10:03     ` Andreas Ericsson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).