git - create local branch from existing remote branch

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

I want to create branch from existing remote branch (lets say remote-A)and commit the changes to repo.

I have used below commands to create local branch from existing remote-A

$git checkout remote-A  git branch master * remote-A

Now I have created local-B from Remote A using below command

git branch local-B git checkout local-B

How to make sure the changes I have on local-B is on top of remote-A so that when i do the push on remote repo for branch local-B is on top of remote-A?

回答1:

you want to create branch on base of remote-A, make changes on it and then push them on remote-A?

git checkout -b remote-A git pull origin remote-A git checkout -b remote-B

make changes on remote-B

 git commit -a -m 'describe changes on remote-B branch'   git checkout remote-A    git merge remote-B    git push origin remote-A


回答2:

Old post, still I'd like to add what I do.

1. git remote add <remote_name> <repo_url> 2. git fetch <remote_name> 3. git checkout -b <new_branch_name> <remote_name>/<remote_branch_name>

This series of commands will

  1. create a new remote,
  2. fetch it into your local so your local git knows about its branches and all,
  3. create a new branch from the remote branch and checkout to that.

Now if you want to publish this new local branch to your remote and set the upstream url also

git push origin +<new_branch_name>

Also, if only taking in remote changes was your requirement, you could have done, instead of step 3,

git pull --rebase <remote_name> <remote_branch_name>

and then opted for git mergetool (needs configurations separately) in case of any conflicts, and follow console instructions from git.



回答3:

To make sure your changes are on top, you must not pull from remote. you must fetch and rebase. il will be something like this:

fetch->stash->rebase->stash pop->commit->push


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!