Leo
Leo

Reputation: 3

Moving GIT repository (and commit history) to another repository currently empty

I have a code in a GIT repository repA and I need to move it to another repository repB that is currently empty but having now the commit and tag history of repA in repB.

Any advice on the workflow/commands for this process?
Thanks in advance!

Upvotes: 0

Views: 62

Answers (1)

CodeWizard
CodeWizard

Reputation: 142552

You simply need to add new remote and than push your code to the new repo

git remote add origin2 <url>
git push origin2 <branch name>

Here is a scrip which im using to checkout all branches and than push them to the new remote

# first add the new rmeote
git remote add origin2 <new-url> 

#!/bin/bash

# loop over all the original branches
# 1. check them out as local branches 
# 2. set the origin as the track branch
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
    git branch --track ${branch#remotes/origin2/} $branch
done

# now push all branches and tags
git push origin2 --all    
git push origin2 --tags

What does the script do?

git branch -a
get a list of all the local branches

| grep remotes The branch names are : 'remotes/origin/' so this will remove the remotes from the branch names

| grep -v HEAD | grep -v master
remove the master (current branch) and HEAD which is an alias to the latest commit

Upvotes: 4

Related Questions