Where should i commit... in main branch or inside a folder in main branch?

I have a folder named “My projects” — master branch…
Inside that folder i have a folder named “First Project”
I made a change in the html file of “First Project”…
When i tried to commit in the “First Project” … the html file is commited and it was said that “Your branch is ahead of ‘origin/master’ by 1 commit.”
So i again moved to “My projects” folder and typed “git status”
Then i got a message saying that “Modified First Project”
So i comited again…

So…my question is what is diff between commiting inside the “My Projects” folder and commitng inside the "First Project " folder…

It’ll be adding changes from the current working directory, so when you are in that subdirectory, you’re adding changes from inside there, then when you move out, there may be more changes visible outside of that directory.

Solution is to always commit from root, it doesn’t make sense to do anything else, committing in a subdirectory makes it very easy to make errors and gives you no benefits.

1 Like

Thanks @DanCouper
So…always we should commit in the root directory…right?

Yes, that’ll avoid any issues.

This doesn’t sound right.
In my experience you can run your git commands anywhere in the folder structure without any issues. The status command might show different paths, but this is only to show you what files are changed relative to your current working directory, but doesn’t have any effect on how the files are “saved” into git.

The behavior mentioned in the OP sounds like the situation where you have a nested git repository. Odds are you have a .git folder in the “My Projects” folder, and a .git folder in your “First Project”. Unless you want to have all your projects versioned controlled together (like a monorepo setup) odds are you don’t care about changes across all projects, you only care about changes on a project level.

Depending on what you want to do is up to you. If you don’t want to track changes across all the projects, just deleted the .git folder in the My Projects folder, this will prevent any more tracking. Or you could just leave it and never use it again.

NOTE: On windows you might not see this file in the folder as it is a “hidden file”. So if you don’t see this folder in your My Projects folder, (or your “First Project” folder) make a quick google search to find out how to see them.

It does depend on the command: it should normally include the whole tree, but it doesn’t, not always — I can’t go off and check which specific commands this relates to at the minute, but I’ve had the same situation as the OP several times. Not commonly, but sometimes, and it’s not to do with nested repos, its to do with where the command is executed from inside the project (and me attempting to stage files for the project in a subdirectory when there are unstated changes outside that subdirectory which don’t get picked up)

Of course you better commit to your sub not your main, so that in case you did something wrong, you will have an original branch to return to.
anyway, the message "Your branch is ahead of ‘origin/master’ " is because after you committed you are supposed to do a PR, which will merge the new change to the main branch, but be careful!! only PR when you are completely sure the new modification is 100% efficient and as much bug-free as possible. you don’t want to be in a situation where you have no backup version to return to.
my prefer way is to have Main > sub Main > branches
I consider my sub Main is my main, I only PR to it after am sure my new modified branch is good-to-go, I leave my MAIN until my project is completed, Tested, and quality approved.