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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to merge a branch with a single commit for staging branch Edited

Hi,

I have three branches dev, test, and master. Now I want to maintain a staging branch before going the changes live. I have so many previous commits in the test branch, now I want to merge the test branch with a staging branch with a single commit (ex: Version 1.0). Then I will merge the staging branch to master. Can anyone help me with this?

Thanks in advance. 

1 answer

Hi @msk134 

First you create a newbranch from your existing test branch (this would be best because if anything happens we can have the original test branch with us)

1. git checkout -b <new-branch-name> 

2. git merge --squash <test-branch-name>

3. git commit #without -m

An editor should be popup with all the commit logs, and files changed from test branch. You could actually delete everything, and write only one line of the commit message you want to show after merging into master.

4. git checkout <staging branch>

5. git merge <new-branch-name>

Hi @Ganesh Babu 

when I do the second step it is showing

"git merge --squash new-branch
(nothing to squash)Already up to date."

Hi @msk134 

From which branch did you create a new branch?

Try creating a new branch from the master branch. (May be i have mentioned as test branch in the last answer sorry for that).

after that follow the step 2 and 3. I think that's enough.

I tried below steps 

1. git checkout test

2. git pull origin test

(I have a bulk of commits in the test branch)

3. git checkout -b staging

4. git merge test

5. git push origin staging

6. git rebase -i --root

I squash all the commits and gave one proper commit message here then I saved the file

Now I checked the log, it is showing updated single commit only

7. git push origin staging -f

after this again when I changed in the "test branch" with a few commits I tried to merge the test branch with staging it showing history-related errors...

Then I tried 

8. git merge --squash test --allow-unrelated-histories

Here I am getting conflict errors. Can you help me if I am doing wrong

Hi @msk134 

See your need is that you have some messy or some commits which you don't need to be mentioned or displayed. So as of now you have three branches where in which now you want to have staging branch as the intermediate between the master branch and the test branch.

So now the staging branch that needs to be created should be the same as your master branch.  So what is the first step you should do is to create a new branch named staging in your case from the master branch.

1. git checkout -b staging (from master branch)

Once the staging branch is created you will now need to merge the changes of testing branch to your staging branch.

2, git merge --squash test

Then commit it using

3. git commit  #don't use -m here.

An editor should be popup with all the commit logs, and files changed from test branch. You could actually delete everything, and write only one line of the commit message you want to show after merging into staging branch.

So by doing this i think it might help you to satisfy your needs.

Or the other way is that you can just create a branch from the latest commit of the test branch and later edit the commit message as per your needs by doing it this way you can have just a single commit which will be the latest commit from the test branch.

Hi @Ganesh Babu 

After the third step, it is showing"On branch staging nothing to commit working tree clean".

Hi @msk134 

Okay did you check with git log? Is it showing all the commits of your test branch or just  a single branch.

Might be i am missing few things. 

As of now i am out where in which i am unable to check and let you know but this is the process actually.

I will attach some links which might help you

https://github.com/rotati/wiki/wiki/Git:-Combine-all-messy-commits-into-one-commit-before-merging-to-Master-branch

https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit

Like msk134 likes this

Thanks for the reply.

I will test this and let you know once I finish it.

Hi @Ganesh Babu 

This is working fine.

Here I have one issue, I did squash merging test branch to staging branch with V1.0 (squashing all commits and pushing one commit with V1.0) is fine now. 

In the test branch, I added new features and pushed a few commits to the test branch. Now I want to squash all the new commits and merge that to staging with a commit message V2.0. 

In this case, I am getting some history related errors. 

I think we need pushes from the test branch to the staging branch and merge from the staging to the master branch. (EOD staging and  master branches to be in sync) Can you please clarify this issue.

Hi @msk134 

But why do you want to merge commits? It will be useful for you to have the history of the changes which might later help you to make changes. Instead of this you can make use of tags which will be good to go. 

If you want your need to achieve then you should make use of git rebase -i commit id, and then in that you should squash the list of commits you want to squash. may be by appending S for squash you can do this. 

In the staging branch, we are planning to maintain only versions with the changelog or new features history. Anyway, we will have those commits history be there in the test branch log right. 

Yes but i think when we do a rebase in the test branch then we will lose the commits

may be for this you need to checkout a branch from the test branch and perform the rebase in the newly created branch and then merge that newly created branch with the staging branch.

If the rebase is directly done on the test branch then we will lose the commits.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Powering DevOps with Bitbucket Server & Data Center

Hi everyone, The Cloud team recently announced 12 new DevOps features that help developers ship better code, faster   ! While we’re all excited about the new improvements to Bitbucket ...

1,892 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you