5

I have 2 branches in a repository (release + dev) that I work regularly on. However, when switching between branches it triggers a long build in my project (I'm talking about ~30mins) so I'd rather avoid to switch between them. In order to work in both I created 2 copies of my repository and that works fine.

However, when I want to merge the changes I need to make the release branch active for a moment to pull changes. Then I can switch back to develop, do the merge and commit. However, as I wrote above this switch will cause a long rebuild. Is there another way to pull a branch without first making it active?

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • Why should checking out a new branch trigger a build? I can see wanting to do a build if you actually commit to a branch, but a checkout doesn't modify the repository at all, just your working directory. – chepner Apr 27 '15 at 14:16
  • Checking out doesn't itself trigger a build, but next time I do a build too many files are found as touched and hence many unncessary files are recompiled. – Mike Lischke Apr 27 '15 at 14:52
  • 1
    Can't you just check out `master` again before building? This isn't really a problem with `git`, but with the apparently large difference between the code in the two branches. – chepner Apr 27 '15 at 14:56
  • 1
    Checking out yet another branch doesn't "untouch" files. When switching from dev to release for merge and then back to dev will touch many files. You cannot undo that. IMO this is one of the weakest point in git (to switch branches in-place). – Mike Lischke Apr 27 '15 at 15:17

3 Answers3

6

You can fetch remote changes. This will download all changes from the remote repository but will do nothing more. But if you want to merge branch A into branch B you cannot do that without checking out B before.

git pull is nothing more than a git fetch combined with a git merge

Michael Mairegger
  • 6,833
  • 28
  • 41
  • You may very well merge a branch without checking it out. You check out the branch to merge **to**, then `git merge `. Did I misunderstand you? – Gauthier Apr 27 '15 at 12:28
  • If you want to merge A into B, then I have to check out B to be able to merge changes from A. The only possibility to merge A into B is if A is a fast-forward merge. – Michael Mairegger Apr 27 '15 at 12:33
  • That's fine I think. In one folder I have dev checked out, in the other release. So I can work on release, commit + push. In the other folder I pull release (without checking it out) and merge to dev (which is checked out). And the job is done. Is that what you are saying should work with fetch? – Mike Lischke Apr 27 '15 at 12:44
  • I your folder where you have the `release` branch, you will execute `git fetch` and `git merge develop`. There you have not to check out the develop branch. But you have to push your changes from the folder where the `delevop` is checked out – Michael Mairegger Apr 27 '15 at 12:55
  • @xxMUROxx: "If you want to merge A into B, then I have to check out B to be able to merge changes from A." Exactly. What you wrote in the answer ("But you are not able to merge a branch without checking it out before.") implies that you'd have to checkout A before merging it. – Gauthier Apr 27 '15 at 13:25
  • @Gauthier i have meant "You do not have to check out branch A if you want it to merge into B". You were right, it is a bit confusing. I have changed the answer to be a more appropriate. – Michael Mairegger Apr 27 '15 at 13:35
  • Note: my question was not about merging. I'm aware that the target branch needs to be checked out for a merge. I rather asked how to update a branch that is not checked out (which then could be easily merged to the checked-out branch). – Mike Lischke Apr 27 '15 at 13:48
  • And I tried fetch: it doesn't update my release branch (if not checked out). So it's of no help at all. – Mike Lischke Apr 27 '15 at 13:55
  • @MikeLischke `fetch` will never update any branch. It will only download the newest commits. You can see http://stackoverflow.com/a/12343727/2964291 more information – Michael Mairegger Apr 27 '15 at 14:02
  • So, why did you propose it as answer then if it clearly doesn't answer my question ;-) (not even providing a workaround)? – Mike Lischke Apr 27 '15 at 14:14
1

If you want to pull a branch in a remote repository, into your current branch, you do not need to switch.

If I understand correctly, you have one repo with release checked out, and another with dev checked out, in which you are working. When you're done with something in dev, you want to merge it in release, without a checkout of release.

I suppose that you have remotes setup in both repos, so that both can refer to each other.

cd to your release repo. Fetch the changes from the dev repo:

git fetch <dev_remote> <branch>

You now have an updated dev branch in your release repo, although you did not need to check it out. Note however that the name of the updated dev branch contains: the name of the remote, a slash, then the name of the branch. It's a remote branch, it shows in two colors in gitk, and as remotes/<remote_name>/<branch> with git branch -a.

Merge the updated branch to your local release branch:

git merge <dev_remote>/<branch>

And there you have it. No checkout, but your release branch in your release repo has merged the content of dev.

This is for the principle. Now you can do this in one step instead:

cd <release_repo> # you are on the release branch
git pull <dev_remote> <branch_name_in_dev_remote>

Your branch in dev_remote will be fetched, and merged into your currently checked out branch in the release repo.


Likewise, you'll want to update your dev repo with the new merge commit in release:

cd <dev_repo> # you are on the dev branch
git pull <release_remote> <branch_name_to_pull>
Gauthier
  • 40,309
  • 11
  • 63
  • 97
  • I see. Your base idea is to pull the remote release branch into the local dev branch which is not exactly what I was asking (because the local non-checked-out release branch is not updated by this), but at least it avoids to have to switch branches. Thanks. – Mike Lischke Apr 27 '15 at 14:50
  • Yes, that is why you need to pull the remote branch in both repos. If you want to update the local `release` branch in the `dev` repo, add a target local branch to `fetch`: `git fetch :release`. This will update your local ref. But if you don't want to check it out anyway, I don't know what you'll do with it. – Gauthier Apr 27 '15 at 15:07
-1

I ended up with another copy of my repository just for merges. Since no build is going on in this folder I can switch between branches without trouble. Once all merges are done and pushed I can then pull them in the individual work folders.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181