天天看點

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)如果本地倉庫有多個分支,則需先手動建立對應分支連接配接,然後pull,最後再push來實作同步:

#git checkout branchName        //預設master分支

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

#git push

***Master以外其他分支:

#git checkout branchName  

#git push --set-upstream origin branchName    //将分支推送至遠端端,進而在遠端端建立同名的分支

二)克隆遠端倉庫

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

or

git clone https://[email protected]/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