Mike Lischke
Mike Lischke

Reputation: 53552

How to pull a git branch without checking it out?

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?

Upvotes: 5

Views: 5296

Answers (3)

Mike Lischke
Mike Lischke

Reputation: 53552

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.

Upvotes: -1

Gauthier
Gauthier

Reputation: 42035

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>

Upvotes: 1

Michael Mairegger
Michael Mairegger

Reputation: 7301

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

Upvotes: 6

Related Questions