Git手册 - 分支远程同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git手册 - 分支远程同步相关的知识,希望对你有一定的参考价值。

一)将本地分支连接到远程分支,这里以GitHub上的同名仓库(项目)为例

1)进入本地项目主分支

2)运行以下命令:

#git remote add origin [email protected]:path/repoName.git        //将远程仓库命名为origin

3)当远程仓库仅有一个主分支时,运行以下命令:

#git push -u origin master        //加参数-u,git不仅会将本地主分支所有内容推送到远程主分支,还会自动将本地主分支与远程主分支连接起来


注:

1)手动建立分支连接命令:

#git branch --set-upstream-to=origin/branchName branchName(local)

2)如果远程仓库有多个分支,且有commit历史版本,则需先手动建立对应分支连接,然后pull,最后再push来实现同步:

#git branch --set-upstream-to=origin/branchName branchName(local)

#git pull origin branchName --allow-unrelated-histories

#git push


二)克隆远程仓库

1)通过SSH

git clone  [email protected]: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手册 - 分支远程同步的主要内容,如果未能解决你的问题,请参考以下文章

Git branch 和fetch 指令总结:分支查看和远程分支同步

GIT实践:同步远程分支

git 同步远程仓库分支

git 同步远程仓库分支

git 同步远程已删除的分支和删除本地多余的分支

git同步远程已删除的分支和删除本地多余的分支