News for hackers. Stuff that matters.
Re:Git Branches: Intuition and Reality by PaulDavisThe1st on Thursday November 23, 2023 @06:40PM (#38399040)
> A git patch is just a textualized form of the list of commits you created locally. You can apply them to any branch you like.

Not even branch. You can combine two unrelated repositories, and in theory you could cherry-pick commits from one of the original repositories to the other.

Of course, in practice this rarely works because the files mentioned in the commit don't exist in the other repository. But there's nothing in git's mechanisms that stops this: it's just a bunch of commits, which are actually just a bunch of file contents.

The whole "diff" or "patch" concept in git is just way of doing data presentation - rather than showing you the actual commit, which is generally not helpful, it shows you the difference between the contents of the commit, and the same files in the state referenced by the previous commit.

Re:Git Branches: Intuition and Reality by jillesvangurp on Friday November 24, 2023 @03:04AM (#38401610)
Git commits always have a parent. Applying git patches to a repository without the parent is not going to work. The repository must have the parent commit. You might force it to work without that but it's going to create conflicts, complicate merging, etc.

The reason is that a git patch is not merely a diff but an export of the actual commit objects and referred content (trees, blob diffs, hashes, etc.). Applying the patch recreates the exact commit objects on the other side. The end state is exactly the same as if you would have merged the commits from some branch. There is no difference.

Git diff, shows you a normal diff. It's not the same thing. You can indeed apply such diffs to your local work copy. But that's not the same thing as a git patch.

The commands you need are git format-patch (exports the patch) and git apply (applies it).

Re:Git Branches: Intuition and Reality by cassianoleal on Friday November 24, 2023 @05:38AM (#38402509)
> Applying git patches to a repository without the parent is not going to work.

If that's the case (and I'm not saying it isn't - I really don't know), how does cherry-pick work?

I know I can cherry-pick commits in the same repo from entirely separate commit chains, where the only common ancestor is either my local HEAD or some other commit way below both of us.

Why would that not work for different repositories?

Re:Git Branches: Intuition and Reality by jillesvangurp on Saturday November 25, 2023 @02:34PM (#38415956)
It still uses the notion of a common parent to merge a single commit. Works as long as there are no conflicts. As to why that would or would not work, I refer to the git internals section of the Git book. Great stuff. But they are not performing any dark magic here.
Re:Git Branches: Intuition and Reality by cassianoleal on Tuesday November 28, 2023 @05:03AM (#38444206)
So are you saying that if I have completely disconnected commit trees in the same repo, git will refuse to cherry-pick between each other?