0
git clone git@github.com:foo/bar.git bar-master

git branch release
git push origin release

git clone -b release git@github.com:foo/bar.git bar-release

cd bar-master
touch foo.txt
git commit -m "foo.txt"

cd bar-release

How do I merge changes from master into bar-release and push back the changes to the remote origin/release branch. What would be the correct way to merge changes and push changes back to the remote branch.

Sam
  • 7,451
  • 19
  • 54
  • 92

2 Answers2

5

I would really encourage you to get used to working inside the one directory. Git is on-purpose designed for this.

If the reason you're doing this is because you can't immediately tell what branch you're on by using something other than the directory name, then I would encourage looking into things that set your prompt to include the branch name. I have something like this in my personal dot files (bash prompt setting here and __git_ps_1 command here.

This has saved me so many times it's worth its weight in gold.


Now that we've got the multiple directories problem solved, you should read this great post called "A successful Git branching model" that covers using several different branches for development, albeit on a single repository.

Once you're done there, now you're ready to apply this same thing to other repositories. First, you're going to need to add the remote repository to the list of remotes tracked by git. This is just helpful as you can reference by name rather than address, with the name generally being much shorter than the address.

git remote add main git@github.com:organization/main.git

I am going to assume here that the remote is what has this release branch on it. The first thing you're going to want to do here is update the information about this repository, which you can do with this command:

git fetch main

If you're working with multiple remotes (which would indicate some level of insanity on your part), you can fetch them all with git fetch --all.

Anyway, once you've done that initial fetch, you can then checkout main's release branch into your repository:

git checkout main/release -b release

This tells git, "Hey, get me main/release's branch, and call it release in this repository (please)".

Now, assuming that you've been a good developer, you should be able to directly merge your commits across into this branch from your master branch. Right now, you'll be in the release branch that is based off main/release so you'll just need to do this:

git merge master

Some people here would ask you to rebase instead (There's an interesting discussion about this here. That has the same syntax as merge: git rebase release.

Now you need to really really make sure that everything's working as it should. That's up to you.

Once you're super-confident about that, you can push your release branch to the main repository by running this command:

git push main release

Now go forth and Git :)

Community
  • 1
  • 1
Ryan Bigg
  • 102,687
  • 22
  • 224
  • 252
1

You should be using the same repository with 2 different remotes. You can continue to do this but now you will have to manage pushing and pulling between the 2 local ones as well as the remote.

For you question though, you would push and fetch between the 2 local ones to arrange what you need and then push from either one to the github remote.

Adam Dymitruk
  • 109,813
  • 21
  • 138
  • 137
  • am using the same repository here, or just let me know how I should manage merging / pushing between two remotes. – Sam Jan 20 '12 at 17:51
  • Managing between two remotes is easy. What you want to do is `git remote add other git://url/to/other`. Now you will have 2 remotes that you can push or fetch to/from in one repo. Just indicate to fetch or push which remote you want to act upon, followed by a branch - if you omit the branch it will be all tracked branches by default. `git push other some-branch` – Adam Dymitruk Jan 20 '12 at 18:06
  • I am sorry, I use a single repo URL (with multiple remotes - origin/master, origin/release). Our code moves from master-->release. I am getting confused with the local / remote branches, so I created 2 different directories to have the source code corresponding to them. Am I fetching the remote branch in a incorrect way, kindly provide the entire sequence of checkout/merge/push. The point of confusion for me is when I merge master codebase into release, the HEAD of release points to master and I am not sure if this is the correct behavior. – Sam Jan 24 '12 at 02:38
  • You mean multible remote branches. Multiple remotes means an entirely different thing. – Adam Dymitruk Jan 24 '12 at 05:00
  • git branch -a shows me the following remotes/origin/master remotes/origin/production. I guess its multiple remote branches of the same repository. – Sam Jan 24 '12 at 09:19