I need to check in a bash script if there is differences between 2 branch in a git repository. I know it is possible to see differences with git diff. However i need to use it in if.
how can i do that?
For example:
git diff ......
if [ $? -eq 1 ]
then
echo "will do something in here"
fi
CodePudding user response:
Here's an example:
#!/bin/bash
# Set the branch names
BRANCH1="master"
BRANCH2="my-feature-branch"
# Check if there are differences between the two branches
DIFFERENCES=$(git diff --name-only $BRANCH1 $BRANCH2)
# If there are differences, print a message
if [ -n "$DIFFERENCES" ]; then
echo "There are differences between the $BRANCH1 and $BRANCH2 branches."
else
echo "There are no differences between the $BRANCH1 and $BRANCH2 branches."
fi
CodePudding user response:
git diff --exit-code
will cause the command to set an exit code similar to the normal diff command if there are differences.
if ! git diff --exit-code <commit1> <commit2> &>/dev/null ; then
echo "Different"
else
echo "Not different"
fi