github代碼管理實用命令及注意要點記錄
ubuntu上安裝git
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git
git --version
git代碼管理實用命令總結
全局配置生成ssh密匙:
# global表示本機器上所有Git倉庫都會使用這個參數
git config --global user.name "mochenge"
git config --global user.email "[email protected]"
# 查看用戶名,郵箱,及所有用戶信息
git config user.name
git config user.email
git config --list
# 生成密匙,
ssh-keygen -t rsa -C "[email protected]"
# 命令執行完畢會生成一個名爲id_rsa.pub的文件。利用文本編輯器打開該文件,全文複製。
# 打開GitHub進入setting,設置deploy keys,用gedit 打開id_rsa.pub中的內容粘貼進去即可。title自定義就可以
gedit id_rsa.pub
# 記住登錄密碼
git config --global credential.helper store
克隆別人的倉庫(包括fork別人的)到本地步驟(暫存-提交暫存-push到雲端);
1.克隆倉庫
git clone git@github.com:icomm--/test01.git
2.刪除.git目錄,並進行初始化
ls -a
rm -rf .git
git init
3.綁定本地文件夾和GitHub倉庫,此步驟可以放在commit之後
git remote add origin git@github.com:icomma00/test01.git
4.測試連接情況:
ssh -T git@github.com
5.先進行一次拉取,再進行一次推送測試:
git pull test01 master
git push --force test01 master
6.在本地修改倉庫內容,通常用vscode打開後進行更改
# 創建文件
touch text1.md
# 向文件中插入內容
7.將改動staged,表示將改動暫存起來
# 全部改動staged
git add .
# 針對指定文件改動
git add text1.md
8.將工作區改動提交到本地倉庫
git commit -m "first commit"
9.將改動推送到遠端倉庫
# 推送到遠端master分支
git push -u origin master
# 推動到遠端名爲name1的分支
git push -u origin name1
常見問題:
問題1.fatal: remote origin already exists
方法:
git remote rm origin
git remote add origin git@git.com:******
問題2:The authenticity of host 'github.com ’ can’t be established
這是由於你的git地址採用了ssh方式,切換爲https方式即可,也可能是你的倉庫地址不對,可以用命令先查看一下:
git remote -v
git remote set-url origin https://github.com/your-github-name/reposition-name.git
問題3:fatal: refusing to merge unrelated histories
因爲系統認爲本地庫和遠程庫是兩個完全不相干的庫,因此不可以合併
解決方法1
git pull origin master --allow-unrelated-histories