我的github其實很早就辦了,只是因為當時沒有灌Ubuntu,又聽說在windows上git不是那麼的好用,所以拖到大概一個月前Ubuntu灌完後才開始使用。
最近終於有時間把自己做的筆記放上來。
安裝git 和 git與github之間的基本操作
首先,在sudo apt-get install git後,必需要設定github的username和email。
git config --global user.name "***" git config --global user.email "***@***"
設定完這個後,在提交code的時候,這些訊息就會自動加到log裡面來分辯到底是誰提交的code。完成之後,在想要成為local repository的資料夾輸入 git init即可。
再來,輸入
git remote add origin https://github.com/$$/*****.git $$是github帳號 *****是github repository的名稱。
順帶一提,在local repository下,輸入git remote,可以看到local repository所設定的名稱,一般來說是origin。而git remote -v 則可以看到URL,如果很多項的話,代表可以從別人的repository獲得更新。再來在repository下放入想要上傳的檔案,譬如說是filename好了。
那接下來我們必須要add,commit,最後再push,完成整個上傳的動作。
git add filename git add .
前者代表add filename,而後者代表整個repo都被add。這邊的add指的是追蹤並暫存這個檔案。
git commit -m 'commit words' commit words
代表這次commit要下的備註。
git push origin master
輸入github的帳號和密碼,即可完成上傳。
這種上傳方法是用HTTPS的方法,還有另外一種方法是SSH,改天再來試試看。
問題接著就發生了,當我push上去後,就產生下列error message。
這是因為我github上面創一個repo的時候,一個readme檔也同時被我產生,但local repository並不知道有這個東西,所以出了問題。
! [rejected] master -> master (fetch first) error: failed to push some refs to 'https://github.com/i314i/*****.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把它和local這邊做整合。
但又遇到問題了,error message如下:
! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/i314i/*****.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
輸入git pull origin master就解決了。
而會發生以上問題的原因,是因為有local branch 和 origin branch的問題。
local branch: --- Commit C / / / origin/branch: Commit A ------ Commit B ---- Commit D pull origin master之後: local branch: --- Commit C -- Commit E / / / / / / origin/branch: Commit A ------ Commit B ---- Commit D
這麼做,remote 和 local branch才會整會再一起。
得要這麼做的原因是為git為了怕可能有別人己經更改code了,所以在push前,要習慣先pull一下,把狀態更改到最新的狀態,防止conflict。
git指令alias
將一些git的命令alias起來,方便以後加快速率,以下是我alias起來的。
git config --global alias.ci commit git config --global alias.st status git config --global alias.last 'log -1 HEAD' 查看上一次的提交訊息 git config --global alias.unstage 'reset HEAD --'
以下這些資訊都存在~/.gitconfig裡,所以其實也可以直接去這邊進行修改。
另外,git也可以加入版本號碼,這方面的資訊可以搜尋 git tag。
常用git command
以下這些是我整理的一些我目前有用到,而且不錯用的git command
git push [remote][branch] 把檔案push到remote repository的branch,一般來說都是 git push origin master git config --list 查看自己的設定值。 git status 查看目前這個repo的檔案狀態。 git config --global core.editor [***] 指定編輯器為*** git rm -f [filename] 移除filename的暫存,並把它刪除 git rm --cached [filename] 移除filename的暫存,成為未追蹤,但不刪除。 git mv a b = mv a b + git rm a + git add b git log 看以前的log git checkout [filename] 復原己修改的filename git commit --amend 更動最後一次提交 git reset head [filename] 取消被暫存的filename git remote show [remote] 查看remote repository的狀況 git config --global alias.[short] [command] 將command alias 為short git branch [branchname] 新增一個為branchname 的branch git checkout [branchname] 將HEAD移到branchname的branch git branch -d [branchname] 刪除branchname
參考資料:
https://git-scm.com/book/zh-tw/v1
關於我:
我是沒一村,專長和興趣是程式、主動投資、科技商業模式。可以參考我的書單和比較熱門的文章: