gitbitbucket

Old master branch out of sync


I have a master branch for a repo in Bitbucket, which was last updated back in 2017. Now I have a task to sync the latest release branch with master and need to bring all code from release into master.

I tried to create a pull request, but there were more than 1000+ files added/changed.

What would be the best way here to bring the code of release branch into master here and files are missed while merging

Please note, I need to keep master branch history.


Solution

  • I can ignore the changes from master, what i need is all code from release to but put in master in cleanest possible way.

    The simplest and cleanest way to align the master branch to release is with a git reset --hard <commit>. The command basically resets the index and the working tree to the given commit, in your case release's HEAD commit, making the master branch point to the same commit referred by release.

    Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted.

    # checking out the master branch
    git checkout master
    
    # Resetting the master branch to the HEAD commit of the release branch
    git reset --hard release
    
    # forcing the overriding of the master branch on the remote with your local version
    git push --force origin master
    

    If you cannot push directly to the master branch due to some branch protection rules on your remote, you can follow this answer.


    Third approach from the comments

    A third approach, where you need to keep master's history and the commits made on it after release branched off, could be via a simple merge.

    In this scenario, since you would have commits over 7 years old, you might first want to check what changes have been introduced by the commits on master that are not present on release. To do that, just log every commit on master minus the ones on release.

    git log --no-merges master ^release
    

    Once you have a clearer picture of your current situation, you might want to rebase the master branch to get rid of some of the commits made after release's divergence. In that case, just supply the merge-base between master and release to the git rebase -i command. At this point, you can finally merge release into master.

    # selecting the master branch
    git checkout master
    
    # rebasing the commits made after release diverged.
    # this step can be skipped if no commits need to be droped.
    git rebase -i $(git merge-base master release)
    
    # merging release into master
    git merge release