Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git add and commit all files in one _native git_ command?

The most common Git command sequence is:

git add .
git commit -m "Message"

I have searched for a native Git command to do it with one line but surprisingly could not find it. There are at least two big threads concerned with this question HERE and HERE, but surprisingly they have high voted answers not doing the job or doing it using additional hacks.

The top voted solution in the first thread linked above suggest to define a macro

git config --global alias.add-commit '!git add -A && git commit'

which has the downside of having to do it again and again for every new software environment. The second voted solution

git commit -a -m "message"

does not work - it does not add new files! Other "solutions" involve writing the same two commands in a single line - this offers no advantage in terms of typing!

Further, Pro Git - one of the well-known books states on page 22:

2.2.7 Skipping the Staging Area

Although it can be amazingly useful for crafting commits exactly how you want them, the staging area is sometimes a bit more complex than you need in your workflow. If you want to skip the staging area, Git provides a simple shortcut. Providing the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part...

The emphasis is mine - this is indeed only works with files that are already tracked. But your new files are never tracked (or I can I change setting to make the being tracked?), so you can't have the added and committed using that command!

I am looking for a clean single line solution provided by the standard git software without additional hacks. Given so many rich advanced options of Git, it is hard to believe such a command does not exist. If there is anything I am missing here, will be happy to learn!

like image 823
Dmitri Zaitsev Avatar asked Oct 20 '25 14:10

Dmitri Zaitsev


1 Answers

To my knowledge there isn't a native command for this, and the questions you have already linked to seem to support this view. Probably because it has the potential to cause problems, although you yourself may have good reasons for wanting to work this way.

You can however tweak one of the commands you have already mentioned:

git config --system alias.add-commit '!git add -A && git commit'

Replacing --global with --system will configure the alias for every account on a single machine in one shot. You will of course need to run the above command with an account that has permission to write to the system gitconfig file in /etc

Not the perfect answer but it might get you closer to what you want, at least from a practical standpoint.

like image 74
Chilledrat Avatar answered Oct 22 '25 02:10

Chilledrat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!