Reputation: 46178
I got into the habit of using .gitignore
and always doing git add -A
, git status
to check the modified files, then commit, so I don't need to remember about the files that I have modified and other developers don't need to know which files are ignored.
I still see other people doing git add files
for each modified file.
So I was wondering whether there is any advantage of adding each file.
The question is: why not always using git add -A
?
Upvotes: 2
Views: 1243
Reputation: 3775
When you are developing a feature, you may also fix some bugs or misspells that you found while browsing the code. Those corrections are not directly related to your feature, so maybe you want a separate commit for them. git add [something]
allows you to work in this way.
Upvotes: 3
Reputation: 9385
Sometimes it's useful to be able to have a file in the git repository, not have it listed in .gitignore
, and make local changes that you don't want to commit.
For me, this applies for example to Django's settings.py
files, where I sometimes make changes to accommodate local database settings and the like. You still need to occasionally change bits of those files, e.g. adding bits to INSTALLED_APPS
, which you can still commit using git add -p
. The alternative would involve having a settings.py.example
file, and adding settings.py
to .gitignore
, but then you need to manually "merge" changes from the example file into your actual settings file. If you haven't changed settings.py
, you can just use git add .
Another use-case is if you have made two sets of changes, which you want to commit independently of each other. Then you can use git add [-p] <file>
, commit, and repeat, as appropriately. Of course you need to ensure that your diffs are truly independent.
Upvotes: 1