(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=41449128

Git 跟踪对文件所做的更改并在不同阶段维护其状态:未修改、暂存和已提交。 暂存文件已准备好包含在下一次提交中,并且通过“git add”跟踪对暂存文件的修改。 但是,在提交之前在“git add”之后更改文件会导致这两个语句都不正确。 例如,“git add -p”很有帮助,但需要额外的步骤来取消暂存文件。 与其最初添加整个文件,然后使用“git add -p”进行后续更改,不如以交互方式暂存文件的特定部分会更方便。 Magit 等工具中存在此功能。 这些工具允许用户在文件中暂存单独的行或块,而不需要线性暂存。 当用户希望删除暂存部分时,他们可以撤消(Magit 中的“ds”命令)暂存。 了解 Git 的内部操作可以提高其使用和推理能力,但文档有时可能会被忽视。 考虑到 Git 文档的详细程度,作者对此感到惊讶。

相关文章

原文


> Files in Git can be in one of these three states: Modified, Staged, Committed

> Staged: The current version of the modified file is staged to be included in the next commit.

A bit of a nitpick, but if I change a file, "git add" it, and then change it again, both of these statements are false.



`git add -p` is such a nice utility. Sometimes I do wish that it could also be used for unstages files, so that if I'm introducing a new file, I could still break its contents up into multiple commits.

Of course, the workaround there is that one adds the initial file into the staging area and then `git add -p` the subsequent changes. It could just be a bit more convenient on that front, is all.



It can, you just gotta do a magic incantation first.
  git add -N file
  git add -p file
The first command signals to git that you intend to add the file. That makes its entire content show up in the patch editor.


TIL! I seem to have just missed the `-N`/`--intend-to-add` while perusing through the `git-add(1)` manual.

Heh, it[0] even notes a similar use case:

> `-N` > `--intent-to-add` > > Record only the fact that the path will be added later. An entry for the path is placed in the index with no content. This is useful for, among other things, showing the unstaged content of such files with `git diff` and committing them with `git commit -a`.

[0]: <https://git-scm.com/docs/git-add#Documentation/git-add.txt--...>



Magit does interactive staging (and unstaging) a lot better than git itself does.

In d u, you can “s” on a file or hunk and it’ll stage just that. And if you select lines (c-spc?) it’ll stage just those lines.

To unstage, go to d s and use “u” the same way.

The massive advantage aside from line-wise staging is that you don’t need to stage linearly.



I think if the word "Files" was replaced with "A change [in a file]", then the statement holds true. Perhaps a better phrasing:

> In Git, a change in a file, can be in one of these three states: unstaged, Staged, Committed



“Objects” rather than “blobs”, in git “blobs” means specifically file contents (/ unstructured as technically you can use blobs for arbitrary storage) but all objects can be delta’d during packing.



Nice article. What is interesting to me is the reactions to articles like this. Not the fact that the git internals are not widely known, I mean that is true for nearly any more complicated topic. In this case I mean the fact that this is actually well documented.

Don’t get me wrong. I think articles like these help a lot to demystify git and I believe it makes the tool easier to use and reason with when one knows what it does. But why is nobody finding or reading the later chapters in the docs?

https://git-scm.com/book/en/v2/Git-Internals-Git-Objects



Creating a Git commit using low-level commands was always something I wanted to do, but I never found the time to really deepen my knowledge of Git. I have actually googled if I could find a blog post or something in this topic, but I've failed to find one. Finally, I got the chance, and for the past couple of weekends, I’ve been reading the Pro Git book (which it seems it's the same content as git-scm.com/book). I believe it’s a good practice to write a blog post about a topic after finishing a book (teaching is a good way of submitting knowledge in memory). To my surprise, creating a Git commit using plumbing commands was already covered in the final chapters of the book. I thought it would be a good idea to simplify that process and write a blog post which can be read under 10 minutes, allowing those who haven’t read the book yet (like myself in the past) to get a basic understanding of what Git is doing under the hood.

> But why is nobody finding or reading the later chapters in the docs?

I think to read the latest chapter of a book, one usually needs to read the earlier ones too. I personally don't jump directly to the internals when I want to read about something, because I'd then assume I am missing a lot of context and background.



> But why is nobody finding or reading the later chapters in the docs?

Because most people don’t ever read the book period. 90% of users follow a basic tutorial or instruction sheet to get the five commands they’ll use by rote and go no further.

And, separately, the internals section of the book are mostly uselessly shallow, so if you start digging into that you quickly forget that the book even has one such section.



> git

On the topic, I'm just going to plug the tool git-branchless. Completely transformative for my use of git at work. Stacked commits that actually work!



Fantastic article! It seems to me that the flexibility of low-level git objects would lend itself to be embedded in other software systems for version control purposes (e.g. tracking changes in a CMS)



I used to struggle with formatting my Git commit messages and often forgot the necessary Git commands.

Now, I've found a utility (made by my brother who shared it with me a few days ago and I told him to opensource it since I liked it soo much) that handles all the formatting and rewriting for me. I just write my commits, and it takes care of the rest.

Here's a video demonstrating this magic (though it's for rsync): [asciinema.org/a/mktcuXSPDTr2Mp1XVbo5tqhK1](https://asciinema.org/a/mktcuXSPDTr2Mp1XVbo5tqhK1).

Check out the utility here: [github.com/zerocorebeta/Option-K](https://github.com/zerocorebeta/Option-K).



The last thing I want is for some LLM to tell me to rm -rf /. Neat POC but the tech just isn't there yet and I hope that everyone on HN who isn't shilling an AI product knows that.



That repo is only nine hours old, so I’m assuming you wrote this tool yourself. If so, I think the lack of a disclaimer (that you’re promoting your own tool) is somewhat dishonest.

联系我们 contact @ memedata.com