Git Merge Branch of Another Remote

Git merge branch of another remote

Just use git pull to pull the remote branch into master:

git remote add something git://host.example.com/path/to/repo.git
git checkout master
git pull something master

Or do it without adding a remote:

git pull git://host.example.com/path/to/repo.git

git pull fetches the remote branch, and merges it into the local branch. If possible, it does a fast-forward merge, which just means that it updates the current master to the latest commit without generating a merge commit. But if there are changes on both sides, it will do a merge, like the ones you see Linus and Junio doing, with the remote URL included.

If you want to guarantee that you get a merge commit, even if it could fast forward, do git pull -no-ff. If you want to make sure that pull never creates a merge commit (so fails if there are changes on both sides), do git pull --ff-only.

If you want to include a more detailed message, like the full log that Linus provides, do git pull --log. If you want to edit the message, instead of just using the automatically created message, use git pull --edit. See documentation for git pull for more options.

Git: Merge a Remote branch locally

You can reference those remote tracking branches ~(listed with git branch -r) with the name of their remote.

You need to fetch the remote branch:

git fetch origin aRemoteBranch

If you want to merge one of those remote branches on your local branch:

git checkout aLocalBranch
git merge origin/aRemoteBranch

Note 1: For a large repo with a long history, you will want to add the --depth=1 option when you use git fetch.

Note 2: These commands also work with other remote repos so you can setup an origin and an upstream if you are working on a fork.

Note 3: user3265569 suggests the following alias in the comments:

From aLocalBranch, run git combine remoteBranch

Alias:

combine = !git fetch origin ${1} && git merge origin/${1}

Opposite scenario: If you want to merge one of your local branch on a remote branch (as opposed to a remote branch to a local one, as shown above), you need to create a new local branch on top of said remote branch first:

git checkout -b myBranch origin/aBranch
git merge anotherLocalBranch

The idea here, is to merge "one of your local branch" (here anotherLocalBranch) to a remote branch (origin/aBranch).

For that, you create first "myBranch" as representing that remote branch: that is the git checkout -b myBranch origin/aBranch part.

And then you can merge anotherLocalBranch to it (to myBranch).

How to merge one remote branch into another remote branch?

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.

Merge a remote branch into another local branch

Simply merge it.

git fetch
git checkout localBranch
git merge remoteBranch

Merge local branch into remote branch other than master?

If branch B is at local, You can merge A to B locally and push B to remote:

git checkout B
git merge A
git push origin B

If you don't have B at local, you can push A to remote and pull request to merge A to B and click merge button on github.

or, fetch B branch to local and merge A to B , then push B to remote, like this:

git checkout master
git fetch origin B:B (fetch B to local)
git checkout B (checkout to branch B)
git merge A (merge A to B)
git push origin B (push merged branch B to remote)

What is the best way to completely replace one remote git branch with another remote git branch?

Consequences

Before doing this you should be aware of the consequences. If anyone else is using the same remote and also has a local copy of stable, them pulling the new branch will lead to a merge of your new stable and their local version of the old stable, resulting in a combination of the changes from both branches (supposedly with huge merge conflicts).

Probably the best solution to this problem is that everyone who is using that remote deletes the stable branch in their local repository as soon as you have replaced the old version and then fetches the updated version of stable from the remote:

git checkout develop
git branch -D stable
git fetch
git checkout stable

This first makes sure something different than stable is checked out as the branch currently checked out cannot be deleted. Then the local stable is deleted, the current changes from the remote are fetched and the new stable is checked out.

Solution

First you should create a local stable branch with the content of the previous develop branch by deleting the old local stable branch and then creating a new one:

git checkout develop
git branch -d stable
git checkout -b stable

Trying to push this branch to the remote will fail as the remote branch contains changes your local branch does not contain. You will receive some message like this one:

Updates were rejected because the tip of your current branch is behind
its remote counterpart. Integrate the remote changes (e.g. 'git pull
...') before pushing again.

There are two ways to work around this. They both lead to the same result, deleting the old stable from the remote and replacing it with your local new version of stable.

git push --force

You can tell git to ignore and discard the remote changes by using the parameter --force.

git push --force stable

Delete the remote branch first

Alternatively you could manually delete the remote branch and afterwards push the new version:

git push :stable
git push origin stable

The first parameter of git push is where to push something and the second parameter is what to push where, divided by a colon. git push origin a:b would push your local branch a to origin using the name b on origin. By leaving out the a part "nothing" will be pushed to stable, leading to deletion of stable on the remote.

Process improvement

It might be a good idea to change your process such that develop and stable stay closer together so you don't need to discard the old stable but can merge your changes from develop to stable.

Maybe you could do regular merges of stable to develop to keep them in sync. To simplify this you could also do fixes and changes required in both versions in stable and then merge them to develop. However how good this might work depends heavily on your processes and how far stable and develop drift apart.

Git merging remote branch to another remote branch - Best practice

The two operations are equivalent. If you merge from the remote though, you don't have to be careful that your local master branch is up to date.

My workflow is typically:

git checkout feature-branch
git fetch
git rebase # if there are upstream commits on the feature branch
git merge origin/master
git push

How to push one commit from one remote branch to another remote branch

You have multiple possibilities:

At first, make sure that you have the branches locally. This can be done by calling git pull origin mainline and git pull origin release-x.

copy the commit

Go to release-x branch(check out a local copy) and copy the commit to that branch using git cherry-pick <commit hash>

Don't forget to execute git push origin release-x after that.

merge it

You can also merge all changes from mainline to release-x by checking out release-x and runing git merge release-x or git merge <commit hash> if you want to merge the difference until a commit in mainline.

Don't forget to execute git push origin release-x after that.

just push it

If you want to copy all changes from mainline to release-x, and release-x is 0 commits behind mainline, you can also push mainline to release-x using git push origin mainline:release-x

overwrite it (STRONGLY DISCOURAGED)

You can also completely overwrite the remote content of release-x with the content of mainline using git push -f origin mainline:release-x. But this will remove all changes made to release that are not committed in mainline. If you want to do that despite this, I suggest you to use --force-with-lease as it does not overwrite the remote if another person pushed to it without you knowing.

Merge two remote branches in a Git repository

If you have remote-tracking branches set up locally, it's as simple as:

git checkout production
git merge development
git push origin production

If you have not yet set up remote-tracking branches, you could do something like:

git fetch origin
git checkout production # or `git checkout -b production origin/production` if you haven't set up production branch locally
git merge origin/development
git push origin production


Related Topics



Leave a reply



Submit