就這么簡(jiǎn)單!10 分鐘入門 Git
雖然Git從本質(zhì)上講是監(jiān)控和跟蹤文本的更改,但它的定位依然是版本控制系統(tǒng)。你可能已經(jīng)以某種方式使用過(guò)git;由于其分布式特性,git成為了事實(shí)上的代碼版本控制標(biāo)準(zhǔn),與集中式Apache Subversion (SVN)截然相反。
安裝Git
要檢查是否在終端中安裝了Git,請(qǐng)運(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
你可以通過(guò)以下方式查看當(dāng)前的全局配置:
- git config --global --list
- # Type ":q" to close
git以純文本形式存儲(chǔ)配置,當(dāng)然你也可以直接在~/.gitconfig或~/.config/git/config中編輯全局配置。
正如命令所建議的那樣,刪除--global將使這些命令的范圍限定為當(dāng)前文件夾。為了測(cè)試這一點(diǎn),我們需要一個(gè)存儲(chǔ)庫(kù)。
創(chuàng)建新的存儲(chǔ)倉(cāng)庫(kù)
存儲(chǔ)倉(cāng)庫(kù)就是一個(gè)包含要跟蹤的所有內(nèi)容的文件夾。創(chuàng)建存儲(chǔ)倉(cāng)庫(kù)請(qǐng)運(yùn)行:
- mkdir gitexample &&
- cd gitexample &&
- git init
- # gitexample git:(main)
此命令在gitexample文件夾中創(chuàng)建了文件夾.git。隱藏的.git文件夾是一個(gè)存儲(chǔ)倉(cāng)庫(kù):所有本地配置和更改都存儲(chǔ)在那里。
做一些變更
讓我們?cè)诖鎯?chǔ)庫(kù)中創(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)
接下來(lái)讓我們來(lái)添加文件,可以直接這樣做:
- git add . # Or `git add hello.txt`, if we don't want all files
如果你現(xiàn)在檢查存儲(chǔ)庫(kù)狀態(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>是一個(gè)簡(jiǎn)寫命令,你也可以使用git commit打開(kāi)編輯器(主要是vim)并提供詳細(xì)的提交描述。
讓我們來(lái)檢查更改記錄:
- 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ú)版本的初始代碼會(huì)很有用:例如在測(cè)試不確定的功能時(shí),也可以在協(xié)同工作時(shí)避免代碼沖突。這就需要git分支登場(chǎng)了:它是從歷史記錄的某個(gè)特定點(diǎn)發(fā)展開(kāi)來(lái)的。
要?jiǎng)?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
如你所見(jiàn),文件內(nèi)容仍與原來(lá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
讓我們也對(duì)主分支進(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.
因?yàn)槲募谕粋€(gè)地方被更改了兩次,所以有了沖突。看文件:
- cat hello.txt
- <<<<<<< HEAD
- Hello, Git
- Hi from Main Branch
- =======
- Hello, Git
- >>>>>>> dev
還有一個(gè)工具可以用來(lái)分別查看更改:
- git diff --ours # :q to close
- git diff --theirs #:q to close
你可以手動(dòng)編輯文件并提交更改,但這里假設(shè)我們只需要其中一個(gè)版本。我們首先中止合并:
- git merge --abort
并使用theirs策略重新開(kāi)始合并,這意味著在發(fā)生沖突時(shí),我們將始終使用傳入分支的內(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策略。將兩個(gè)更改合并在一起需要手動(dòng)編輯(或使用git mergetool)。
要查看所有分支的列表,請(qǐng)運(yùn)行:
- git branch # type :q to close
- # dev
- # * main
最后,是如何刪除分支:
- git branch -d dev
- # Deleted branch dev (was 6259828).
Rebase命令
分支從git歷史記錄中的特定點(diǎn)開(kāi)始“生長(zhǎng)”,rebase命令允許更改這些特定點(diǎn)。讓我們創(chuàng)建另一個(gè)分支并再次對(duì)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)我們?cè)趍ain中對(duì)story分支所做的更改,請(qǐng)運(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其他人可能使用過(guò)的分支,例如主分支。此外,請(qǐng)記住,遠(yuǎn)程存儲(chǔ)庫(kù)上的每個(gè)歷史操作都需要強(qiáng)制這些更改生效。
遠(yuǎn)程存儲(chǔ)倉(cāng)庫(kù)
如果你還沒(méi)有存儲(chǔ)庫(kù),那么請(qǐng)創(chuàng)建一個(gè)GitHub帳戶,登錄并創(chuàng)建一個(gè)新的空存儲(chǔ)庫(kù)(私有或公共)。
假設(shè)存儲(chǔ)庫(kù)名稱是example,運(yùn)行以下命令(用你自己的用戶名替換):
- git remote add origin git@github.com:USERNAME/example.git &&
- git push -u origin main
你可以刷新頁(yè)面并查看主分支中的文件。要將所有本地分支推送到遠(yuǎn)程存儲(chǔ)庫(kù),請(qǐng)運(yùn)行:
- git push --all origin
讓我們?cè)贕itHub上編輯一些內(nèi)容:?jiǎn)螕羧我馕募蚿encil圖標(biāo)。你想要什么文本都可以通過(guò)一行代碼添加進(jìn)去,然后按Commit changes。
現(xiàn)在在本地運(yùn)行此命令以獲取遠(yuǎn)程更改:
- git checkout main &&
- git pull
管理未提交的更改
如果要保存本地更改以供以后使用,可以使用git stash:
- echo "Changes" >> hello.txt &&
- git stash
然后你可以使用以下命令來(lái)檢查、應(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編號(hào),即git stash pop 0來(lái)應(yīng)用特定的stash或通過(guò)git stash drop 0來(lái)刪除它。
如果你想放棄所有本地更改并簡(jiǎn)單地將存儲(chǔ)庫(kù)恢復(fù)到上次提交的更改,運(yùn)行:
- git restore .
管理已提交的更改
創(chuàng)建提交后,此更改將保存在本地git歷史記錄中。如前所述,所有影響遠(yuǎn)程歷史的更改都需要git push --force。以下所有命令都值得一記。
讓我們從編輯最后一條提交消息開(kāi)始:
- git commit --amend # type :wq to save and close
- # Press "i" to edit, "Esc" to stop editing
我們把一切都重置到一開(kāi)始怎么樣?
要查找最開(kāi)始提交的ID,請(qǐng)運(yùn)行此命令并滾動(dòng)(向下箭頭)到最后:
- 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)在來(lái)重置存儲(chǔ)庫(kù),但保持所有更改未暫存:
- git reset --soft COMMIT # e.g. a07ee27
與此相反,你也可以使用git reset --hard COMMIT進(jìn)行強(qiáng)制重置并擺脫所有更改。你可以從git文檔中了解其他幾種類型的重置。
別名
大多數(shù)情況下,你將只用到少量命令(大多數(shù)情況下為checkout、add、commit、pull、push 和merge),但多了解一些總是有備無(wú)患。
另一種方法是git別名。要配置別名,只需在配置中設(shè)置即可。例如,我經(jīng)常使用的一個(gè)別名是git tree,它以樹(shù)的形式打印漂亮的歷史日志:
- git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit'
- # Try it with `git tree`
另一個(gè)有用的別名是刪除所有合并的分支:
- git config --global alias.clbr '!git branch --merged | grep -v \* | xargs git branch -D'
如你所見(jiàn),它使用!作為前綴,這允許我們使用其他命令,而不僅僅是git命令。
今天就到這里,希望對(duì)你的開(kāi)發(fā)之旅有所幫助。