3

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!

Community
  • 1
  • 1
Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
  • Assuming you would need some form of both 'add' and 'commit' in the dual command, how much time are you spending in order to effectively save yourself from typing 'git' (+ a space) twice? – mcalex Apr 09 '15 at 08:33
  • @mcalex It is all about your cognitive load, more things to think about, more chances to mistype, for what? – Dmitri Zaitsev Apr 10 '15 at 05:26

1 Answers1

3

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.

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
  • Thanks! Any idea what kind of potential problems? I am a bit worried of "messing with system". – Dmitri Zaitsev Apr 16 '15 at 02:58
  • 2
    Yes, simply adding files you never intended to have under version control. By having the add and commit separate you have the option of running `git status` just to double check that you are only adding files you expect, and not the contents of some subdirectory that slipped your mind. Removing something from the staging area is a lot simpler than removing it from three commits ago, especially if you've shared that commit with other repos. – Chilledrat Apr 16 '15 at 09:02
  • I understand this is a valid problem but don't see how non-providing a simple solution helps to solve it. As I am busy typing `git add .; git commit` again and again, the last thing I do is an extra `git status` unless things go wrong, when it is usually too late ;) – Dmitri Zaitsev Apr 16 '15 at 10:02
  • 2
    You can't stop people shooting themselves in the foot with git, but making it harder for them to do without overburdening normal users is probably the right strategy ;) – Chilledrat Apr 16 '15 at 10:35