Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: Commit multiple files but add messages

Tags:

git

github

I have tons of files in my repo, and sometimes I work on 20 files and I want to commit all of them. But, I would like to add a message for each.

How can I add all of these files that have been updated and also add a message for each without having do run a command for each one manually? Is this possible to do a bulk run and have it prompt me to add a message for each one?

like image 211
Nic Hubbard Avatar asked Mar 02 '11 17:03

Nic Hubbard


People also ask

How do I write multiple commit messages?

Another method of adding a multi-line Git commit message is using quotes with your message, though it depends on your shell's capacity. To do this, add single or double quotes before typing the message, keep pressing enter and writing the next line, and finally close the quote at end of the message.

Can you git commit multiple files?

1 Answer. You can add all the files using the git add command like below for all the files you want to add, which will add all your files to the staging area, which means the files are ready to be committed. Now commit your files, the editions or addition made in the files will now be saved.

How do I add a message to a commit?

On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter. In your text editor, edit the commit message, and save the commit. You can add a co-author by adding a trailer to the commit.


1 Answers

(A note: You shouldn’t think about individual files, nor should you commit a dozen unrelated changes at once. Think about your changes in terms of logical groups. You can commit as you go, or you can commit each file one at a time. But really try to make one conceptual change, commit it, and move on to something else.)

If you have lots of changed files, you can just add them all and use git commit, for the editor window contains a list of changed files which you can incorporate into your commit message. But if you want to be prompted for each one, you can loop through them and commit them individually, then squash those into a single commit.

  • Write down where you are; we will squash on top of this later:

    $ git rev-parse HEAD
    57c1df83f140b2dedbdf8a44ee623654d37bd1c3 
    
  • Loop through the modified files (note that untracked files are not considered here) and commit each one individually:

    $ for file in $(git ls-files)
    >   do git add $file
    >   git commit  # Write a commit message for the file.
    >               # It might be a good idea to use a format like "filename: changes"
    > done
    
  • Squash everything:

    $ git rebase -i 57c1df83f140b2dedbdf8a44ee623654d37bd1c3
    

    Change every line but the first from pick to squash, then save and quit.

like image 197
Josh Lee Avatar answered Sep 23 '22 12:09

Josh Lee