Linux學習篇(六):學習 git
阿新 • • 發佈:2020-10-05
逛 github 時面對git 、make 無從下手?本文先來介紹 git 。
git 用於 文字檔案的 版本管理,git 作者 與 Linux 核心作者是同一個人——林納斯。
初次學習 git 搭配 BeyondCompare 會更香哦。
安裝git:
<<huaecase@Huaecase ~/gitwork >>$ sudo apt install git
建立工作區( 建議在一個 空資料夾內):
<<huaecase@Huaecase ~/gitwork >>$git init
產生了隱藏檔案:
<<huaecase@Huaecase ~/gitwork >>$ls -a .git
新建文字檔案 huae:
<<huaecase@Huaecase ~/gitwork >>$ vim huae
新增內容到huae:
echo "first" > huae # '>' 和 '>>' 代表輸出重定向 '>' 為替換模式 而 '>>' 為追加。
將huae提交到git 快取區:
<<huaecase@Huaecase ~/gitwork >>$ git add huae
將huae提交到 git 倉庫:
<<huaecase@Huaecase ~/gitwork >>$ git commit huae -m "first commit" *** Please tell me who you are. Run git config --global user.email "[email protected]" git config --global user.name "Your Name" to set your account's default identity. Omit --global to set the identity only in this repository. fatal: empty ident name (for <[email protected]>) not allowed
首次使用要設定繫結的郵箱及賬戶名,解決後再次 commit。
<<huaecase@Huaecase ~/gitwork >>$ git config --global user.email "[email protected]" <<huaecase@Huaecase ~/gitwork >>$ git config --global user.name "Huae" <<huaecase@Huaecase ~/gitwork >>$ git commit huae -m "first commit" [master (root-commit) 6f44e7b] first commit 1 file changed, 1 insertion(+) create mode 100644 huae
git status 檢視狀態:
<<huaecase@Huaecase ~/gitwork >>$ git status
On branch master
nothing to commit, working tree clean
git log 檢視日誌:
<<huaecase@Huaecase ~/gitwork >>$ git log commit 6f44e7bb3e1f590d3c9ce5ea389becb41a29ec66 (HEAD -> master) Author: Huae <[email protected]> Date: Mon Oct 5 17:35:24 2020 +0800 first commit
git log 是站在 當前 版本節點 看日誌,版本切換後git log 內容 也會跟著更改。 而 git reflog 則是 站在 上帝視角看日誌。
將huae 中的 內容更改為 second並提交:
<<huaecase@Huaecase ~/gitwork >>$ echo "second" > huae
<<huaecase@Huaecase ~/gitwork >>$ git add huae <<huaecase@Huaecase ~/gitwork >>$ git commit huae -m "second commit"
此時git log 和 git reflog:
<<huaecase@Huaecase ~/gitwork >>$ git log commit cd64c55d4fedeffdea73379bd36b9c472430d2b7 (HEAD -> master) Author: Huae <[email protected]> Date: Mon Oct 5 17:45:21 2020 +0800 second commit commit 6f44e7bb3e1f590d3c9ce5ea389becb41a29ec66 Author: Huae <[email protected]> Date: Mon Oct 5 17:35:24 2020 +0800 first commit <<huaecase@Huaecase ~/gitwork >>$ git reflog cd64c55 (HEAD -> master) HEAD@{0}: commit: second commit 6f44e7b HEAD@{1}: commit (initial): first commit
回退到上一版本:
<<huaecase@Huaecase ~/gitwork >>$git reset --hard 6f44 #6f44 是第一次提交後HEAD指向的 hard 的前一部分
<<huaecase@Huaecase ~/gitwork >>$git reset --hard HEAD^ # HEAD 相當於 指標, ^ 表示前一版本
此時的 git log 和 git reflog:
<<huaecase@Huaecase ~/gitwork >>$ git log commit 6f44e7bb3e1f590d3c9ce5ea389becb41a29ec66 (HEAD -> master) Author: Huae <[email protected]> Date: Mon Oct 5 17:35:24 2020 +0800 first commit <<huaecase@Huaecase ~/gitwork >>$ git reflog 6f44e7b (HEAD -> master) HEAD@{0}: reset: moving to 6f44 cd64c55 HEAD@{1}: commit: second commit 6f44e7b (HEAD -> master) HEAD@{2}: commit (initial): first commit <<huaecase@Huaecase ~/gitwork >>$