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?

1
  • You've used SVN, which can't do this either. Jun 12, 2013 at 21:31

5 Answers 5

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.

6
  • 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, 2013 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. Nov 8, 2016 at 6: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. Nov 8, 2016 at 7: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, 2019 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, 2020 at 19:47
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]

2
  • 5
    But then you will only commit that file. Aug 9, 2018 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, 2023 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
1
  • 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 5, 2017 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.