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

Re: Duplicating a branch

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 2, 2009, 15:36 UTC
Message-ID
<7voct6fy1e.fsf@alter.siamese.dyndns.org>
In-Reply-To
<BAF61FDE71D344FDB9F1415B5E2AFD75@HPLAPTOP>
"Aaron Gray" <aaronngray.lists@googlemail.com> writes:
> How do I go about duplicating a branch within a repository, so I can
> make test mods ?
Run
    $ git checkout -b experiment master

to make a new "experiment" branch that points at the same comit as "master" (or whatever other branch), hack away (including committing which would grow the history of "experiment" branch without touching "master").

After you are done, and if you want to discard it, simply:
    $ git checkout master
    $ git branch -D experiment
or if you want to keep all of it:
    $ git checkout master
    $ git merge experiment

*BUT* if your "experiment" was truly exploratory in the sense that your history is full of "let's try this, commit to snapshot, test, oops, it did not work, let's try that, commit to snapshot, test, ok, I made some progress, let's continue" crufts, you would most likely want to clean-up your history before the latter "checkout master and merge experiment into it" steps with something like:

    $ git rebase -i master experiment

If you in advance know that what you are going to do is truly "throw-away experiment", you do not even need to use an "experiment" branch. You can do your exploration while on a detached HEAD:

    $ git checkout master^0

and hack away, including making commits (but you will discard them in the end), and then finally:

    $ git reset --hard ;# if you have local changes you do not want to
			  take back to master
    $ git checkout master
    
Previous: Aaron GrayNext: Aaron Gray
Message 2 of 3 in “Duplicating a branch”
  1. Aaron GrayJun 2, 2009
  2. Junio C HamanoJun 2, 2009
  3. Aaron GrayJun 2, 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.