七分鐘,用事例帶你看完工作中常用的 Git 命令
從本質(zhì)上講,Git可以記錄文本的變化,但其定義是一個版本控制系統(tǒng)。你有可能已經(jīng)以這種或那種方式使用了git:由于它的分布式性質(zhì),它是代碼版本控制的事實標(biāo)準(zhǔn),與集中式的Apache Subversion(SVN)相對。
安裝 git
要檢查是否安裝了Git,在終端運(yùn)行:
- $ git version
- git version 2.27.0.rc1.windows.1
如果沒有安裝,請按照 https://git-scm.com/downloads 上的說明。Mac用戶可以用brew來安裝它:brew install git。
配置 git
我們只需要配置一些東西
- git config --global user.name "前端小智" && # 你的名字
- git config --global user.email johndoe@example.com && # 你的郵箱
- git config --global init.defaultbranch main # 默認(rèn)分支名稱,與GitHub兼容
可以用下面命令查看當(dāng)前的全局配置
- git config --global --list
- # Type ":q" to close
git在純文本中存儲配置,如果你想直接修改,可以直接在~/.gitconfig或~/.config/git/config中編輯全局配置。
正如命令所建議的那樣,去掉--global會使這些命令的適用范圍擴(kuò)大到當(dāng)前文件夾。但要測試這一點(diǎn),我們需要一個存儲庫。
創(chuàng)建新存儲庫
存儲庫只是一個文件夾,里面有我們想跟蹤的所有東西。通過命令創(chuàng)建:
- mkdir gitexample &&
- cd gitexample &&
- git init
- # gitexample git:(main)
這個命令在gitexample文件夾內(nèi)創(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)
根據(jù)提示建議,我們添加文件:
- git addd .
如果我們不想要所有文件添加可以使用。
- git add hello.txt
如果你現(xiàn)在檢查版本庫的狀態(tài),你會看到文件已經(jīng)被添加了(又稱staged),但還沒有提交。
- 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
檢查提交記錄:
- git log
- # Author: qq449245884 <44924566884@qq.com>
- # Date: Sat Jul 17 14:57:24 2021 +0800
- #
- # Add hello.txt
- #
創(chuàng)建分支
在很多情況下,擁有一個獨(dú)立的初始代碼版本是很有用的:例如,在測試你不確定的功能時,或者在一起工作時避免代碼沖突。這正是git分支的意義所在:它從歷史上的一個特定點(diǎn)開始生長。
要創(chuàng)建分支,運(yùn)行g(shù)it branch NAME,要切換分支,運(yùn)行g(shù)it checkout NAME?;蛘吆唵蔚兀?/p>
- git checkout -b dev # 切換到一個名為“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.
因為文件在同一個地方被修改了兩次,我們就產(chǎn)生了沖突??纯催@個文件:
- cat hello.txt
- <<<<<<< HEAD
- Hello, Git
- Hi from Main Branch
- =======
- Hello, Git
- >>>>>>> dev
還有一個命令可以單獨(dú)查看更改:
- git diff --ours # :q to close
- git diff --theirs #:q to close
你可以手動編輯文件并提交修改,但我們設(shè)想一下,我們只想要其中一個版本。我們就從中止合并開始。
- git merge --abort
并以 "theirs"策略重新啟動合并,這意味著在發(fā)生沖突時,我們將使用傳入的分支所堅持的東西。
- 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
最后,刪除分支運(yùn)行:
- git branch -d dev
- # Deleted branch dev (was 6259828).
重置分支
分支從 git 歷史中的某一點(diǎn)開始 "生長(grow)",rebase 允許改變這個點(diǎn)。我們再創(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"
重置我們在main到story分支所做的更改:
- git checkout story &&
- git rebase main
- # Successfully rebased and updated refs/heads/story.
可以看到在主分支創(chuàng)建的新文件被添加到story 分支。
- ls
- # changes.txt hello.txt story.txt
注意:不要rebase 別人可能使用過的分支,例如主分支。另外,請記住,在遠(yuǎn)程版本庫上進(jì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上編輯一些東西:只要點(diǎn)擊任何文件和鉛筆圖標(biāo)。添加一行你想要的任何文字,然后按 "提交修改"。
在本地運(yùn)行這個命令,以獲得遠(yuǎn)程的變化。
- git checkout main &&
- git pull
管理未提交的更改
如果你想保存你的本地修改以便以后使用,你可以使用git stash。
- echo "Changes" >> hello.txt &&
- git stash
現(xiàn)在你可以使用以下命令來檢查、應(yīng)用或放棄這些變化。
- git stash list
- # stash@{0}: WIP on main: 92354c8 Update changes.txt
- git stash pop # 應(yīng)用更改
- git stash drop # 撤銷修改
你可以使用 stash 編號,即git stash pop 0來應(yīng)用一個特定的儲藏庫,或者git stash drop 0來撤銷。
如果你想放棄所有的本地修改,只需恢復(fù)版本庫到最后提交的修改,請運(yùn)行。
- git restore .
管理提交的更改
一旦你創(chuàng)建了一個提交,這個變化就會保存在本地的git歷史中。如前所述,所有影響遠(yuǎn)程歷史的修改都需要git push --force。以下所有命令都要記住這一點(diǎn)。
我們從編輯最后的提交信息開始。
- git commit --amend # type :wq to save and close
- # Press "i" to edit, "Esc" to stop editing
我們把一切重設(shè)到最開始怎么樣?
要找到第一次提交的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)在運(yùn)行這個來重置版本庫,但保持所有的修改不被緩存。
- git reset --soft COMMIT # e.g. a07ee27
與之相反,你也可以進(jìn)行硬重置,用git reset --hard COMMIT來刪除所有修改。還有幾種其他的重置方式,你可以從git文檔中了解到。
別名
大多數(shù)時候,你只需要使用少數(shù)幾個命令(主要是checkout、add、commit、pull、push和merge),但有些命令可能是你想要“以防萬一”的。
存儲這些信息的一種方法是git aliases。要配置一個別名,只需在配置中設(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命令。
作者:Valeria 譯者:前端小智 來源:dev 原文:https://dev.to/valeriavg/master-git-in-7-minutes-gai