228

I have a branch that I'd like to move into a separate Git repository, and ideally keep that branch's history in the process. So far I've been looking at git filter-branch, but I can't make out whether it can do what I want to do.

How do I extract a Git branch out into its own repository?

Costique
  • 23,712
  • 4
  • 76
  • 79
Aupajo
  • 5,885
  • 6
  • 30
  • 28

2 Answers2

370

You can simply push a branch to a new repository. All of its history will go with it. You can then choose whether to delete the branch from the original repository.

e.g.

git push url://to/new/repository.git branch-to-move:new-branch-name

For a new repository, new-branch-name is typically master.

Creating a new, empty repository can be done with git init.

Monica Cellio
  • 1,559
  • 20
  • 25
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 19
    Just in case anyone's wondering, I substituted the URL with the path to a Git repository directory, and that worked perfectly. – Aupajo Feb 10 '10 at 08:32
  • 52
    You can also name the branch name in the new repository: `git push url://to/new/repository.git branch-to-move:new-branch-name` – Yves Van Broekhoven Oct 28 '13 at 10:00
  • 5
    Yves' above comment is correct, otherwise you'll have no master in there and the cloning process will conclude `warning: remote HEAD refers to nonexistent ref, unable to checkout.`. If already at that stage, just go with `git checkout -b branch-to-move` and you're safe or you simply go with `git checkout -b master` to have your master branch :) – Ain Tohvri Sep 12 '14 at 08:29
  • `git checkout -b master` solved my problem it was on the screen `error: src refspec master does not match any.` (only wonder why I didn't see the branch name I was at) Thank you – kangkyu Oct 31 '15 at 06:55
  • Need `git config receive.denyCurrentBranch warn` for new repo because `refusing to update checked out branch: refs/heads/master By default, updating the current branch in a non-bare repository is denied, because it will make the index and work tree inconsistent with what you pushed, and will require 'git reset --hard' to match the work tree to HEAD.` – wener Dec 13 '15 at 17:09
  • @wener another is to create a bare repo with `git --bare init` – Burnash Apr 01 '16 at 19:19
  • 1
    note that this will copy not just the branch and its history, but all the history back to the beginning of the repository, including the history along the branch(es) that the specified branch was branched from – simpleuser Apr 18 '18 at 17:48
  • I had to throw the -f flag in to overwrite the existing history of the remote branch – mikeLundquist Apr 19 '18 at 17:12
  • This does not copy any tags – Ismail Iqbal Sep 06 '18 at 05:18
  • Replying to my own comment to push the tags please use the following command `git push url://to/new/repository.git --tags` kudos – Ismail Iqbal Sep 06 '18 at 05:32
  • 6
    In case this answer was confusing for you too, here's how I made sense of it in order to make it work: 1) in your terminal, be in the directory that contains the repo and branch you want to make into a new repo; 2) the `url` should be a git url, like git@github.com:brianzelip/groceries.git; 3) `branch-to-move` is the branch of the current repo you want to make into a new repo; 4) `new-branch-name` is the name you want for the new branch in the new repo being created, ie: `master`. – Brian Zelip Oct 13 '18 at 18:48
  • this command keep all history, without history i need to move only on that branch changes only how ? how i achieve this – yala ramesh May 23 '19 at 12:31
  • Worked for me only after adding refs: ```git push url://to/new/repository.git branch-to-move:refs/heads/new-branch-name``` – Amit Merin Nov 04 '19 at 18:06
  • Huge thumbs up, I accidentally pushed a bunch of consecutive commits to the wrong repo and this helped me get back on track! –  May 03 '20 at 19:53
  • Does this command delete the existing branch (in the old repo) after moving ? – Youssef N Aug 17 '21 at 07:27
  • @YoussefN Remove branches doesn't have any meaning. Git branch is just a pointer to specified commit. – Thinh Vu Aug 20 '21 at 11:02
  • this answer should be updated to have `https://` instead of `url://`. using `url://` gives me `git: 'remote-url' is not a git command. See 'git --help'.` – mangusta Mar 14 '23 at 06:10
  • Related to this (but I don't think it warrants a new question!) does anyone know if there is a simple way to do this in SourceTree. – Bill Naylor Apr 05 '23 at 20:20
21

This will keep the history of all the branches, but make your copy point to the one branch in particular:

git clone -b newbranch CurrentRepo NewRepo

This does not 'move' anything, just makes a copy.

Damon
  • 687
  • 3
  • 17