git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: sending changesets from the middle of a git tree

From
Linus Torvalds <torvalds@osdl.org>
Date
Aug 14, 2005, 05:16 UTC
Message-ID
<Pine.LNX.4.58.0508132204050.3553@g5.osdl.org>
In-Reply-To
<Pine.LNX.4.58.0508132155100.3553@g5.osdl.org>
On Sat, 13 Aug 2005, Linus Torvalds wrote:
> That's correct. Same things apply: you can move a patch over, and create a 
> new one with a modified comment, but basically the _old_ commit will be 
> immutable.
Let me clarify.

You can entirely _drop_ old branches, so commits may be immutable, but nothing forces you to keep them. Of course, when you drop a commit, you'll always end up dropping all the commits that depended on it, and if you actually got somebody else to pull that commit you can't drop it from _their_ repository, but undoing things is not impossible.

For example, let's say that you've made a mess of things: you've committed three commits "old->a->b->c", and you notice that "a" was broken, but you want to save "b" and "c". What you can do is

	# Create a branch "broken" that is the current code
	# for reference
	git branch broken
	# Reset the main branch to three parents back: this 
	# effectively undoes the three top commits
	git reset HEAD^^^
	git checkout -f
	# Check the result visually to make sure you know what's
	# going on
	gitk --all
	# Re-apply the two top ones from "broken"
	#
	# First "parent of broken" (aka b):
	git-diff-tree -p broken^ | git-apply --index
	git commit --reedit=broken^
	# Then "top of broken" (aka c):
	git-diff-tree -p broken | git-apply --index
	git commit --reedit=broken

and you've now re-applied (and possibly edited the comments) the two commits b/c, and commit "a" is basically gone (it still exists in the "broken" branch, of course).

Finally, check out the end result again:
	# Look at the new commit history
	gitk --all
to see that everything looks sensible.

And then, you can just remove the broken branch if you decide you really don't want it:

	# remove 'broken' branch
	rm .git/refs/heads/broken
	# Prune old objects if you're really really sure
	git prune

And yeah, I'm sure there are other ways of doing this. And as usual, the above is totally untested, and I just wrote it down in this email, so if I've done something wrong, you'll have to figure it out on your own ;)

			Linus
Previous: Linus TorvaldsNext: Junio C Hamano
Message 5 of 12 in “sending changesets from the middle of a git tree”
  1. Steve FrenchAug 14, 2005
  2. Ryan AndersonAug 14, 2005
  3. Ryan AndersonAug 15, 2005
  4. Linus TorvaldsAug 14, 2005
  5. Linus TorvaldsAug 14, 2005
  6. Junio C HamanoAug 14, 2005
  7. Petr BaudisAug 14, 2005
  8. Junio C HamanoAug 15, 2005
  9. Catalin MarinasAug 15, 2005
  10. Wolfgang DenkAug 17, 2005
  11. Junio C HamanoAug 17, 2005
  12. Catalin MarinasAug 15, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.