How could I check if feature_branch
and origin/feature_branch
point to the same commit?
You can compare the output of git-rev-parse
, such as in this bit of shell script:
export BRANCH_A=feature_branch
export BRANCH_B=origin/feature_branch
if [ x"$(git rev-parse $BRANCH_A)" = x"$(git rev-parse $BRANCH_B)" ]
then
echo $BRANCH_A and $BRANCH_B are the same
fi
If you're not interested in using this in a script, but just want some indication of where feature_branch
and origin/feature_branch
are relative to each other, then the top of the output of git status
will tell you their relative positions if they are not the same, e.g.:
$ git status
# On branch foo
# Your branch is behind 'origin/foo' by 187 commits, and can be fast-forwarded.
However, note that this only works if your git config indicates that origin/feature_branch
is "upstream" for feature_branch
. If you've created the local branch from a pre-existing remote-tracking branch, this will typically be the case. If, instead, feature_branch
was a new branch you created locally, you can set up that association in your config by pushing the branch with -u
, e.g. git push -u origin feature_branch
.)
git rev-parse origin/foo_branch
will not get you the latest commit hash on the remote branch. It will give you the latest commit hash that your local git repository knows about on the remote branch.
If you want to compare whether your local foo_branch
is up to date with the current remote, you likely want:
# remote commit hash.
# will do a network request to get latest.
git ls-remote --head --exit-code origin foo_branch | cut -f 1
# local commit hash.
git rev-parse foo_branch
Alternatively, if you want to use git rev-parse
, you should fetch the latest changes first before running rev-parse
:
git fetch origin foo_branch
git rev-parse origin/foo_branch
I like the git ls-remote
approach because it leaves your local repository untouched.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With