4 ms·
Git needs the parent pointer equivalent of NIL. Whenever I start a blank new repo nowadays, I do a git commit --allow-empty -m "NIL" See, many git operatio
by kazinator 10y ago
Git needs the parent pointer equivalent of NIL.
Whenever I start a blank new repo nowadays, I do a
git commit --allow-empty -m "NIL"
See, many git operations require a parent reference! For instance if you want to interactively rebase the top commit, it's actually "git rebase -i HEAD^": rebase back to (but excluding) HEAD's parent. The equivalent "git rebase -i HEAD~1" means the same thing: that ~1 actually spans two commits: it refers to the parent, but since the parent is the excluded delimiting endpoint you can think of that 1 as being the number of commits being rewritten.
If all you have is a single commit with no parent, you cannot rebase it.
If you have NIL there, it takes the HEAD^ reference; no problem.
Essentially, git is a list manipulation system whose use cases fail when operations span the entire list, oops. You can tell it was designed by someone who never took a Lisp or Scheme course (or forgot all of it). Imagine if (remove 3 '(1 2 3)) failed because the suffix (3) has no CDR, haha. "Sorry, you can't "rebase" your (1 2 3) to (1 2) because 3 is the "root commit"! Just scrap the whole thing and build (1 2) in its place."
- twic 10y agoFWIW, Mercurial has this - there's a notional changeset called 'null' whose hash is all zeroes, which is the parent of every changeset which does not have a real parent. As a result, you can rebase a root changset. For example: $ hg init $ echo silver >colour $ hg add colour $ hg commit --message 'First commit' $ hg checkout null 0 files updated, 0 files merged, 1 files removed, 0 files unresolved $ echo liquid >state $ hg add state $ hg commit --message 'Second commit' created new head $ hg log --template '{rev} = {node|short} child of {p1node|short}: "{desc}" @ {date|isodate}\n' 1 = 99f1472f8a69 child of 000000000000: "Second commit" @ 2016-10-02 17:29 +0100 0 = 6433a3e95c3d child of 000000000000: "First commit" @ 2016-10-02 17:29 +0100 $ hg rebase --source 0 --dest 1 rebasing 0:6433a3e95c3d "First commit" saved backup bundle to /path/to/repo/.hg/strip-backup/6433a3e95c3d-331b8224-backup.hg $ hg log --template '{rev} = {node|short} child of {p1node|short}: "{desc}" @ {date|isodate}\n' 1 = b9951882336f child of 99f1472f8a69: "First commit" @ 2016-10-02 17:29 +0100 0 = 99f1472f8a69 child of 000000000000: "Second commit" @ 2016-10-02 17:29 +0100
- kazinator 10y agoI was thinking exactly the same thing: why doesn't git use an all-zero hash as a special NIL. I think SHA-1 cannot output all zeros as a valid hash.
- al2o3cr 10y agoGit needs the parent pointer equivalent of NIL. Whenever I start a blank new repo nowadays, I do a Sounds like you've already got one...
- kazinator 10y agoOnly in a repo I started myself, not cloned from anywhere.
- arjie 10y ago> If all you have is a single commit with no parent, you cannot rebase it. Well, of course what you want to do here depends. You can squash onto it, edit it, or reorder it. You're commonly going to have conflicts if your first commit created a file and the rest edit it. If you reorder you can't edit a file that doesn't exist yet. Here are a couple of videos to illustrate: https://gfycat.com/GrizzledPartialAntelopegroundsquirrel https://gfycat.com/GrizzledPartialAntelopegroundsquirrel https://streamable.com/1chg https://streamable.com/1chg This wasn't always possible through the UI but it's possible now. If this doesn't do the trick, would you mind sharing what your use case is?
- kazinator 10y agoThe use case is that when I'm making a new repo, I want to be able to do: git rebase -i <first-commit-sha>^ git reset --patch <first-commit-sha>^ git checkout --patch <first-commit-sha>^ I can do all this if the first commit is actually the second and the first is a dummy empty one.
- arjie 10y agoI meant, "what are you setting out to do?" as in "I'm trying to reorder commits" not "I'm trying to `git rebase -i commit`" (the ultimate aim, rather than the path you choose). My instinct is that you can get where you want to go fairly easily with the `git rebase -i --root` I mentioned earlier.
- deleted 10y ago[deleted]
- kazinator 10y agoIf you plant a NIL commit in your repo before doing anything else, and tag it as NIL, then absolutely all git commands that take a ref (including ones you don't even know yet) will Just Work without any special arguments or modifications for the root case. You don't need to memorize yet more special cases for yet one more corner case in git. In existing old repos or other people's repos that do not have NIL, I don't need it, because I won't be rewriting their history (let alone all of it back from the empty state!) It's handy, when a repo is brand new, to be able to massage the newly created master branch using the same workflows that are rehearsed daily, rather than special cases.