20

I would like to know how to merge one remote branch into another remoter branch and have the previous one removed right after the merge applied.

codeforester
  • 28,846
  • 11
  • 78
  • 104
Zorgiev
  • 704
  • 1
  • 6
  • 19

3 Answers3

23

From what I understand, you have one remote foo, containing branch_1 and branch_2. First, we can't do merge operation remotly. We have to track the remote repository, do the operation we want locally (such as merging branches) and then push our new snapshot to the server.

Ie:

  • git clone [repo_adress]

You are on the master branch.

  • You can then checkout or create other branches and do your work in it.

Now suppose we have the two branches branch_1 and branch_2. You want to merge branch_1 into branch_2 and then delete branch_1.

You checkout to branch_2 and then merge branch_1 with it:

$ git checkout branch_2
$ git merge branch_1

From there either the merge is smooth or you've got conflict. Once the merge is done, you can delete the merged branch i.e branch_1 by doing:

$ git branch -d branch_1

And then push your work:

$ git push

In case branch_2 doesn't exist on the remote, you've got to create it:

$ git push -u foo branch_2

Note that deleting branch_1 locally doesn't delete it remotely (considering that it exists on the remote). To do so, we are going to say to git: "push nothing to the branch i want to delete" ie:

$ git push remote_name :branch_name

To read like git remote push remote_name "nothing":branch_name.

Now is there any mean to do it automatically?

I don't know (although I would investigate post merge "git hook"), but I'm not sure we ought to wish it. Deleting branches on remote is somewhat hazardous. Doing so manually is a good way to be sure of what we are doing.

kaan
  • 3,045
  • 2
  • 11
  • 31
chaiyachaiya
  • 2,257
  • 15
  • 18
  • Then how does the 'Merge' functionality on Bitbucket work? The reviewer reviews the PR changes and after approving he just clicks on 'Merge' and the branches is merged remotely. If I refresh the local SourceTree, I see incoming changes from the remote. – Tejas Chandrashekhar Sep 10 '19 at 13:51
  • Yes. You're right. At the time I answered I was focused on what 'possibly' happens under the hood on the git repository side and proposed it as a 'possible' answer A more practical answer to the question is like you suggest. You create a MR/PR on the repo side and check the "delete branch after merge" box. Thx for raising the point! @TejasChandrashekhar – chaiyachaiya Sep 11 '19 at 20:41
2

You can switch to the tracking branch(a local branch which represents your remote branch) in which you want to merge another branch by using the following command

git checkout origin/name_of_your_branch

After that merge the another remote branch

git merge origin/brach_name_you_wanted_to_merge

After that, if any conflicts occur, solve it. and after that just commit and push.

and now checkout to your local branch. by the following command

git checkout name_of_your_brnach

After that pull the origin by using git pull command. that's it.

Riddhi
  • 2,522
  • 5
  • 31
  • 52
  • 1
    Disambiguation for the ones who might not know: .. you mentioned "you can switch to the remote branch" which is inaccurate: that isn't a remote branch/it's still a local branch that is tracking a remote branch and needs network operators to be updated, such as pull, fetch or clone; .. all branches you have are **local** (even **tracking** branches that track the remote branches are still local) - so any checkouts you do switch between local branches or tracking branches - see here https://stackoverflow.com/questions/4693588/git-what-is-a-tracking-branch#answer-49755270 – Johnny Dec 04 '18 at 15:43
0

You can pull them to your local. Do a merge and push it. To delete the redundant branch thereafter, just run

git push origin :<branch_to_delete>

For example, if you want to delete a branch A

git push origin :A
Akash
  • 4,615
  • 2
  • 26
  • 39