好久沒更了.....
之前一直沒想到要寫些什麼,下班又精神不濟,藉口一堆,哈
不過之前有小玩一下
AWTRIX, 買了整組材料,打印殼,焊接,最後雖然是完成了,不過我挑的3D列印品質很差,公差太大導致組不起來,真的要組的話需要用黏的,所以就不獻醜了xD
先說一下Pro Git這本書,非常推薦完全沒有經驗的人來看,只要看完前幾章,在一般情況下就可以應付日常使用,電子書可以從這裡
下載。
在看這本書之前,我已經使用git一段時間,基本指令都算熟悉,只不過我原本學習的方式是透過網路東查西查,總覺得學習的沒有系統,所以想說來看一下書裡是怎麼寫的,複習順便看看有什麼指令是沒學到的。
這一篇我想記錄一下前三章的心得,把基本指令記一下,以後忘記可以來看。
Getting Started
這章在介紹版本控管的歷史,沒sense的話可以看一下,不然就可以直接看What is Git? 了解Git架構,
Three States 的概念很基本很重要,一定要記得。最後就教怎麼安裝跟設定,
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git config --list
git config --global core.editor vim
git help <verb>
git add -h
Git Basics
git init
git add *.c
git commit -m "first commit"
Clone with rename directory
git clone https://github.com/libgit2/libgit2 mylibgit
git status
//Short Status
git status -s
我常用指令為
git st -uno
通常我只在意staging跟changing files
gitignore
gitignore其實有很多細節要注意,沒寫好就會造成上傳再下載時的結果不如預期,對開發SDK來說會是很嚴重的錯誤,需要好好搞懂,建議搭配下面文章一起學比較快。
https://www.cnblogs.com/kevingrace/p/5690241.html
Github上有各種gitignore的樣版,在初入某個領域時可以先拿來用,節省時間順便學習
https://github.com/github/gitignore
git diff
git diff --staged = git diff --cached
git difftool --tool-help //沒用過...
git commit -m "Story 182: Fix benchmarks for speed"
git commit with -v //put diff of the changes in the editor
git commit -a -m 'added new benchmarks' //commit all changed files
我常用指令為
git ci "XXXX"
git rm
git rm -f file //for modified or staging files
git rm --cached README //不讓Git 追蹤README了
rename a file
git mv file_from file_to
git log
git log -p
git log FILENAME
git log --stat
git log --pretty=oneline
git log --pretty=format:"%h - %an, %ar : %s"
git log --author=Gokberk
git log --grep="something"
git log --all-match --grep="sdfsfd" //grep sdfsfd from log message except author
git log -S something //grep contents of commit files
修改HEAD commit,單純下可以改commit訊息,如果有東西忘記commit,先用git add FILE,再下下面指令就可以把FILE也加到HEAD commit裡
git commit --amend
git reset HEAD
常用, 把HEAD整筆還原出來
git reset HEAD^
git checkout file
git remote -v
git remote add <shortname> <url>
git fetch <remote>
git pull = git fetch + git merage
git push <remote> <branch>
git remote show origin
git remote rename pb paul
git remote rm paul //Delete paul
tag這功能在我看書前完全沒用過...,可以對commit另取個名字、刻版本、小筆記等等功能
git tag
git tag -l
git tag -a v1.4 -m "my version 1.4"
git tag -a v1.2 9fceb02
The git push command doesn't transfer tags to remote servers, need push tag explicitly
git push origin v1.5
//push all tags
git push origin --tags
Delete
git tag -d <tagname>
Delete remote tags
git push origin :refs/tags/v1.4-lw
git push origin --delete <tagname>
alias寫的好,使用git沒煩腦xD
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.ci "commit -m"
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk' //using !to run other command
Git Branching
Some people refer to Git’s branching model as its “killer feature,” and it certainly sets Git apart in
the VCS community.
Git最重要特色之一,也是Git強大的地方,就是branch開免錢xD
git branch
git branch testing
git checkout testing
git log --oneline --decorate --graph --all
git checkout -b iss53
git checkout master
git checkout -b hotfix
git checkout master
git merge hotfix //fast-forward
git branch -d hotfix
git checkout iss53
//after developing complete...
git checkout master
git merge iss53 //three-way merge
sometimes will have merge conflicts...
git mergetool //沒用過,需要先設定
git branch -v
git branch --merged
git branch --no-merged
git ls-remote <remote> or git remote show <remote>
git clone -o booyah //set default remote name, default remote name is "origin"
git fetch <remote>
git push origin serverfix:awesomebranch //push serverfix to remote and rename as awesomebranch
設定完比較方便,才不用每次需要打帳密
git config --global credential.helper cache
git checkout -b serverfix origin/serverfix = git checkout --track origin/serverfix
git branch -u origin/serverfix //set upstream branch
upstream shorthand @{upstream} @{u}
e.g. git merge @{u}
git branch -vv //show each branch info
git pull == git fetch + git merge
git push origin --delete serverfix //remove remote branch
rebase這個指令很重要,可以讓歷史保持線性,但會修改歷史,後面有個小節專門在討論這個指令的利弊。在多人協同工作的環境下這個指令最好只在本地端使用,不然將修改過歷史的branch推送到遠端時其他人可能會殺了你xD
Do not rebase commits that exist outside your repository and that people may have based
work on.
If you follow that guideline, you’ll be fine. If you don’t, people will hate you, and you’ll be scorned
by friends and family.
Rebasing makes for a cleaner history!!
git checkout experiment
git rebase master
在rebase之後切回master,做merge就會是fast-forward
git checkout master
git merge experiment
這也是我看這本書才發現的指令,蠻少人在介紹這個指令的,我也幾乎沒有這種情境可以用到這個指令
git rebase --onto master server client
我覺得basebranch topicbranch兩個的角色很容易搞混,要多注意
Rebase 完後,<topicbranch> 會蓋在<basebranch>之上
Rebase server branch onto master branch
git rebase <basebranch> <topicbranch>
git rebase master server = git checkout server + git rebase master
留言
張貼留言