Migration of local git repository to remote git repository

For the convenience of the following discussion, we will call the git warehouse to be migrated as a local warehouse, and the remote git warehouse as a remote warehouse. Requiring to keep the submission records, the following is the migration steps.

1. Create an empty remote warehouse

1.1. Create on GitLab web page

After the creation is complete, you will get the git address: http://10.69.12.11/android/hello.git

1.2. Create using API provided by GitLab or GitHub

In order to use the API in the simplest way, I will write a shell script in the ~ / .bashrc configuration file to complete:

~$ vim ~/.bashrc

Add the following content at the end of .bashrc:

  • GitLab version:
repo_create(){
		# 判断是否存在参数,有,则赋给repo_name变量,否则使用当前目录作为repo_name的值
        if [ $1 ];then
                repo_name=$1
        else
                repo_name=`basename $(pwd)`
                echo "set Repo Name to ${repo_name}"
        fi
        # 使用GitLab Api创建远程仓库
        # oExkVZEW2WKDcoF6mXdW是Personal access tokens,在gitlab web上创建
        # name=${repo_name}是指定仓库的名称
        # namespace_id=12是group id可以在gitlab上查到
        # http://10.69.12.11/api/v4/projects,则是GitLab服务器的地址,及api
        curl -k --request POST --header "PRIVATE-TOKEN:oExkVZEW2WKDcoF6mXdW" --data "name=${repo_name}&namespace_id=12" http://10.69.12.11/api/v4/projects
}
  • GitHub version:
repo_create(){
		# 判断是否存在参数,有,则赋给repo_name变量,否则使用当前目录作为repo_name的值
        if [ $1 ];then
                repo_name=$1
        else
                repo_name=`basename $(pwd)`
                echo "set Repo Name to ${repo_name}"
        fi
        # 使用GitHub Api创建远程仓库
        # 8ad7a0e3faaa7574c4c1f38b5280c7d9ecf980ab是Personal access tokens,在GitHub上创建
        # '{"name":"'${repo_name}'"}'是指定仓库的名称
        curl -u '[email protected]:8ad7a0e3faaa7574c4c1f38b5280c7d9ecf980ab' https://api.github.com/user/repos -d '{"name":"'${repo_name}'"}'
}

Run the source command to use the configuration file to take effect:

~$ source ~/.bashrc

Use repo_create to create a remote warehouse:

~$ repo_create Hello

After successful creation, it will return to http://10.69.12.11/android/hello.git.
GitLab API reference https://docs.gitlab.com/ce/api/

2. Delete the remote warehouse originally associated with the local warehouse

~/local_project$ git remote rm origin

3. Associate the local warehouse to the new remote warehouse

~/local_project$ git remote add origin http://10.69.12.11/android/hello.git

Note : If the operation of point 2 is not performed, the following error will be reported:

fatal: remote origin already exists.

4. Submit the code of the local warehouse to the cache to be submitted

~/local_project$ git add .

5. Write a note (optional)

~/local_project$ git commit -m "迁移Local_project项目到远程hello仓库"

6. Merge history (when the remote warehouse is created and initialized, it has done git init operation, it is necessary to perform this step of operation, otherwise skip)

6.1. Specify the branch to be merged

~/local_project$ git branch --set-upstream-to=origin/master master

Note: The above means to merge the local master branch with the remote origin / master branch.

6.2. Start merge

~/local_project$ git pull origin master --allow-unrelated-histories

7. Submit the code

Submit code to remote warehouse

~/local_project$ git push -u origin master

Click to follow:

Insert picture description here

Published 381 original articles · praised 85 · 80,000 views +

Guess you like

Origin blog.csdn.net/weixin_40763897/article/details/105555429