5 ms·
> The "index" is a silly dongle in Git. Everyone thinks that until they need to use it for something. If all you do is a bunch of linear small changes with ob
by ajross 9y ago
> The "index" is a silly dongle in Git.
Everyone thinks that until they need to use it for something. If all you do is a bunch of linear small changes with obvious implications and two-line commit messages, then the index is nothing but an extra step.
But at some point you're going to want to drop a thousand-line change (from some crazy source like a contractor or whatnot) on top of a giant source tree and split it up into cleanly separable and bisectable patches that your own team can live with. And then you'll realize what the index is for.
- sigjuice 9y agoAt what point do you test these "cleanly separable" and "bisectable" patches? Do you do a second pass where you check out and build/test each of these commits?
- deleted 9y ago[deleted]
- Pyxl101 9y agoYou would compose a series of local commits and, if you wanted, test them individually before pushing them. With a bunch of changes made to your files locally, you'd use tools like `git add -i` or `git add -p` to stage subsets of your changes to make those commits. As you finish building these commits, you would be left with a series of commits to your local branch, and no additional unstaged or uncommitted changes. You're "draining" the uncommitted changes into commits, part by part. Commands that manipulate the index are how you describe what you want to do to Git. "Index" is probably not a helpful term. I think of them as simply "staged changes", that is, changes that will be committed to the repository when I run `git commit`, as distinct from local changes that will not be committed when I run `git commit`. With a Git repository checked out, just editing a file locally will not cause it to be included in a commit made with `git commit`. Rather, `git add` is how you describe that you want a change to be included in the "staged changes" that will be committed. You can add some files and not others, or even parts of a file and not other parts. The need for this doesn't come up especially often, but it's really helpful when it does. One common case where this can come up is when you've been developing for a while locally, and you realize that your changes pertain to two different logical tasks or commits, and you want to break them up. Maybe one commit is "upgrade these dependencies" and the other is "add feature X". You started upgrading dependencies while building feature X, but the changes are logically unrelated, and now the dependency change is bigger than you expected and deserves to be reviewed on its own. So with all of these changes in your workspace, you'll stage just the changes for "feature X" or "upgrade dependencies" and then run `git commit`. At this point, maybe you'll move this commit into its own branch or pull request in order to code review and ship it separately. (You might use `git stash` to save the remaining uncommitted changes while you do this.) Then you'll return to the remaining changes, which you will stage and commit as well. You've just gone from a bunch of unstructured, conflated changes to two or more separate commits on different branches/PRs (if that's what you want), that can be reviewed and shipped independently. You've gone from a massive change that's too big to review, to multiple bite-sized pieces. These tools are also especially helpful if you, for any reason, need to manipulate source control history, such as breaking up one already-made commit into several commits, or simply modifying an existing commit. To do this, you would take that commit, apply it to the local workspace as if it's an unstaged change, and then, starting from the point in history before that commit was made, stage parts of the changes again and check them in. At this point, you can push the changes as a new branch, or even rewrite history by replacing your existing branch. To give a use-case for this last capability, imagine that a developer accidentally checks in sensitive data as part of a big commit. Before (or even after) shipping the change, you realize this, so you want to go back and edit that commit, to remove the part of the change that checked in data while leaving the rest of the changes. You would describe these manipulations with the index as described in the previous paragraph.
- krupan 9y agoYou seem to miss the point that instead of staging changes out of your working directory and then committing them, you could just commit those changes out of your working directory. The extra step of staging is not needed. Everywhere you said stage you could say commit (or amend) and then not need the extra step of committing afterwards.
- Pyxl101 9y ago> You seem to miss the point that instead of staging changes out of your working directory and then committing them, you could just commit those changes out of your working directory. The extra step of staging is not needed. How would you describe this? One massive `git commit` with a ton of parameters? I don't see how it could work. How would you describe "commit the first hunk of fileA (but not the second), and the second hunk of fileB (but not the first), and all of file C?". How do you "just commit those changes"? I believe you are missing how to actually describe this on the command line or with an API. The index is absolutely needed. It's what allows you to build up a commit through a series of small, mutating commands like `git add fileC`, `git add -p fileA`. The value of the index is that you can build up your pending commit incrementally, while displaying what you've got with `git status`, then adding to it or removing from it.
- fyi1183 9y agoWhat krupan and kazinator are saying is that you would use exactly the same command as today, i.e git add -p etc. However, unlike today, those commands would automatically also do the equivalent of: if HEAD is marked as WIP then git commit --amend else git commit --special-wip-flag As somebody who understands and uses the git index, I would wholly approve of this change.
- deleted 9y ago[deleted]
- kazinator 9y agoDo you know how to use Git at all? You can build a commit using multiple small "commit --amend --patch" commands. These use the index, but only in a fleeting, ephemeral way; changes go into the index and then immediately into a commit. They go into a new commit, or if you use --amend, into the existing top-most commit. The index is a varnish onion. Git has too many kinds of objects in its model which are all bags of files. I do not require a staging area that is neither commit, nor tree. Look at GIMP. In GIMP, some layer operations get staged: you get a temporary "floating layer". This gets commited with an "anchor" operation, IIRC. But it's the same kind of thing: a layer. It's not a "staging frame" or whatever, with its own toolbox menu of operations.
- klodolph 9y agoPersonally, Yes. Often I discover a commit won’t build so I do a little bit of interactive rebasing to move some dependent change into the same commit or an earlier one.
- ajross 9y agoIt's pretty routine for a CI integration to test every patch, yeah. Not all do. (e.g. Gerrit-based systems generally do because the unit of review is a single patch, github likes to do integration on whole pull requests). It's certainly possible. I don't really understand your point. Are you arguing that it's preferable to dump a giant blob into your source control rather than trying to clean it up for maintainability?
- joesb 9y agoI think he is arguing that you should work on one feature at a time.
- ajross 9y agoAnd I was saying that as a practical matter, you don't always get that option. Individual developers working on their own code don't need the index. But then quite frankly they don't need much more than RCS either (does anyone remember RCS? Pretend I said subversion if you don't). Situations like integrating a big blob of messy changes happen all the time in real software engineering, and that's the use case for the git index.
- chris_wot 9y agoIs it really that many developers who don't split up their code into seperate commits?
- kazinator 9y agoI split work consisting of multiple changes in the same file just fine under CVS and Quilt. I would convert the change to a unified diff, remove the change, and then apply the selected hunks out of that diff with patch. ("Selected" means making a copy of the diff, in which I take out the hunks I don't want to apply. Often I'd just have it loaded in Vim, and use undo to roll back to the original diff and remove something else.) Using reversed diffs (diff -uR) I used also to selectively remove unwanted changes, similarly to "git checkout --patch" This is basically what git is doing; it doesn't require the index. The index is just the destination where these selective hunks are being applied.
- krupan 9y agoDid you read parent comment? How is dealing with the thousand-line change not possible with what they described (hint, it's totally possible, no index needed)?
- WhyNotHugo 9y agoHOW is it possible? Imagine I have a file with lines 10 and 150 changes. How do you commit just one without some form of index (or alike)?
- kazinator 9y ago> or alike Well, that's the weasel word. In my grandparent comment, I proposed the "alike", didn't I? Nowhere did I say, just remove the index from Git, but don't replace its functionality with any other representation or mechanism. In git, we can do that today in such a way that the index is only temporarily involved: $ git commit --patch ... interactively pick out the change you want ... $ # now you have a commit with just that change It is not some Law of Computer Science that the above scenario requires something called an "index", which is a big archive holding all of the files in the repo, where these changes are first "staged" before migrating into a commit.
- carussell 9y agoThe problem is not that Git supports staging partial changes. The problem is that Git has shoehorned a tool that "at some point you're going to want"—to help you deal with a rare occurrence—into the default workflow, forcing you to deal with the overhead of staging every time.
- YZF 9y agoIt's basically the overhead of typing -a ... so git commit -a rather than git commit. It's not such a big deal. it does take a while to get used to the git "pipeline" tbh but when the rare occurrence happens you have this option, on source control systems without this option you just don't have it.
- carussell 9y agoYou're minimizing the overhead. `git commit -a` also won't help you with new or renamed files. So when you write about "the overhead of typing -a", what you really mean is the overhead of 1. typing `git commit`, checking the output, then typing `git commit -a`, or 2. typing `git commit`, then moving on with your life, and realizing minutes, hours, or days later that the changes you meant to include were not actually included, so you have to go back and add them if you're lucky, untangle them from whatever subsequent changes you were trying to make and/or do an interactive rebase if you're unlucky, and maybe face the prospect of doing a `git push --force` if you're really unlucky Scale that up to several days or weeks to match the learning period and repeat for every developer who has to sit down and interact with it. That's the overhead we're talking about. The article got it right; this is a monumental waste of human effort. > Every developer has a finite number of brain-cycles
- kazinator 9y agoI've never used a version control system that didn't have to be notified about which files you would like to add. "vc commit" cannot simply pick up all files and put them under version control, because you have junk files all over the place: object files, editor backups, throwaway scratch and test files and so on. But even when we use "git add", we are not aware of the index. The user can easily maintain a mental model that "git add" just puts files into some list of files to be pulled into version control when the next commit takes place. That is, until that silly user makes changes to the file after the git add, and those changes do not make it in because they forgot the "-a" on the commit.
- kazinator 9y agoWhat I described supports staging small changes and turning them into individual commits. Just the staging area is a commit object rather than a gratuitously different non-commit object with different commands and command options to deal with it.
- ajross 9y agoYou'd still need separate commands, though. Commit vs. commit --amend. Add vs. add -p. Diff-against-grandparent vs. diff --cached. You just want different separate commands to achieve your goal, which is isomorphic to the index. So sure: if you want a not-index which works like the index and has a bunch of 1:1 operations that map to the existing ones, then... great. I still don't see how that's much of an argument for getting rid of the index.
- kazinator 9y agoWell; yes; the tool can't read your mind whether you'd like to batch a new change with an existing one, or make a separate commit. Remember that git, initially, didn't hide the index in the add + commit workflow! You had to "git add" and then "git commit". So the fact there is only "git commit" to do everything is because they realized that the index visibility is an anti-pattern and suddenly wanted to hide it from view. Since the index is already hidden from view (and largely from the user's model of the system) in the add + commit workflow, we are not going to optimize the command set by turning the index into some other representation. That's not what this is about. The aim is consistency elsewhere. For instance, if the index is an actual commit, then if we abandon it somehow, like with some "git reset", it will be recorded in the reflog. Currently, the index is outside of the commit object model, so it gets destroyed. It's possible for a git index to have content which doesn't match the working tree; in that case when the index is lost with a git reset, that content is gone. If the index is a commit, it can have a commit message. It can be tagged, etc.