Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root


1 badge earned


Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!


Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.


Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!


Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

How to find source branch name of a PR in bitbucket pipeline

I am running bitbucket pipeline where I need to run a script from master. I have to fetch few details from the PR source branch. Hence I need the name for source branch on which we create the pull request. Bitbucket provides the default parameter for the destination but not for the source branch. 


Please help if someone knows the way to find source branch name. Thanks

3 answers

I achieved this like:


CURRENT_COMMIT_HASH="$(git rev-parse --short HEAD)"

if ! git show ${CURRENT_COMMIT_HASH} | grep -q "Merged in"; then
    echo "Single commit directly added to branch, versioning only works for branch merge."
    return 0

BITBUCKET_PR_SOURCE_BRANCH=$(git show ${CURRENT_COMMIT_HASH} | grep -oP 'Merged in \K.*(?= \(pull request)')

IFS='/' read -ra BRANCH_PARTS <<< "${BITBUCKET_PR_SOURCE_BRANCH}" # split branch name by '/'
if [ "${#BRANCH_PARTS[@]}" -eq 0 ]; then
  echo "No branch prefix specified, this commit wont be versioned."
  return 0

I achieved like this:


COMMIT_INFO=$(git show -s --format=%s $BITBUCKET_COMMIT)
if [[ $COMMIT_INFO == *"pull request"* ]]; then
    [[ $COMMIT_INFO =~ Merged[[:space:]]in[[:space:]](.+)[[:space:]]\(pull[[:space:]]request ]]
    echo "My source branch is: ${BASH_REMATCH[1]}"
0 votes
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Dec 11, 2020

Hi @Prashantanu Singh Rathore (Temp)  and welcome to the community!

I assume that this is for a Pipelines build that is running when a pull request is created?

If so, you can get the source branch of the PR with the variable BITBUCKET_BRANCH.

This is a default variable, like BITBUCKET_PR_DESTINATION_BRANCH.

Please feel free to let me know if this works for you and if you need anything further.

Kind regards,

Hi Theodora, Thanks for your reply. 


I am merging from 'feature/ABCD' to 'master' branch. 

This merge will initiate the pipeline automatically on master branch. 

I want to know the source branch of PR(feature/ABCD) using any command or variable after the merge. 

So  in my case , $BITBUCKET_BRANCH will give me result as 'master' because I am running master configuration. 

Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
Dec 21, 2020

Hi @Prashantanu Singh Rathore (Temp) ,

Thank you for the reply and info.

This information is available via default variables only for builds that run on pull requests.

If you have a pipeline that runs on master, there is no PR info available for such builds.

You may be able to retrieve this info with Git commands, however please keep in mind that this will essentially be guesswork, as Git itself doesn't store any PR info.

First, you'll need to execute this command:

git fetch origin "+refs/heads/*:refs/remotes/origin/*"

Then, the following command will give you the two parents of the merge commit:

git show --format="%P" $BITBUCKET_COMMIT

Then, you can execute the following command on the second commit hash (which is the parent commit from the source branch) returned by the previous command:

git branch -a --contains commit_hash

All this assumes that you have merged the PR with merge commit as strategy.

The output of the above command will show you all branches that contain that parent commit, including the source branch (if it still exists and hasn't been deleted).

You can also filter out master by

git branch -a --contains commit_hash | grep -v master

Keep in mind though that if there are more than one branches that contain this commit (and not only the source branch of the PR), they will all be returned by this command.

Kind regards,

Hi Atlassian Team,

I'm trying to achieve the same, is there any new update on getting the merged feature branch name in the master branch pipeline.


Like Cameron Gibson likes this

Suggest an answer

Log in or Sign up to answer
AUG Leaders

Atlassian Community Events