0kxphg
Last Updated: October 17, 2016
·
4.753K
· rabovik

Merging Branches Without Checkout

(only for fast-forward merges)

Summary

$ git fetch . develop:master

This fast-forwards master to develop without checkout.

Explanation

Let's say we are working in a develop branch which is ahead of master by several commits. So our history looks something like this:
Needs fast-forward

Now we want to merge develop to master to receive this:
After fast-forward

The usual way of doing this would be:

$ git checkout master
$ git merge develop

But it causes git to change working directory two times, although the final result is identical to the one before checking out master.
In some cases this may be unacceptable, e.g. we have a large repo so checkouts are time-consuming, or our compiler relies on timestamps and may think that a lot of files are dirty and need a rebuild. So we need another way.

Suppose we have a clone of our repo. We can fetch develop ref from clone to master. According to docs fetch syntax is

$ git fetch <repository> <src>:<dst>
The remote ref that matches <src> is fetched, and if <dst> is not empty string, the local ref that matches it is fast-forwarded using <src>.

So if we run $git fetch clonerepo develop:master then master will be fast-forwarded to develop.

But <repository> can be an URL, including local URL, e.g. '/path/to/repo/'. And as '.' is the same as '/path/to/current/repo/' we can use our repo itself instead of a clone:

$ git fetch . develop:master

4 Responses
Add your response

5218

Excellent.

over 1 year ago ·
5222

Why would you fast forward, though?

over 1 year ago ·
5224

@skopp Depending on chosen branching model there may be different cases when we need fast-forward. In the article I show a very simplified one, just for example.

over 1 year ago ·
14602

How can I "+1" or "Like" or "Upvote" this post?! :)

Thanks.

EDIT: Oh, I found the "Upvote" button! :D

over 1 year ago ·
Filed Under

Awesome Job

Frontend Web Developer for Games
·
Leamington Spa (United Kingdom)
·
Full Time