Git與Github的使用總結(jié)

一.上傳本地代碼到Github

(1)新建repository
(2)填寫相關(guān)參數(shù)
(3)在本地新建一個項目,然后打開Git Bash Here,依次輸入如下命令

  1. git init
  2. git add --all
  3. git commit -m "xxx"
  4. git remote add origin [xxx]
  5. git pull origin master
  6. git push -u origin master

(4)執(zhí)行完上面的命令,提示如下錯誤

  $ git push -u origin master
  To https://github.com/AndryYu/KtHand.git
   ! [rejected]        master -> master (fetch first)
  error: failed to push some refs to 'https://github.com/AndryYu/KtHand.git'
  hint: Updates were rejected because the remote contains work that you do
  hint: not have locally. This is usually caused by another repository pushing
  hint: to the same ref. You may want to first integrate the remote changes
  hint: (e.g., 'git pull ...') before pushing again.
  hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決方法:

  1. git pull origin master --allow-unrelated-histories 
  * (git  pull  --rebase origin master)
  2.  然后手動修改沖突文件
  3. git rebase --continue
  4. git push -u origin master

二.切換遠程分支

git clone只能clone遠程庫的master分支,無法clone所有分支。

  1. 找一個干凈的目錄xxx
  2. cd xxx
  3. git clone http://myrepo.xxx.com/project/.git 
  4. cd project
  5. git branch -a //列出所有分支名稱如下
        remotes/origin/dev
        remotes/origin/release
  6. git checkout -b dev origin/dev //作用是checkout遠程的dev分支,在本地起名為dev分支,并切換到本地的dev分支
  7. git checkout -b release origin/release //作用參見上一步解釋
  8. git checkout dev  //切換回dev分支,并開始開發(fā)。

三.修改遠程倉庫地址

1.修改命令
  git remote set-url origin [url]
2.先刪后加
  git remote rm origin
  git remote add origin [url]
3.直接修改config文件 //沒試過

四.提交代碼

  1. git add -A
  2. git commit -a '提交說明'
  3. git pull origin master   //拉取遠程倉庫最新代碼
  3. git push origin  分支名

五.區(qū)別比較

1. git fetch與git pull的區(qū)別

git fetch
相當于從遠程獲取最新版本到本地,不會自動merge.

  1. git fetch origin master //首先從遠程的origin的master主分支下載最新的版本到origin/master分支上
  2. git log -p master..origin/master  //然后比較本地的master分支和origin/master分支的差別
  3. git merge origin/master   //最后進行合并

git pull
相當于從遠程獲取最新版本并merge到本地。

git pull origin master  //上述命令其實相當于git fetch 和 git merge

在實際使用中,git fetch更安全一些。因為在merge前,我們可以查看更新情況,然后再決定是否合并。但是如果只關(guān)心沖突的話,可以用git pull,自動合并。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容