Reputation: 41
I am trying to understand this git command. I have got a software repository which has got submodules and that submodule points to another submodule. In order to check out, this command is used -
git submodule foreach --recursive 'git checkout master || :'
This question is about understanding this command. What does "|| :" means in this command? I am asking this question so that I can be more flexible in changing these commands according to the need.
In this command - foreach, means for each submodule found go and do a checkout of master but what "|| and :" means here.
Upvotes: 1
Views: 793
Reputation: 2217
command_a || command_b
leads bash to execute command_b only if command_a was not executed successfully (command_a returns exit code diffrent to 0). :
is an alternative notation for true
which is equal to exit code 0
So git checkout master || :
will always return true even if git checkout master
failed.
Just writing git submodule foreach --recursive 'git checkout master'
would interrupt if the checkout was not successful for one of the submodules.
Upvotes: 0
Reputation: 164919
From the git submodule
docs for foreach
...
A non-zero return from the command in any submodule causes the processing to terminate. This can be overridden by adding || : to the end of the command.
It says to keep processing submodules even if the command fails on one of them.
If you're familiar with make
, it's similar to putting a -
at the front of a command then make
will then ignore any errors from that command.
Upvotes: 0
Reputation: 387805
git submodule foreach --recursive '<command>'
This runs the <command>
in each checked out submodule.
So that leaves the command itself to understand:
git checkout master || :
git checkout master
should be pretty clear.
||
is an OR control character. For command1 || command2
, command2
is executed if command1
returned a non-zero exit status. So basically when command1
failed.
So in this case, :
runs when git checkout master
failed.
The colon on the other hand is an alias for true
in Bash. So effectively, this command will not do anything, making the full command run git checkout master
if it can or it will silently fail.
Upvotes: 3
Reputation: 240010
:
is a shell command that does nothing and always succeeds (similar to true
). So by adding
|| :
to a commandline, the overall command will always be successful, even if the command on the left side of the ||
failed. In this case it causes git submodule foreach
to continue even if one of the submodules isn't able to checkout master
.
This usage is suggested by the git documentation:
A non-zero return from the command in any submodule causes the processing to terminate. This can be overridden by adding
|| :
to the end of the command.
Upvotes: 1
Reputation: 212268
cmd ||:
is just a way to ensure that the command always succeeds. If cmd fails, then :
is executed and succeeds, so that $?
will always be 0. So basically your submodule foreach is ignoring any errors checking out master.
Upvotes: 0