使用Git遇到的問題及解決方案

第一次使用Git進行代碼提交,遇到一些問題,於此記錄並分享之

使用Git初體驗

首先做了一個練習項目,在本地項目目錄進行了初始化命令:

git init

在github網站新建了一個repository,勾選了Initialize this repository with a README,cloneSSH格式的倉庫地址,現在開始提交:

git add -A
git commit -m "提交備註信息"//可選
git remote add [email protected]:account/repository

錯誤及解決方案

提示出錯信息:
fatal: remote origin already exists.
解決方案:

git remote rm origin
git remote add [email protected]:account/repository

這個錯誤應該不會再出現了。
然後提交的時候,又出現錯誤:
fatal: Could not read from remote repository.Please make sure you have the correct access rights.and the repository exists.
出現這個問題是因爲,沒有在github賬號添加SSH key
解決方案: 這個錯誤可以用命令行處理掉,如下:

ssh-agent
ssh-add ~/.ssh/id_key

但簡潔的方法是使用GitGui的show SSH Key工具,如圖所示:
GitGui的show SSH Key工具
然後將生成的SSH Key複製,打開Github網站,在setting選項頁中點擊SSH and GPG keys鏈接,
SSH and GPG keys
點擊右上角的New SSH Key按鈕,將之前複製的SSH Key粘貼上去,title隨便起個名字。
於此,這個錯誤已經解決了。
又出現新的錯誤:
failed to push some refs to '[email protected]:account/repository
原因在於github上創建倉庫,建立README.md,導致該文件不在本地代碼中,可以通過以下方式解決:

git pull --rebase origin master
//把遠程服務器github上面的文件拉下來
//再次執行git push origin master即可完成代碼上傳
git push origin master

終於,項目上傳到Git倉庫中了。

Git清除本地緩存命令:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

git設置全局忽略文件

  1. 在個人文件夾添加文件*.gitignore_global*,在裏面寫入你要全局忽略的內容,語法同.gitignore文件
  2. 運行命令git config --global core.excludesfile ~/.gitignore_global

git同步fork源倉庫

# checkout 出你要合併的目標分支,這裏是master分支
git checkout master
# 下載(fetch)遠程的代碼變動
git fetch upstream
# 合併
git rebase
# 將合併後的代碼push到你的遠程分支上
git push
# 至此, origin的master branch已經於原作者項目同步了。

使用git merge, 會讓你當前在working branch上面已經做的更改與upstream master的更改在timeline上出現分支. 而使用rebase, 會把你的更改加到upstream master更改的後面, 結果是整體時間軸呈線性的, 沒有分岔。

git stash 使用

  1. 可以在一個分支上保存一個儲藏,切換到另一個分支,然後嘗試重新應用這些修改,git stash apply命令並不會刪除stash列表的記錄,要刪除,可用如下命令:
    # 從棧上刪除儲藏
    git stash drop stash@{2}
    # 應用後立即刪除記錄
    git stash pop
    

git統計兩個commit之間的文件變動數

git diff master developer --shortstat # 還有類似的參數:stat, numstat.
# 類似如下輸出
# 93 files changed, 3169 insertions(+), 1969 deletions(-)

術語

  • WIP:work in progress, do not merge yet 開發中
  • LGTM:looks good to me Review完別人的PR,沒有問題
  • PTAL:please take a look 幫我看下,一般都是請別人review自己的PR
  • ACK — acknowledgement, i.e. agreed/accepted change
  • NACK/NAKnegative acknowledgement, i.e. disagree with change and/or concept
  • RFC — request for comments, i.e. I think this is a good idea, lets discuss
  • AFAIK/AFAICT — as far as I know / can tell
  • IIRC — if I recall correctly
  • IANAL — “I am not a lawyer”, but I smell licensing issues
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章