How can I push a local Git branch to a remote with a different name easily?

前端 未结 5 1482
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 16:38

I\'ve been wondering if there\'s an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.

For exa

5条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 17:17

    Here's the process that has worked for me.

    git clone original-repo-url
    git remote rename origin upstream
    git remote add origin new-repo-url
    

    Now your new repo will be ‘origin’ and the original repo is ‘upstream’. Confirm it by running git remote -v. (Side note: Upstream is used to fetch from the original repo - in order to keep your local copy in sync with the project you want to contribute to - and origin is used to pull and push since you can contribute to your own repo).

    git push origin master

    Now your new remote repo's master (on Github) will be in-sync with the original master, but it won't have any of the feature branches.

    git rebase upstream/branch-name
    git push origin master
    

    Rebase is a smart merge. Then push to master again and you’ll see the selected feature branch as master on the new repo.

    Optional:

    git remote rm upstream
    git remote add upstream new-repo-url
    

提交回复
热议问题