Skip to contentSkip to conversation

Duplicating a branch

DISCUSSION Aaron Gray 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore

Aaron Gray

author
#0 lore raw

Hi,

How do I go about duplicating a branch within a repository, so I can make test mods ?

Many thanks in advance,

Aaron

#1 lore raw
Quoted 2 lines · Aaron Gray

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
author
#2 lore raw

Nice expanation and mini tutoial Junio !

Thanks ever so much, very useful.

Aaron

Quoted 48 lines
Quoted 2 lines · Aaron Gray

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