0

Recently, we have migrated code from Bitbucket to GitHub repository. I would like to know if there is any git command to compare the branches between Bitbucket and GitHub. This comparison is to check / verify if all the code has been migrated properly.

One option I have tried is that, to checkout code from both the branches (from Bitbucket and GitHub) and compare using the folders using beyond compare or some comparison tools. But would like to know if there is any git command, that we can use to compare the branches efficiently.

2 Answers 2

6

You just need to fetch the changes from both remote repositories, and then perform a git diff between the two tracking branches.

Say origin_bit_bucket and origin_git_hub represent respectively the remote repositories on BitBucket and GitHub, you could run:

git fetch origin_bit_bucket my_branch
git fetch origin_git_hub my_branch

git diff origin_bit_bucket/my_branch origin_git_hub/my_branch

In the explanation above, I assumed that your remotes had already been added to your local repository since you've stated: "Recently, we have migrated code from Bitbucket to GitHub repository." So, because you're in this in-between phase, I figured that the two remotes were already present in the repo.

However, in case they're not, you can add multiple remote repositories with the git remote command, specifically with the add subcommand, and name them with appropriate handles.

git remote add origin_bit_bucket <Bitbucket_repo_URL>
git remote add origin_git_hub <GitHub_repo_URL>
Sign up to request clarification or add additional context in comments.

5 Comments

@dano-vta Thanks for the response. I have some queries here. Since both the remote repositories, have the same name, Im unable to checkout the repos in a single location. Also I tried to perform git diff by specifying the absolute path of the origin_bit_bucket/my_branch. But Im getting a message fatal: Invalid path. Any help on this is much appreciated.
The two remote repositories have the same name because they represent the same project (one on GitHub and one on Bitbucket), but the name that you give to the remote reference in your local repo must be different. If you already have a handle called origin for the repository on Bitbucket, you cannot use the same name also for the remote on GitHub. In fact, if you try to add GitHub's remote repo as orogin as well with git remote add origin <GitHub_url>, you would receive the following error message error: remote origin already exists., because origin is already used for Bitbucket.
@vr3w3c9 if you add GitHub's remote repo with a different name, for example git remote add origin_git_hub <GitHub_url>, you could have two references pointing to their corresponding remote (origin and origin_git_hub) within the same local repo. Therefore, you could fetch from both remotes like so: git fetch origin my_branch, git fetch origin_gut_hub my_branch, and compare their branches with git diff origin/my_branch origin_git_hub/my_branch
dani-vta Thanks a ton for the detailed response . The above worked perfectly and I was able to fetch the differences. I have one more query. Post fetching the difference , how do I merge the difference from bitbucket to github. Is there any command I can use to merge the changes ?
@vr3w3c9 If the differences between the two branches can be solved with a fast-forward, i.e. Bitbucket's my_branch contains the same set of commits of GitHub's my_branch plus some others, then you could simply push to GitHub with a custom refspec, git push origin_git_hub refs/remotes/origin_bit_bucket/my_branch:my_branch. The previous command basically pushes to the remote on GitHub (git push origin_git_hub) by supplying the tracking branch for Bitbucket as the source (refs/remotes/origin_bit_bucket/my_branch), and the branch my_branch on GitHub as the destination.
2

You can have multiple remote repositories attached to you local repository. Commonly, the main remote repo is called origin, but you can add second remote with git remote add <name> <url>, lets say git remote add bitbucket https://bitbucket.... Then you can git fetch all remote branches and reference them as git diff origin/branch-name bitbucket/branch-name in diff command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.