Backtracking in Git. Is there is a way to commit one file when we have a lot of files that are in the staging area?


Hello! I have a question about git backtracking. For example, if I add a lot of files to the staging area simultaneously, and then I want to commit each file separately, how can I do it?
If I use commant git add -m “commit message” it will commit all the files in the staging area with this commit message. How to do it separately? Maybe there is a way to define certain file? Or the only way is to add file to the staging area, than commit it, and than do it with another file? I think it takes a lot of time?

Hi there.

I’m not entirely sure why you might want to commit each file individually, but you can selectively commit files by telling Git which ones you want to commit.

Let’s say you’ve got the following:

|       dasmotos-arts-redline.jpg
|       index.html
|       style.css

You can selectively commit files by specifying them to the commit command. If we imagine that I’ve made changes to several of these files, but I only want to commit the changes to index.html, style.css, and frames.jpeg, I can do this:

git commit -m "style tweak, fixed typo, reduced img file size" index.html style.css frames.jpeg

Git will then only commit the files I specified at the end of the command.

If you’re stuck on how to use any of the commands in Git, it has a pretty comprehensive help system. You can see the help for any command by running git [command] --help - so for the commit command, running git commit --help from the terminal would bring up the relevant help.


Thank you so much for the answer!

1 Like

No problem. :slight_smile: You know where we are if you get stuck with anything else.

1 Like

I would like to add that committing one file at a time is uncommon.

When you have a lot of changes, you might not want to have all these changes in a single commit, but generally you then group the files by feature or fix (there is always a logic way to group these files). This way, its easy to find changes you made.


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.