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

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

msk134 January 27, 2020

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

0 votes
Ganesh Babu
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2020

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>

msk134 January 28, 2020

Hi @Ganesh Babu 

when I do the second step it is showing

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

Ganesh Babu
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2020

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.

msk134 January 28, 2020

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

Ganesh Babu
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2020

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.

msk134 January 28, 2020

Hi @Ganesh Babu 

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

Ganesh Babu
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 28, 2020

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
msk134 January 28, 2020

Thanks for the reply.

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

msk134 January 28, 2020

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.

Ganesh Babu
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2020

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. 

msk134 January 29, 2020

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. 

Ganesh Babu
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 29, 2020

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
AUG Leaders

Atlassian Community Events