就這么簡單!10 分鐘入門 Git
雖然Git從本質(zhì)上講是監(jiān)控和跟蹤文本的更改,但它的定位依然是版本控制系統(tǒng)。你可能已經(jīng)以某種方式使用過git;由于其分布式特性,git成為了事實上的代碼版本控制標(biāo)準(zhǔn),與集中式Apache Subversion (SVN)截然相反。
安裝Git
要檢查是否在終端中安裝了Git,請運(yùn)行:
- git version
- # git version 2.25.1
Ubuntu用戶可以使用apt安裝:sudo apt install git。
配置Git
我們需要配置的東西不多:
- git config --global user.name "John Doe" && # your name
- git config --global user.email johndoe@example.com && # your email
- git config --global init.defaultbranch main # default branch name, to be compatible with GitHub
你可以通過以下方式查看當(dāng)前的全局配置:
- git config --global --list
- # Type ":q" to close
git以純文本形式存儲配置,當(dāng)然你也可以直接在~/.gitconfig或~/.config/git/config中編輯全局配置。
正如命令所建議的那樣,刪除--global將使這些命令的范圍限定為當(dāng)前文件夾。為了測試這一點,我們需要一個存儲庫。
創(chuàng)建新的存儲倉庫
存儲倉庫就是一個包含要跟蹤的所有內(nèi)容的文件夾。創(chuàng)建存儲倉庫請運(yùn)行:
- mkdir gitexample &&
- cd gitexample &&
- git init
- # gitexample git:(main)
此命令在gitexample文件夾中創(chuàng)建了文件夾.git。隱藏的.git文件夾是一個存儲倉庫:所有本地配置和更改都存儲在那里。
做一些變更
讓我們在存儲庫中創(chuàng)建一些東西:
- echo "Hello, Git" >> hello.txt
如果我們運(yùn)行g(shù)it status,我們將看到新創(chuàng)建的未被跟蹤的文件:
- git status
- # On branch main
- #
- # No commits yet
- #
- # Untracked files:
- # (use "git add <file>..." to include in what will be committed)
- # hello.txt
- #
- # nothing added to commit but untracked files present (use "git add" to track)
接下來讓我們來添加文件,可以直接這樣做:
- git add . # Or `git add hello.txt`, if we don't want all files
如果你現(xiàn)在檢查存儲庫狀態(tài),你將看到文件已添加(也稱為已暫存),但尚未提交:
- git status
- # On branch main
- #
- # No commits yet
- #
- # Changes to be committed:
- # (use "git rm --cached <file>..." to unstage)
- # new file: hello.txt
要記錄更改,先提交:
- git commit -m "Add hello.txt"
- # [main (root-commit) a07ee27] Adds hello.txt
- # 1 file changed, 2 insertions(+)
- # create mode 100644 hello.txt
小提示:git commit -m <MESSAGE>是一個簡寫命令,你也可以使用git commit打開編輯器(主要是vim)并提供詳細(xì)的提交描述。
讓我們來檢查更改記錄:
- git log
- # type :q to close
將顯示如下內(nèi)容:
- commit a07ee270d6bd0419a50d1936ad89b9de0332f375 (HEAD -> main)
- Author: Your Name <your@email.address>
- Date: Sun Jul 11 11:47:16 2021 +0200
- Adds hello.txt
- (END)
創(chuàng)建分支
在很多情況下,擁有單獨(dú)版本的初始代碼會很有用:例如在測試不確定的功能時,也可以在協(xié)同工作時避免代碼沖突。這就需要git分支登場了:它是從歷史記錄的某個特定點發(fā)展開來的。
要創(chuàng)建分支可以運(yùn)行g(shù)it branch NAME,切換分支可以運(yùn)行g(shù)it checkout NAME。或者干脆就這樣做:
- git checkout -b dev # switches to a new branch called "dev"
- # Switched to a new branch 'dev'
- # gitexample git:(dev)
讓我們更改hello.txt文件中的內(nèi)容并提交更改:
- echo "\nHello, Git Branch" >> hello.txt &&
- git commit -am "Change hello.txt"
現(xiàn)在讓我們切換回主版本:
- git checkout main &&
- cat hello.txt
- # Switched to branch 'main'
- # Hello, Git
如你所見,文件內(nèi)容仍與原來相同。要比較分支,我們可以運(yùn)行:
- git diff dev
- # diff --git a/hello.txt b/hello.txt
- # index 360c923..b7aec52 100644
- # --- a/hello.txt
- # +++ b/hello.txt
- # @@ -1,3 +1 @@
- # Hello, Git
- # -
- # -Hello, Git Branch
- # (END)
- # type ":q" to close
讓我們也對主分支進(jìn)行更改:
- echo "\nHi from Main Branch" >> hello.txt &&
- git commit -am "Change hello.txt from main"
- # [main 9b60c4b] Change hello.txt from main
- # 1 file changed, 2 insertions(+)
現(xiàn)在讓我們嘗試合并更改:
- git merge dev
- # Auto-merging hello.txt
- # CONFLICT (content): Merge conflict in hello.txt
- # Automatic merge failed; fix conflicts and then commit the result.
因為文件在同一個地方被更改了兩次,所以有了沖突。看文件:
- cat hello.txt
- <<<<<<< HEAD
- Hello, Git
- Hi from Main Branch
- =======
- Hello, Git
- >>>>>>> dev
還有一個工具可以用來分別查看更改:
- git diff --ours # :q to close
- git diff --theirs #:q to close
你可以手動編輯文件并提交更改,但這里假設(shè)我們只需要其中一個版本。我們首先中止合并:
- git merge --abort
并使用theirs策略重新開始合并,這意味著在發(fā)生沖突時,我們將始終使用傳入分支的內(nèi)容:
- git merge -X theirs dev
- # Auto-merging hello.txt
- # Merge made by the 'recursive' strategy.
- # hello.txt | 5 +----
- # 1 file changed, 1 insertion(+), 4 deletions(-)
與此策略相反的是ours策略。將兩個更改合并在一起需要手動編輯(或使用git mergetool)。
要查看所有分支的列表,請運(yùn)行:
- git branch # type :q to close
- # dev
- # * main
最后,是如何刪除分支:
- git branch -d dev
- # Deleted branch dev (was 6259828).
Rebase命令
分支從git歷史記錄中的特定點開始“生長”,rebase命令允許更改這些特定點。讓我們創(chuàng)建另一個分支并再次對hello.txt添加一些更改:
- git checkout -b story &&
- echo "Once upon a time there was a file">>story.txt &&
- git add story.txt &&
- git commit -m "Add story.txt"
- # Switched to a new branch 'story'
- # [story eb996b8] Add story.txt
- # 1 file changed, 1 insertion(+)
- # create mode 100644 story.txt
現(xiàn)在,讓我們回到主分支并添加更改:
- git checkout main &&
- echo "Other changes" >> changes.txt &&
- git add changes.txt &&
- git commit -m "Add changes.txt"
要重現(xiàn)我們在main中對story分支所做的更改,請運(yùn)行:
- git checkout story &&
- git rebase main
- # Successfully rebased and updated refs/heads/story.
你可以看到在main分支中創(chuàng)建的新文件被添加到story分支:
- ls
- # changes.txt hello.txt story.txt
注意:不要重新rebase其他人可能使用過的分支,例如主分支。此外,請記住,遠(yuǎn)程存儲庫上的每個歷史操作都需要強(qiáng)制這些更改生效。
遠(yuǎn)程存儲倉庫
如果你還沒有存儲庫,那么請創(chuàng)建一個GitHub帳戶,登錄并創(chuàng)建一個新的空存儲庫(私有或公共)。
假設(shè)存儲庫名稱是example,運(yùn)行以下命令(用你自己的用戶名替換):
- git remote add origin git@github.com:USERNAME/example.git &&
- git push -u origin main
你可以刷新頁面并查看主分支中的文件。要將所有本地分支推送到遠(yuǎn)程存儲庫,請運(yùn)行:
- git push --all origin
讓我們在GitHub上編輯一些內(nèi)容:單擊任意文件和pencil圖標(biāo)。你想要什么文本都可以通過一行代碼添加進(jìn)去,然后按Commit changes。
現(xiàn)在在本地運(yùn)行此命令以獲取遠(yuǎn)程更改:
- git checkout main &&
- git pull
管理未提交的更改
如果要保存本地更改以供以后使用,可以使用git stash:
- echo "Changes" >> hello.txt &&
- git stash
然后你可以使用以下命令來檢查、應(yīng)用或放棄這些更改:
- git stash list
- # stash@{0}: WIP on main: 92354c8 Update changes.txt
- git stash pop # to apply changes
- git stash drop # to drop changes
提示:你可以使用stash編號,即git stash pop 0來應(yīng)用特定的stash或通過git stash drop 0來刪除它。
如果你想放棄所有本地更改并簡單地將存儲庫恢復(fù)到上次提交的更改,運(yùn)行:
- git restore .
管理已提交的更改
創(chuàng)建提交后,此更改將保存在本地git歷史記錄中。如前所述,所有影響遠(yuǎn)程歷史的更改都需要git push --force。以下所有命令都值得一記。
讓我們從編輯最后一條提交消息開始:
- git commit --amend # type :wq to save and close
- # Press "i" to edit, "Esc" to stop editing
我們把一切都重置到一開始怎么樣?
要查找最開始提交的ID,請運(yùn)行此命令并滾動(向下箭頭)到最后:
- git log --abbrev-commit
- # commit a07ee27
- # Author: Your Name <your@email.address>
- Date: Sun Jul 11 11:47:16 2021 +0200
- Adds hello.txt
- (END)
- # type ":q" to close
現(xiàn)在來重置存儲庫,但保持所有更改未暫存:
- git reset --soft COMMIT # e.g. a07ee27
與此相反,你也可以使用git reset --hard COMMIT進(jìn)行強(qiáng)制重置并擺脫所有更改。你可以從git文檔中了解其他幾種類型的重置。
別名
大多數(shù)情況下,你將只用到少量命令(大多數(shù)情況下為checkout、add、commit、pull、push 和merge),但多了解一些總是有備無患。
另一種方法是git別名。要配置別名,只需在配置中設(shè)置即可。例如,我經(jīng)常使用的一個別名是git tree,它以樹的形式打印漂亮的歷史日志:
- git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit'
- # Try it with `git tree`
另一個有用的別名是刪除所有合并的分支:
- git config --global alias.clbr '!git branch --merged | grep -v \* | xargs git branch -D'
如你所見,它使用!作為前綴,這允許我們使用其他命令,而不僅僅是git命令。
今天就到這里,希望對你的開發(fā)之旅有所幫助。