17

I am new to git having previously used Perforce, SVN, source safe and many other source control tools.

I am looking for the functionality that I used to use in Perforce where I could construct a change list; I was able to add files to the change list and provide a comment specific to each file.

git has a staging area into which changed files are added, is there a way to provide a per file comment when adding a file to the staging area?

Or perhaps at the comment stage I can add a per file comment; I have had a good look and not been able to workout if either how to perform either - in fact from what I can seen neither is possible.

Anyone have any ideas how I could do this?

eklektek
  • 1,083
  • 1
  • 16
  • 31

5 Answers5

20

Git does not provide such feature. The Git philosophy is to track 'content', not 'files'. Adding files to the staging area allow you to prepare precisely your commit. If several files are added to the staging area, it's because there are linked to the same feature. That's why the commit message represents the whole change.

If you need a message per file, you may consider creating several commits on a feature branch, with only one file per commit.

Hope this helps.

Guillaume Darmont
  • 5,002
  • 1
  • 23
  • 35
  • 1
    Thanks for pointing this out - I need change the way I have been thinking and consequently using git. Helps a lot. – eklektek Jun 12 '13 at 20:41
  • 1
    By this logic in every commit there should only be "one" localized change in a single file which happens very rarely and usually for very minor adjustments. Otherwise that commit message cannot _properly_ represent the whole change, since the changes _are_ distributed "all over the place", but your commit message is not. So this logic is flawed since, yes, git 'thinks' in content, but you still think in files when you organize that content. – Zuzu Corneliu Nov 08 '16 at 06:44
  • 1
    To finish the idea, it would still be very useful for Git to allow comments per-change or blocks of changes ("block" as in perhaps allowing logically relating a subset of the changes to a comment), if not per-file. It would make describing/understanding a change-set much more pleasant for both the _committer_ and the _reviewer_. – Zuzu Corneliu Nov 08 '16 at 07:46
  • 3
    I am not a philosopher but I think it is a useful feature for those who want to review the file change. Not always but in many occasions it can help them to understand the changes better. – MehranTM Jan 28 '19 at 14:56
  • @ZuzuCorneliu My understanding of the git philosophy is that you shouldn't be committing large enough changes to warrant an approach that would require a per-file message. If you make a change spanning multiple files to implement a feature, you stage just the changes for that feature, then commit for that feature with a single comment explaining the change. – Andrew Oct 20 '20 at 19:47
  • To clarify: This may be a personal preference, but I think if the commit is too complex to explain with a single comment, then you're committing too much at once and need to break it up into smaller chunks. I.e. If a new feature includes a library change, new file, and existing file change, each of those would be a separate commits in my book. – Andrew Oct 20 '20 at 19:59
16

These days you CAN add commit messages to individual files. I just did this for instance:

git commit -m 'reference containers in app' src/App.js

Context: multiple files added to git through $git add . THEN: commit message on this individual file (src/App.js).

[posting answer since this still comes up in google]

Katinka Hesselink
  • 3,961
  • 4
  • 20
  • 26
  • 5
    But then you will only commit that file. – Homero Esmeraldo Aug 09 '18 at 14:08
  • @HomeroEsmeraldo it mean you can create a program to push into git per file with that command. no need to do it manually, once the program done, its easier to manage the package in the future too – Micah Mar 30 '23 at 22:43
0

You can just go to the folder after some change do:

git add .
git commit -m 'mssg' 

and go back and push it:

git push origin master
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Would it be possible to edit the answer to mark the `git ...` lines as code? (just indent every code line by 4 spaces or use the button in the interface). I can't submit an edit because I don't reach enough added characters for the edit to be submittable. – GPhilo Sep 05 '17 at 14:12
0

In GitGui, you can select a file in the Unstaged Changes, then go to "Commit>Stage to commit". That will stage only the selected file(s)

-3

In each file in git you will see '+' when you do hover, click on that add your comments for each file.