1. 程式人生 > >Git學習(GitHug安裝及其闖關攻略)

Git學習(GitHug安裝及其闖關攻略)

學習知識點:

學習git的遊戲好幫手-GitHug的安裝:

GitHug闖關攻略(兩篇結合起來看比較好):

小知識點總結:

1. 配置別名用以顯示不同顏色的log,特別贊,使用時,直接輸入git lg命令即可:

DavidtekiMacBook-Air:~ du$ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset-%C(yellow)%d%Creset%s%Cgreen(%cr)%C(bold blue)<%an>%Creset'--abbrev-commit"
2. git blame <fileName>,展示每行的最後一次的修改記錄。或者直接在AndroidStudio程式碼行數的區域內右鍵,選擇Annotate選項,功能與此相同。
DavidtekiMacBook-Air:GitTest du$ git blame README 
^88a80bc (duhongliu 2017-08-30 15:01:59 +0800 1) i am a teacher
^88a80bc (duhongliu 2017-08-30 15:01:59 +0800 2) you are my sunshine
3065d0dd (duhongliu 2017-08-30 15:09:19 +0800 3) hahakuankuandoctor
^88a80bc (duhongliu 2017-08-30 15:01:59 +0800 4) oo
3. git shortlog 根據提交者的名字進行分組顯示,-e顯示郵件資訊和提交者及提交資訊,-s只顯示提交數量和提交者,-n按照提交數量大小對提交者和提交資訊進行排序
DavidtekiMacBook-Air:GitTest du$ git shortlog
duhongliu (2):
      first commit
      second commit

4. gitk是和git一起安裝的,which gitk檢視gitk安裝位置,輸入gitk命令,會出現圖形化介面

DavidtekiMacBook-Air:GitTest du$ gitk


5. git reset

執行git add之後,檔案進入暫存區,使用git reset HEAD <fileName> 將檔案移出暫存區,回到工作區。


執行git add和git commit之後,檔案進入本地倉庫,使用git reset <last commit's SHA-1 hash code>,注意:reset本次提交的版本的上一個雜湊值,將檔案移回到工作區。


6. git stash 與 git pop,AndroidStudio中的Version Control中有Shelve Changes和Unshelve功能,與上述功能類似。

git stash                   //暫存當前的改動
git stash save "修改說明資訊" //暫存當前的改動
git stash list              //暫存的改動列表
git stash apply             //恢復暫存的所有改動
git stash apply [email protected]{num} //恢復暫存的某個改動,num為通過git stash list查出的編號
git stash pop               //將git stash棧中最後一個版本取出來恢復

7. 在GitHub中建立一個Repository後,將本地的專案進行上傳時,執行以下兩個命令

DavidtekiMacBook-Air:GitTest du$ git remote add origin [email protected]:Kikitious/TestGit.git
DavidtekiMacBook-Air:GitTest du$ git push -u origin master

我遇到的問題是:第二行命令輸入後,顯示failed to push some refs to '[email protected]:Kikitious/TestGit.git'

DavidtekiMacBook-Air:GitTest du$ git push -u origin master
To github.com:Kikitious/TestGit.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to '[email protected]:Kikitious/TestGit.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.
出現這個錯誤的原因是因為遠端庫中的檔案沒有下載下來(建立Repository時有README檔案)。此時應該執行git pull origin master命令:
DavidtekiMacBook-Air:GitTest du$ git pull origin master
From github.com:Kikitious/TestGit
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories
又出現了refusing to merge unrelated histories錯誤,什麼原因呢?因為他們是兩個不同的專案,要把兩個不同的專案合併,需要這樣寫git pull origin master --allow-unrelated-histories,此處參考:http://blog.csdn.net/lindexi_gd/article/details/52554159
DavidtekiMacBook-Air:GitTest du$ git pull origin master --allow-unrelated-histories
From github.com:Kikitious/TestGit
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md
這時就將遠端Repository中的程式碼pull下來了,此時再執行push命令:
DavidtekiMacBook-Air:GitTest du$ git push -u origin master
Counting objects: 23, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (23/23), 2.06 KiB | 703.00 KiB/s, done.
Total 23 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To github.com:Kikitious/TestGit.git
   bf38f7c..75c0ced  master -> master
Branch master set up to track remote branch master from origin.
大功告成。之後的對檔案的操作流程就是add--commit--push了。

8. git tag

打個本地標籤,一般在Master上打標籤。打tag時,用git tag <tagName>打在當前最新提交上 或者 git tag <tagNmae> <SHA-1 hash code>打在指定的commit id上

DavidtekiMacBook-Air:GitTest du$ git tag v0.1
DavidtekiMacBook-Air:GitTest du$ git tag v0.1 1f3cbfe0973f6790ceed8ebb5997b5b4273096c6

將標籤push到遠端倉庫上,用git push origin <tagNmae>,push所有tag到遠端分支,用git push origin --tags

DavidtekiMacBook-Air:GitTest du$ git push origin v0.1
Total 0 (delta 0), reused 0 (delta 0)
To github.com:Kikitious/TestGit.git
 * [new tag]         v0.1 -> v0.1
檢視本地標籤,用git tag
DavidtekiMacBook-Air:GitTest du$ git tag
v0.0
v0.1
v0.2
刪除本地標籤,用git tag -d <tagName>
DavidtekiMacBook-Air:GitTest du$ git tag -d v0.1
Deleted tag 'v0.1' (was bda6e27)
刪除遠端倉庫的標籤,先刪除本地標籤,再用git push origin :refs/tags/<tagName>命令將刪除的tag推送到遠端倉庫:
DavidtekiMacBook-Air:GitTest du$ git tag -d v0.1
Deleted tag 'v0.1' (was bda6e27)
DavidtekiMacBook-Air:GitTest du$ git push origin :refs/tags/v0.1
To github.com:Kikitious/TestGit.git
 - [deleted]         v0.1