# Merging a release branch into master

**URL:** <https://community.jenkins.io/t/merging-a-release-branch-into-master/15105>\
**Category:** Using Jenkins\
**Created:** [May 14, 2024, 2:08pm UTC](https://community.jenkins.io/t/merging-a-release-branch-into-master/15105 "2024-05-14T14:08:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![p4pratik](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p4pratik](https://community.jenkins.io/u/p4pratik)\
**Post date:** [May 14, 2024, 2:08pm UTC](https://community.jenkins.io/t/merging-a-release-branch-into-master/15105/1 "2024-05-14T14:08:10Z")

</div>

Hello Team,  
I am trying to merge a release branch with the “main” branch via jenkins pipeline.

**Some points to note:**

- main branch does not have a JenkinsFile. However, the release branch has. and thats what needs to be merged

In such a case how can I merge the release branch into main branch using Pipeline SCM. I am using SCM to give the user the flexibility to select the Source branch and Destination branch. (Can this be achieved without SCM?, if yes please provide a way out.)

I tried to checkout the release (source) branch and merged the main into it, but it got detached and found nothing to update, since the release branch was created from the main branch.

Alternatively, I tried to vice versa (checking out the main branch and then merging the release branch). But the jenkins got failed for the obvious reason that it did not find the “JenkinsFile” using which the Jenkins will perform the execution.

In such a scenario, I cant find a way around and hence requesting your assistance…

Regards  
Pratik

I attach my JenkinsFile for your reference,.

[JenkinsFile.zip](https://community.jenkins.io/uploads/short-url/oYAalTmwsJe3xsGVTlyu41Lk1vO.zip) (560 Bytes)

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [May 14, 2024, 9:05pm UTC](https://community.jenkins.io/t/merging-a-release-branch-into-master/15105/2 "2024-05-14T21:05:46Z")

</div>

The `checkout` configuration that you’ve used is missing some crucial items if you want to use that repository to merge changes from other branches into the working branch of that job, especially if you intend to push the resulting merge to another repository (like GitHub, Bitbucket, GitLab, or Gitea).

The `checkout` command by default uses a “detached head” checkout because it assumes that a working branch configuration is not used by most Jenkins jobs. You can see that if you run the command `git status` in the Jenkins workspace immediately after checkout. It will report something like:

`HEAD detached at c06af323`

Most merge operations need a destination branch, so the `checkout` needs to add [“checkout to specific local branch”](https://plugins.jenkins.io/git/#plugin-content-checkout-to-specific-local-branch) with the name of the branch that will be the destination of the merge. Since you are merging from `main` to `release`, the branch name for local checkout will be `release`.

When pushing the changes back to the central repository, you’ll need to use either the Pipeline step [`withCredentials`](https://plugins.jenkins.io/git/#plugin-content-git-credentials-binding) (if the repository URL uses https) or the [`sshAgent` step](https://www.jenkins.io/doc/pipeline/steps/ssh-agent) (if the repository URL uses ssh).

Users that run a freestyle job, will use the [Git publisher](https://plugins.jenkins.io/git/#plugin-content-git-publisher). Pipeline is better. Use Pipeline.

---

<div class="post-metadata">

**Author:** ![p4pratik](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p4pratik](https://community.jenkins.io/u/p4pratik)\
**Post date:** [May 15, 2024, 5:47am UTC](https://community.jenkins.io/t/merging-a-release-branch-into-master/15105/3 "2024-05-15T05:47:57Z")

</div>

Hello Mark,  
Thank you very much for your explanation.

However I have not quite understood the same. I would like to either do with Pipeline script or Pipeline with SCM (not with Git publisher)

Correction: I want to merge release to controller (and not controller to release as you replied in the previous thread).

Secondly I am using ssh, so basis your explanation withCredentials will work on Windows?. **Kindly confirm**

Hence can you help me what changes I can do to make it work? A detailed ordered list step would be enough for me to do a kick start

Regards  
Pratik

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [May 15, 2024, 11:15am UTC](https://community.jenkins.io/t/merging-a-release-branch-into-master/15105/4 "2024-05-15T11:15:10Z")

</div>

> [@p4pratik](#):
>
> Correction: I want to merge release to controller (and not controller to release as you replied in the previous thread).

Let’s call the branch that has the extra content the “source branch”. Let’s call the branch that should receive the extra content the “destination branch”. The extra content from the source branch can be brought into the destination branch by performing a checkout of the destination branch, then performing a `git merge source-branch`.

Thus, if the source branch is `main` and the destination branch is `release`, the steps would be:

```bash
git checkout -b release -t origin/release
git merge origin/main
git push origin release

```

If the source branch is `release` and the destination branch is `main`, then the commands would be:

```bash
git checkout -b main -t origin/main
git merge origin/release
git push origin main

```

In order for the git plugin to best approximate the first command, `git checkout -b destination-branch -t origin/destination-branch`, you need to add the “checkout to specific local branch” option to the `checkout` step in Pipeline. If you don’t add that option, git will complain that it is using a detached head.

> [@p4pratik](#):
>
> Secondly I am using ssh, so basis your explanation withCredentials will work on Windows?. **Kindly confirm**

Since the repository URL uses ssh, you’ll need to use the `sshAgent` step rather than the `withCredentials` step. I’m not aware of any limitation for that step on Windows.

---

<div class="post-metadata">

**Author:** ![p4pratik](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p4pratik](https://community.jenkins.io/u/p4pratik)\
**Post date:** [May 15, 2024, 1:38pm UTC](https://community.jenkins.io/t/merging-a-release-branch-into-master/15105/5 "2024-05-15T13:38:43Z")

</div>

Hello Mark,  
Thank you very much for the detailed explanation.

Not sure but I am not able to use sshagent . (It says **could not find a suitable ssh-agent provider** )

Secondly when I use withCredentials over ssh, I get “remote rejected”.  
Everything works fine (incl merge), except **git push** where I get the “remote rejected” error.

My “main” branch has a restriction to commit (to an email address - [devops@xx.com](mailto:devops@xx.com)) and the ssh key is created for the same email address.

I attach the jenkins file. Line # 42-48 gives the error :

C:\Users\Devops\AppData\Local\Jenkins.jenkins\workspace\Framework\_MergeAndPushTag\> **git push origin testmaster**

**remote: Permission denied to update branch testmaster.**

To **[bitbucket.org](http://bitbucket.org):username/repo.git**

**! [remote rejected] testmaster → testmaster (pre-receive hook declined)**

**error: failed to push some refs to ‘[bitbucket.org](http://bitbucket.org):username/repo.git’**

The “testmaster” is the destination branch.

Please find attach the Jenkins File.  
[Jenkinsfile.zip](https://community.jenkins.io/uploads/short-url/auZDnSdt65oRdYTFghH6QTvMpjT.zip) (809 Bytes)

I am very sorry to bother you on this.

Regards  
Pratik

---

<div class="post-metadata">

**Author:** ![MarkEWaite](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/markewaite/32/20_2.png) [@MarkEWaite](https://community.jenkins.io/u/MarkEWaite)\
**Post date:** [May 16, 2024, 5:52pm UTC](https://community.jenkins.io/t/merging-a-release-branch-into-master/15105/6 "2024-05-16T17:52:26Z")

</div>

> [@p4pratik](#):
>
> **remote: Permission denied to update branch testmaster.**
> 
> To **[bitbucket.org](http://bitbucket.org):username/repo.git**
> 
> **! [remote rejected] testmaster → testmaster (pre-receive hook declined)**
> 
> **error: failed to push some refs to ‘[bitbucket.org](http://bitbucket.org):username/repo.git’**
> 
> The “testmaster” is the destination branch

The Bitbucket administrators have blocked you from pushing to that branch. You’ll need to work with them to resolve that issue.
