git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: request for documentation about branch surgery

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 7, 2009, 02:50 UTC
Message-ID
<7vab3hb40x.fsf@alter.siamese.dyndns.org>
In-Reply-To
<200907070105.12821.bruno@clisp.org>
Bruno Haible <bruno@clisp.org> writes:
> 1) After the section "Rewriting a single commit", it may be useful to
> have a section "Inserting one or more new commits". This is something that
> cannot be done with the "detached head" technique.
You learn new things every day, and today is such a day ;-)
Show 9 quoted lines
>   If you want to add a commit in the middle of a branch:
>
>             A---C---...---Z    master
>
>   =>
>
>             A---B---C---...---Z    master
>
>   it is achieved by
        $ git checkout master~25 ;# detach HEAD at A
        $ edit edit edit
        $ git commit ;# creates B
which makes
               B              HEAD (detached)
              /
             A---C---...---Z    master
and then
        $ git rebase HEAD master
which reshapes the history into
               B---C'--...---Z' master
              /
             A---C---...---Z    master@{1}
and you are done.
Show 6 quoted lines
> 3) When do I need "git merge", and when do I need "git rebase", in the
>    context of branch surgery?
>
>    The simple answer, that I would find worth mentioning, is:
>      - "git merge" copies commits from one branch to another.
>      - "git rebase" only moves commits around to make history more linear.

If you think "git merge" _copies_, you will never understand what "merge" does.

If you have master and origin branch, merging them together
                X---Y---Z  origin
               /
              /
             O----A----B master
        $ git checkout master
        $ git merge origin
will give you this:
                X---Y---Z  origin
               /         \
              /           \
             O----A----B---M master
                       ^
                       master@{1}

There is no copying involved anywhere . It only creates a new commit, M, and that commit allows 'master' (that used to point at B) to reach both the history leading to B and the history leading to Z.

On the other hand, rebase _copies_. It _does_ make new commits A' and B', whose _effect_ to the tree mimics those of A and B.

        $ git rebase origin master
                X---Y---Z---A'---B' master
               /        ^origin
              /
             O----A----B master@{1}
Show 8 quoted lines
> 4) It would be good to have a section "Cutting branches"
>
>    How do I remove the N most recent commits from a branch?
>
>                D---E---F---G---H---.........---Y---Z master
>
>   =>
>                D---E master
And it is not even cutting.  It merely makes this:
                      F---G---H---.........---Y---Z master@{1}
                     /
                D---E
                    ^master

so that a new history can grow at E that is parallel to the history that leads to Z.

I think your confusion is primarily coming from not understanding what a branch in git is. A branch in git does not have its own identity per-se, and a commit does _not_ belong to a branch, in the sense that a commit object does not record anywhere on which branch it was created on. A branch is just a pointer into a dag and the pointer can be moved.

Previous: Andreas EricssonNext: Bruno Haible
Message 6 of 10 in “request for documentation about branch surgery”
  1. Bruno HaibleJul 6, 2009
  2. Elijah NewrenJul 7, 2009
  3. Elijah NewrenJul 7, 2009
  4. Bruno HaibleJul 7, 2009
  5. Andreas EricssonJul 7, 2009
  6. Junio C HamanoJul 7, 2009
  7. Bruno HaibleJul 7, 2009
  8. Andreas EricssonJul 7, 2009
  9. Junio C HamanoJul 7, 2009
  10. Daniel BarkalowJul 7, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.