5

After making changes to files already tracked by git I usually do a:

git commit -a -m "a message describing what you did"

followed by

git push origin master

to commit and then push all changes to my Github account. The first command however does not work for commiting files either added or removed. To do that I would have to first type:

git add -A

as mentioned here: How to commit and push all changes, including deletes?.

In this other question Git commit all files using single command the accepted answer indicates that the only way to apply the full commit (ie: including files added or removed, not only edited) and then the push is by combining them with the && command like so:

git add -A && git commit

which in my case would look like:

git add -A && git commit -a -m "a message describing what you did" && git push origin master

Now for the question(s): is this large command correct? Is there a way to create a new git command to apply these three commands all at once? Furthermore: is this merging of commands recommended or discouraged? If so, please state the reason.


Edit

I immediately regret this decision: Edit last pushed commit's message.

Community
  • 1
  • 1
Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • also make sure to keep .gitignore up to date if you will always be adding all. – bryanmac Nov 29 '13 at 13:55
  • I've found so much use for the correct answer on this thread, so I enjoy the fact that you asked the wrong question, got the right answer to that question, figured out how to do what you really intended to do, then other people continually benefit from the answer to the original question. I love Stack Overflow – Dave Helms Jul 31 '18 at 07:32
  • Duplicate of [How can I stage and commit all files, including newly added files, using a single command?](https://stackoverflow.com/questions/2419249/how-can-i-stage-and-commit-all-files-including-newly-added-files-using-a-singl), which explains that you don't need the `-a` option of commit after you've already done `git add -A`. – Nickolay Aug 20 '20 at 02:00

4 Answers4

13

Looks correct to me.

You could create an alias for the command by executing

git config --global alias.commitall '!func(){ git add -A && git commit -am "$1" && git push origin HEAD; }; func'

Then you could commit all changes by entering

git commitall "a message describing what you did"
jacob1123
  • 361
  • 3
  • 12
  • I'm having trouble with this command too. It fails with: `Expansion of alias 'commitall' failed; '!func(){' is not a git command`. What am I doing wrong? – Gabriel Nov 29 '13 at 14:04
  • 1
    @Gabriel @Stoic's version seems to be missing quotes. This line works for me: `acp = "!f(){ git add -A && git commit -am \"$1\" && git push origin master; };f"` (Can't comment on his answer yet) – jacob1123 Nov 29 '13 at 14:11
  • Thanks Jacob, the modified version works now. And thank you for pointing out the error in Stoic's version too. – Gabriel Nov 29 '13 at 14:16
  • Note to self: this command works in all branches (not just master): `git config --global alias.commitall '!func(){ git add -A && git commit -am "$1" && git push origin; }; func'`. Notice the lack of `master` after `origin`. Otherwise you get the "Everything up to date" issue, (see: http://stackoverflow.com/a/2936751/1391441) – Gabriel Jun 03 '15 at 19:44
  • Please don't just copy-paste this, first notice that it will commit everything to master, even if you're working on a feature branch. I personally removed the `&& git push origin master` bit. this separates the "local" commit with the pushing of your code to origin – Joey Baruch Jan 24 '18 at 11:26
  • 1
    @joeybaruch good catch. I've updated my answer to push to the current branch. – jacob1123 Jan 24 '18 at 17:24
3

Yeah. That large command is actually correct. It will allow you to commit everything inside a given directory in a repo, including newly created / untracked files.

Yes, you can create a new git command (actually, an alias) like this:

[alias]
acp = "!f(){ git add -A && git commit -am "$1" && git push origin master; };f"

The above alias must go inside your global git configuration (at ~/.gitconfig) under section labelled [alias]. Now, you can write something like: git acp <message> to run the above command.

Merging of commands is definitely recommended and is the power that *nix provides. However, in the current case, make sure that you actually want to perform all these actions in a single step. Git assumes that you want to selectively add your changes.

Stoic
  • 10,536
  • 6
  • 41
  • 60
  • I copy/pasted the command above into my `~/gitconfig` file but running it returns the error: `f(){ git add -A && git commit -am $1 && git push origin master: 1: f(){ git add -A && git commit -am $1 && git push origin master: Syntax error: end of file unexpected (expecting "}")`. – Gabriel Nov 29 '13 at 13:58
  • 1
    It seems that my answer missed the quotes. Have updated the answer. – Stoic Nov 29 '13 at 16:34
1

Recently I wrote a git-powercommit script that commits all changes in a Git repositary. Currenlty it ignores removed/created files, but this feature shouldn't be hard to add. From the other hand, it does support a rich workflow involving stash/pull/push and also handles submodules.

Grwlf
  • 896
  • 9
  • 21
0

If you feel bad about typing three lines again and again, you could also use GUI tools. http://www.git-scm.com/downloads/guis By the way, as "Stonic" said, you could always create a new git command.

Kush
  • 755
  • 7
  • 22