0kxphg
Last Updated: October 17, 2016
·
3.183K
· rabovik
7eeed4cd79368adc2148b3519f11b6b9

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
Say Thanks
Respond

4 Responses
Add your response

5218
598c944ccfe53b6874fe15ddf0d9d803

Excellent.

over 1 year ago ·
5222
74c714225754481aabee8ab22408cd2d

Why would you fast forward, though?

over 1 year ago ·
5224
7eeed4cd79368adc2148b3519f11b6b9

@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
0e8ecffe344b8260723d8488754652ed

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