1.git的工作流程:
圖片引用:https://www.bilibili.com/video/BV1mb411n7Nw?p=3
2.開始創建git賬號:以國內的碼云爲例
2.1 、到碼雲官網註冊賬號:https://gitee.com/signup
我就推薦使用這兩個吧,方便的話就用微信,我直接綁定我的github就得。
3.新建倉庫
3.1 :登錄好之後就可以新建倉庫了:
好的目前爲止倉庫就創建完成了。
備註:issues
issues功能被用來追蹤各種想法,增強功能,任務,bug等。許多的項目通過centeral bug tracker收集用戶的反饋。
4.git拉取項目操作:
4.1、 fork:從別人開放得遠程倉庫複製到自己的遠程倉庫
比如我從一個大佬的開源項目複製過來自己的倉庫:
然後在我的倉庫中就有這個項目作爲一個分支:
4.2、 clone:從遠程倉庫克隆項目到自己電腦
比如我要克隆剛剛我新建的項目到我的上進項開發:
注:如果clone不是自己的項目,那麼修改後沒有開發者以上權限時不可以推送代碼到別人遠程倉庫的
使用vscode克隆clone項目:
回車選擇存儲位置就得了。。。
4.3、 pull:從遠程倉庫合併代碼到自己的工作區
git pull [分支],不寫默認master分支
比如我倉庫新增一個文件,達到效果:倉庫更新但是本地還沒跟新
然後在項目目錄下使用:git pull
4.4、 fatch:從遠程倉庫合併代碼到自己的本地倉庫
git fetch
4.5、 merge:從自己本地倉庫合併到代碼到自己工作區
git merge
小總結:
fatch + merge == pull
5.git提交項目操作:
5.1、遠程倉庫沒有源
- git init #初始化項目
- git add . # 提交項目到git本地庫
- git commit -m “備註信息” # 提交到本地庫
- git remote add origin https://gitee.com/miaoderen/VueDemo.git #關聯遠程倉庫
- git push -u origin master #推送到github上
可能會報這個錯誤:
To https://gitee.com/miaoderen/VueDemo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://gitee.com/miaoderen/VueDemo.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.
- 原因是:
本地沒有遠程庫的文件
- 解決方法:
git pull --rebase origin master
再提交:
git push -u origin master
完成效果:
小總結:每次提交之前都要
- git pull
- git commit -m “修改的信息”
- git push
三連操作
6.常用錯誤和莫名其妙:
- fatal: refusing to merge unrelated histories
- git pull --tags origin master
From https://gitee.com/miaoderen/VueDemo
* branch master -> FETCH_HEAD
fatal: refusing to merge unrelated histories
原因:拒絕合併無關歷史
解決方法:
git pull origin master --allow-unrelated-histories
- fatal: The current branch master has no upstream branch.
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
原因:沒有將本地的分支與遠程倉庫的分支進行關聯
解決方法:
git push -u origin master
7.常用命令:
打勾常用
- git pull origin master --allow-unrelated-histories
- git init :初始化一個git項目
- git clone :克隆一個項目到本地
- git pull : 合併遠程倉庫項目的更新
- git pull https://gitee.com/miaoderen/VueDemo.git chaoge :從指定項目的分支拉去代碼
- git add :添加當前目錄的所有文件到暫存區
- git commit :提交暫存區到本地倉庫
- git push :提交代碼到遠程倉庫
- git diff :顯示暫存區和工作區的差異
- git checkout :切換到指定分支,並更新工作區
- git fetch :下載遠程倉庫的所有更新
- git merge :合併指定分支到當前分區
- git branch -a :查看倉庫所有分支
8.衝突解決:
- 強制push本地倉庫到遠程 (這種情況不會進行merge, 強制push後遠程文件可能會丟失)
- git push -u origin master -f
- 避開解決衝突, 將本地文件暫時提交到遠程新建的分支中,創建完branch後, 再進行push。
- git branch [name]
- git push -u origin [name]