9 ms·
> 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
by jeroen 2y ago
> 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.
- jjmarr 2y agoI use git add -p somewhat frequently to do partial staging of a file to split up my changes into multiple commits.
- sham1 2y ago`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.
- matheusmoreira 2y agoIt 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.
- sham1 2y agoTIL! 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--N https://git-scm.com/docs/git-add#Documentation/git-add.txt--...>
- tczMUFlmoNk 2y agoAlternately: git add file git reset -p file
- johnisgood 2y agoWow, I have been using git for ages but I did not know about this. I was relying on magit (for Emacs) and git-cola.
- ht85 2y agoYou can also discard changes that way, e.g.: git checkout -p -- .
- johnisgood 2y agoI will have to read about how to use it, because it shows some hunks on a page, and I do not want to stage all of them, for example.
- jeroen 2y agoWhen it shows you a hunk that's bigger than you like, you can use 's' to split it into smaller hunks.
- johnisgood 2y agoThank you!
- harry_ord 2y agoCherrypick (-p) is wonderful. A command I also like is rebase interactive(-I) Git rebase -i HEAD~[number of commits]
- johnisgood 2y agoYeah, I use `git rebase -i HEAD~n` a lot.
- masklinn 2y agoMagit 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.
- gurjeet 2y agoI 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