How to checkout a git branch without checking it out?

I have 2 branches in the repository (release + dev) that I work on regularly. However, when switching between branches, it triggers a long build in my project (I'm talking about ~ 30 minutes), so I would rather not switch between them. To work in both, I created 2 copies of my repository and this works great.

However, when I want to merge the changes, I need to activate the release branch momentarily to pull the changes. Then I can go back to development, merge and commit. However, as I wrote above, this switch will lead to a lengthy rebuild. Is there any other way to pull the branch without making it active?

+3


source to share


3 answers


If you want a pull

branch in a remote repository, to your current branch, you don't need to switch.

If I understood correctly, you have one repo with release

and the other with dev

checked out where you are working. When you finish something in dev

, you want to combine it in release

, without checking release

.

I assume you have remotes setup in both repos so that both can reference each other.

cd

to the repository release

. Pull the changes from the repo dev

:

git fetch <dev_remote> <branch>

      

You now have an updated branch dev

in your repository release

, even though you didn't need to check it out. Note, however, that the name of the updated branch dev

contains: the name of the remote, a forward slash, and then the name of the branch. This is a remote branch and is displayed in two colors in gitk

and as remotes/<remote_name>/<branch>

with git branch -a

.

Merge the updated branch into the local branch release

:

git merge <dev_remote>/<branch>

      



And you have it. No checkout

, but your branch release

in your repository has release

merged the content dev

.

This is the principle. Now you can do it in one step:

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 checked out and merged with your current checked out branch in the registry release

.


Likewise, you need to update the repository dev

with a new merge compilation to release

:

cd <dev_repo> # you are on the dev branch
git pull <release_remote> <branch_name_to_pull>

      

+1


source


You can fetch

remote changes. This will download all changes from the remote repository, but it will do nothing. But if you want to merge

branch A

into a branch B

, you can't do that without checking out B

earlier.



git pull

is at most a git fetch

combined withgit merge

+4


source


Probably the simplest approach is to have its own folder for each branch you want to work on. Then you can just merge from one folder to another. This actually disables git's basic idea of ​​switching branches in place (which I think is pointless, at least for projects with non-trivial size and many differences between branches).

0


source







All Articles