---
# System prepended metadata

title: 搬移 Remote Repository 到另一個 Remote Repository
tags: [Git]

---

---
title: 搬移 Remote Repository 到另一個 Remote Repository
tags: Git
---

# 搬移 Remote Repository 到另一個 Remote Repository

## 前言
當原本的 remote repository 位於私人網域，GitHub 或 Bitbucket 可能無法透過 http 或 ssh 複製 remote repository，此時可以採用以下做法：先將 repository 拉至本地端，再重新 push 新的 repository 上。

## 操作說明
1. clone remote repository
	```bash
	git clone ssh://git@my_repo_host/my_repo.git
	```

2. 進入資料庫，將所有 remote branch 拉下來
	```bash
	cd my_repo
	for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; 
		do git branch --track ${branch#remotes/origin/} $branch;
	done
	```

3. 刪除原本的 origin
	```bash
	git remote rm origin
	git remote -v # Check
	```

4. 新增新的 origin
	```bash
	git remote add origin ssh://my_new_repo_host/my_repo.git
	git remote -v # Check
	```

5. 將本地 repository 推上新的 repository
	```bash
	git push -u --all origin
	```
	
---	
## 參考資料
* [How to clone all branches and tags between remote git repositories.](https://coderwall.com/p/czbnaq/how-to-clone-all-branches-and-tags-between-remote-git-repositories)
* [Git clone all remote branches locally.](https://coderwall.com/p/0ypmka/git-clone-all-remote-branches-locally)
