How to locally fetch and checkout a pull request

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

A user may want to locally fetch a Pull Request from Bitbucket Cloud.  This allows for testing and viewing diffs before deciding to merge.  This is a way to check out the branches and do some diffs to confirm the pull request diffs.  This article will also show some of the useful diff types that can be run locally terminal using the git command.

Environment

Repository: KB Example Repo / kb-example-repo 

Forked Repository: Forked KB Example Repo / forked-kb-example-repo 

This article assumes that you have full access to the repository.  Most of this article only requires read permissions.  But to merge you do need write permissions.  If you merge locally, you may not have permission to push the change up to Bitbucket if you do not have write access to the repository. If you do not have permission for a task, please see your repository admin.

Solution

Test the merge

This is the process to test the merge on a clone before the pull request does the merge.

  1. Make sure you have fetched both branches in the pull request in your cloned repository.  Then checkout branch the target branch, the branch the changes from the source branch will be merged into.  This example matches the screen shot below, feature/one.

    git branch --track feature/one origin/feature/one.      # target branch
    git branch --track bugfix/CS-1000 origin/bugfix/CS-1000 # source branch
    git fetch --all
    git switch feature/one
  2. Use the merge command with the -no-commit --no-ff options. 

    git merge --no-commit --no-ff bugfix/CS-1000
  3. This command will not make any changes.  It will print out any conflict data.  Here is the output if the branches have already been merged: 

    $ git merge --no-commit --no-ff bugfix/CS-1000
    Already up to date.
    $

Test the diff

  1. Get the URL of the Merge request.  In this example it is https://bitbucket.org/atlassian/kb-example-repo/pull-requests/1.  The second screenshot is an example of a pull request from a forked repository with the url: https://bitbucket.org/atlassian/kb-example-repo/pull-requests/2


    • The source and destination branch names can be copied from the UI as shown in the above screenshot. Alternatively, you can also use the below API query to get the branch names in a pull request. 

      curl -u <username>:<apppassword>  "https://api.bitbucket.org/2.0/repositories/<WorkspaceID>/<repoSlug>/pullrequests/<ID>?fields=destination.branch.name,source.branch.name" 


      Kindly refer to API authentication methods for various types of authentication methods that can used with the API calls. 

  2. Make sure you have fetched both branches in the pull request in your cloned repository.  If the pull request source branch is for a fork, you will need to clone the fork repository to perform these actions. Clicking on the branch name in the pull request will allow you to copy the branch name into your clipboard.

    git clone git@bitbucket:atlassian/kb-example-repo.git # OR git clone git@bitbucket.org:dlaser/forked-kb-example-repo.git
    cd kb-example-repo.git
    git branch --track <localBranchName> <RemoteBranchName>
    git fetch --all

    Example:

    kb-example-repo$ git branch --track feature/one origin/feature/one
    branch 'feature/one' set up to track 'origin/feature/one'.
    kb-example-repo$ git branch --track bugfix/CS-1000 origin/bugfix/CS-1000
    branch 'bugfix/CS-1000' set up to track 'origin/bugfix/CS-1000'.
    kb-example-repo$ git fetch --all

    Git documentation: git-branch, git-fetch, git-clone

  3. This is the command to view the diff as it is in the pull request.  This diff shows all of the changes between bugfix/CS-1000 and the common parent of the two branches. 


    git diff feature/one...bugfix/CS-1000
  4. Here are some other useful commands.
    1. Find all changes between the two branches. Git documentation: git-diff

      git diff feature/one..bugfix/CS-1000 #OR
      git diff feature/one bugfix/CS-1000
    2. Find the hash to the common parent.  Git documentation: git-merge-base

      kb-example-repo$ git merge-base feature/one bugfix/CS-1000
      1bf4fd3d4e58285938e0fce801b2b216a5c0d7f4
      kb-example-repo$
    3. Branches that contain a commit. Git documentation: git-branch

      kb-example-repo$ git branch --contains 1bf4fd3d4e58285938e0fce801b2b216a5c0d7f4
        bugfix/CS-1000
        feature/one
      * master
      kb-example-repo$
  5. To merge, we recommend using the merge button in the pull request.   You can also merge locally and push the merge up to Bitbucket using these commands.  Once pushed, the pull request will show as merged.  

    kb-example-repo$ git switch feature/one # This command makes sure you have the right starting branch checked out.
    kb-example-repo$ git merge bugfix/CS-1000 
    Updating 1bf4fd3..773677e
    Fast-forward
     logp.py | 4 ++--
     1 file changed, 2 insertions(+), 2 deletions(-)
    kb-example-repo$ git push

    Git documentation: git-switch, git-merge, git-push


Last modified on Dec 4, 2023

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.