3

Main question

Pushing all local changes since the last commit and push is an operation very frequently used when working with git, so it may be beneficial to optimize its usage into one command that would have a script like this as its backbone:

git add * && git commit -m "my-message" && git push

But for some reason this failed in the repo I tried to use this in. After some searching, I found out using echo $? that when ignored files are present git add * returns 1. This is usually not a problem as it doesn't effect typing out the next command, but in a script it is critical to be able to automatically decide whether to continue. The obvious workaround to this problem is simply using

git add * || git commit -m "my-message" && git push

but I want to avoid this as it continues execution even if actual errors happen in git add.

Is there a configuration option (or any other solution) that makes git report an exit code of 0 when ignored files are included in the add command but no other error happens?

PS

  • I know that automating this is not a very wise use of time, that is not the purpose.
  • This is just the most simplified version of the code that fails, the actual script doesn't use hard coded commit messages.
sisisisi
  • 481
  • 7
  • 17
  • 1
    `git add *` why `*` and not `-A `? `Is there a configuration option` that makes no sense, just check if there are any modification, if there are, then add the files... https://stackoverflow.com/questions/3878624/how-do-i-programmatically-determine-if-there-are-uncommitted-changes – KamilCuk Jul 29 '20 at 23:27

1 Answers1

4

This is caused by * being expanded by Bash to all the non-hidden files in the current directory (by default) and passed as separate arguments to git add. git complains because some of these explicitly named (as far as git can tell) files are ignored, so the command is ambiguous.

git add . will do basically the same thing, with the only exception of adding dotfiles, and returns exit code 0 even if the current directory includes ignored files.

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • I ended up using `git add -A` [as suggested in the comments](https://stackoverflow.com/questions/63163868/git-add-can-i-force-exit-code-0-when-the-only-error-is-the-presence-of-ignored/63165591#comment111694511_63163868) as it's the cleanest solution, but this answer cleared up the differences between the options. – sisisisi Aug 04 '20 at 04:13