├── .gitignore ├── README.md └── transfer.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | .idea 3 | .idea/workspace.xml 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Git Transfer 2 | This is a python script that will transfer all the branches and tags of your git project to a new remote repository. 3 | 4 | #### Purpose 5 | When we start working on a existing project, we may not **checkout** to all the **branches** while working on the project. So when we try to transfer the full repository we cannot push all the branches. This project is meant to solve the problem. 6 | 7 | #### Prerequisites 8 | - Have to have **python2.7** installed in your local machine. 9 | - Have to have **git** installed 10 | - It is better if you setup ```ssh``` with your [bitbucket][69e33d05] or github account 11 | - A freshly created repository 12 | 13 | [69e33d05]: https://bitbucket.org/ "bitbucket" 14 | 15 | #### Installation 16 | - ```git clone git@github.com:rhasan33/git-transfer.git```. I am using ssh here. You can use https as well 17 | - ```cd git-transfer``` 18 | - ```cp transfer.py /path/to/your/local/project/repository``` 19 | 20 | #### Usage 21 | - ```cd /path/to/your/local/project/repository``` 22 | - Run ```python transfer.py``` 23 | - It will ask for your new git repository's ssh address. 24 | - Then it will ask for your old repositories remote alias (normally origin) 25 | - If it is not origin then the prompt will ask for y/n to be authenticated. 26 | - It will ask for the new remote's alias. 27 | - If the new remote alias cannot be similar to old remote alias. 28 | 29 | That's it. Now you need to wait for the repository to be transferred. 30 | 31 | **Thanks** 32 | -------------------------------------------------------------------------------- /transfer.py: -------------------------------------------------------------------------------- 1 | import os 2 | import json 3 | import subprocess 4 | 5 | class GitTransfer(): 6 | def __init__(self, new_repo, old_alias, new_alias): 7 | self.path = os.getcwd() 8 | self.new_repo = new_repo 9 | self.old_alias = old_alias 10 | self.new_alias = new_alias 11 | 12 | def check_path(self): 13 | return os.path.exists(os.path.join(self.path, '.git')) 14 | 15 | def get_current_repo_branches(self): 16 | old_origin_branches = [] 17 | branches = subprocess.check_output('git branch --all', shell=True) 18 | for branch in branches.split('\n'): 19 | if branch.find('remotes') is not -1: 20 | old_origin_branches.append(branch.strip().replace("remotes/%s/" % self.old_alias, "")) 21 | return old_origin_branches 22 | 23 | def transfer(self): 24 | data = dict() 25 | if not self.check_path(): 26 | data.update({ 27 | 'error': 'This is not a local git repository.' 28 | }) 29 | return json.dumps(data) 30 | 31 | if not self.new_repo.endswith('.git') or not self.new_repo.startswith('ssh://'): 32 | data.update({ 33 | 'error': 'This is not a valid repo.' 34 | }) 35 | return json.dumps(data) 36 | 37 | all_old_repo_branches = self.get_current_repo_branches() 38 | os.system("git remote add %s %s" % (self.new_alias, self.new_repo)) 39 | 40 | print "First add the master branch to the new repo" 41 | os.system("git checkout master") 42 | os.system("git push --all %s" % self.new_alias) 43 | os.system("git push --tags %s" % self.new_alias) 44 | 45 | for branch in all_old_repo_branches: 46 | print "\n\n------------- Now pushing the branch %s --------------\n\n" % branch 47 | os.system("git checkout %s" % branch) 48 | os.system("git push --all %s" % self.new_alias) 49 | os.system("git push --tags %s" % self.new_alias) 50 | os.system("git remote rm %s" % self.old_alias) 51 | print "Old remote is deleted" 52 | os.system("git remote rename %s %s" % (self.new_alias, self.old_alias)) 53 | print "Old remote %s becomes %s" % (self.old_alias, self.new_alias) 54 | 55 | data.update({ 56 | 'success': 'The new repo for your project is: ' + str(self.new_repo) 57 | }) 58 | return json.dumps(data) 59 | 60 | new_repo = raw_input("Provide your new repository (SSH URL)\nFor example: 'ssh://path/to/git/repo/project.git': ") 61 | old_alias = raw_input("You old repository alias (eg. origin):") 62 | if old_alias != 'origin': 63 | print old_alias 64 | permission = raw_input("Are you sure about your old alias? (y/n): ") 65 | else: 66 | permission = 'y' 67 | if permission != 'y': 68 | print json.dumps({"error": "Please try again. Process exit."}) 69 | exit() 70 | new_alias = raw_input("You new alias which will be renamed to old one at the end of the process: ") 71 | if old_alias == new_alias: 72 | print json.dumps({"error": "Old alias cannot be same as new alias. Process exit."}) 73 | exit() 74 | gt = GitTransfer(new_repo, old_alias, new_alias) 75 | print gt.transfer() 76 | --------------------------------------------------------------------------------