Move git branch from one repository to another with preserving history

Kasun Siyambalapitiya
2 min readOct 23, 2019

--

Image Source: https://www.geograph.org.uk/photo/4272215

There can be times that your company has build a certain product/app with a specific language at past and now you are working on implementing its newer version in a different language due to the unique or specific features being provided by the new language.

For a example if the initial product/app version was implemented using Java, there is a high chance that its newer version may be implemented using go language . This is because a platform specific JVM needs to be installed on the running instance in order to execute Java programs and for go programs there is no such requirement.

Regardless of the new language you choose for the implementation, more than 90% of the time you may be using a separate branch in the same repository for this development.

As time passes by your new implementation becomes steady and stable (with tests and all 😊 ) and you will end up with either of the following two options to choose from

  1. Make the new implementation the master branch of the repository
  2. Move the new implementation branch to a different repository with preserving history.

The first option can easily be done from the Github UI by a user with admin permissions. But what if your company decided to go with the second option and you were chosen as the person to perform it 😲.

Do not worry, it very simple and straight forward 😌 🤗

let’s dig in 🤓

  1. First clone your initial repository to a new location (say ~/saveMe/initialRepo).
mkdir -p ~/saveMe/initialRepo
cd ~/saveMe/initialRepo
git clone <initial repository URL>

Ex:

git clone https://github.com/kasunsiyambalapitiya/awesome-project.git

2. Checkout to the new implementation branch by creating a new local branch. (say go-implementation)

git checkout -b go-implementation origin/go-implementation

3. Create the new repository at github, gitbucket or etc.

4. Clone the new repository to a new location. (say ~/saveMe/newRepo)

mkdir -p ~/saveMe/newRepo
cd ~/saveMe/newRepo
git clone <new repository URL>

Ex:

git clone https://github.com/kasunsiyambalapitiya/super-awesome-project.git

5. Create a new remote inside the new repository (super-awesome-project) for the initial repository (awesome-project)

git remote add initial-repo <path-to-cloned-initial-repository>

Ex:

git remote add initial-repo ~/saveMe/initialRepo/awesome-project

6. Pull all the files and directories from initial repository (awesome-project) go-implementation branch to the master branch of the new repository (super-awesome-project) with preserving history.

git pull initial-repo go-implementation --allow-unrelated-histories

7. Remove the remote added for the initial repository (awesome-project) inside the new repository (super-awesome-project).

git remote rm initial-repo

8. Add all changes and push to github.

git add .
git push origin

You got saved !!!! 🤓

--

--