您的位置:首页 > 其它

Git手册 - 分支远程同步

2017-06-15 10:30 267 查看
一)将本地分支连接到远程分支,这里以GitHub上的同名仓库(项目)为例

1)进入本地项目主分支
2)运行以下命令:
#git remote add origin git@github.com:path/repoName.git //将远程仓库命名为origin
3)当本地仓库与远程仓库都仅有一个主分支时,运行以下命令:
#git push -u origin master //加参数-u,git不仅会将本地主分支所有内容推送到远程主分支,还会自动将本地主分支与远程主分支连接起来

注:
1)手动建立分支连接命令:
#git branch --set-upstream-to=origin/branchName branchName(local)
2)如果本地仓库有多个分支,则需先手动建立对应分支连接,然后pull,最后再push来实现同步:
#git checkout branchName //默认master分支
#git branch --set-upstream-to=origin/branchName branchName(local)
#git pull origin branchName --allow-unrelated-histories
#git push
***Master以外其他分支:
#git checkout branchName
#git push --set-upstream origin branchName //将分支推送至远程端,从而在远程端创建同名的分支

二)克隆远程仓库
1)通过SSH
git clone git@github.com:TaoismLEE/Git.git
Notes 1:
A. After modifying, use: "git push origin master" to push the modification.
B. Can use: git remote -v to check the information about remote repository.
Notice 2:
If clone using SSH method, the data transfered between Git and Github will be encrypted with SSH key, so some settings should be made firstly in order to fetch the project from Github successfully:
Setting SSH key:
A. In home folder, check whether the .ssh folder exists, if exists,then check whether there are "id_rsa" and "id_rsa.pub" files
B. If the folder or the files not exist, then generate them by running following commands:
#ssh-keygen -t rsa -C "email-address" //configured email of git
C. Setting SSH Key in Github with the content of id_rsa.pub file //Open "Account settings" -> "SSH Keys"; then click "Add SSH Key";fill in "Title"; in the Key textarea, paste the content of id_rsa.pub file
2)通过HTTPS
git clone https://github.com/TaoismLEE/Git.git
Notice:
If clone using HTTPS method, will need accout and password of remote Github when pushing modification to remote repository. OR running below command to store and remember the remote account information:
#git config --global credential.helper store
3)克隆后获取非主分支
#git checkout -b dev(suggest to name the new local branch the same with the remote branch) origin/dev(other branch in remote repository)

三)协同工作
A. First, try to use "git push origin branch-name" to push our own modification
B. If conflict, then should use "git pull" to pull out the latest code
C. Fix conflicts manually and commit as a new version
D. Run "git push origin branch-name" again to push our code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Git分支管理