4

What I am interested in doing is creating an alias that adds all files, commits with a message, does a pull, if there are any conflicts stop and show a list of conflicted files, otherwise push.

I have already found an alias to list conflicted files (git config --global alias.conflicts "diff --name-only --diff-filter=U"), but I have no idea how to integrate the rest of the commands.

Is it even possible to create an if statement in this format?

Pseudo code (multi-line for readability):

git config --global alias.commitall '!func(){ git add -A && git commit -am "$1" &&
             git pull && <conflict detection and possible die of command> &&
             git push; }; func'
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • That's a terrible idea. Instead of making it even easier to add the wrong content and push it automatically, use `git add -p` – user229044 Feb 04 '14 at 12:12
  • While I can see the benefits of using `-p` (had to look it up in a manual - http://goo.gl/BiRXFB - to see what it does) I'm going to stick with `-a` because if I have added/deleted files/directories then I want those changes to be tracked as well. I also don't want to be accepting every file change as this may take a LONG time if committing a large number of changes. That's just my opinion though. – Richard Parnaby-King Feb 04 '14 at 12:33
  • I usually just create shell functions or aliases for this type of thing. – bundacia Feb 04 '14 at 12:46
  • @bundacia This is what I am attempting. What are your aliases? – Richard Parnaby-King Feb 04 '14 at 12:59
  • @RichardParnaby-King The situation you describe is exactly when you would want to use `add -p` and when you *shouldn't* use `add -a`. Sooner or later (probably sooner based on what you describe) you'll commit something you didn't mean to. – user229044 Feb 04 '14 at 13:08

2 Answers2

8

There is no need to add a conflict check into the alias. If a conflict is detected on git pull then it automatically echos out the files that have conflicts and stops. This allows the alias to reduced to the following (multi-line for readability):

git config --global alias.commitall '!func(){ git add . && git commit -aqm "$1" &&
    git pull -q --no-progress && git push -q; }; func'

I have added the -q argument to stop the calls from echoing the normal bumpf, but that is preference.

Usage:

git commitall "message goes here"
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
2

I think the if potion would look something like this

if [[ -n $(git diff --name-only --diff-filter=U) ]]; then
    git diff --name-only --diff-filter=U                 
else                                                     
    git push                                             
fi         

This answer helped

Community
  • 1
  • 1
bundacia
  • 1,036
  • 5
  • 13
  • That if block does work. Integrating it into the above alias command, I get `git config --global alias.commitall '!func(){ git add -p && git commit -am "$1" && git pull && if [[ -n $(git diff --name-only --diff-filter=U) ]]; then git diff --name-only --diff-filter=U else git push fi; }; func'` which, when I run it, gives me :`syntax error near unexpected token '}'`. Any ideas how I fix this? – Richard Parnaby-King Feb 04 '14 at 14:15
  • The syntax error was caused by having the if then else fi on one line. Thanks :) – Richard Parnaby-King Feb 04 '14 at 15:14
  • Yeah, you'd need a semi-colon after the else – bundacia Feb 04 '14 at 17:30