自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

七分鐘,用事例帶你看完工作中常用的 Git 命令

開發(fā) 前端
從本質(zhì)上講,Git可以記錄文本的變化,但其定義是一個版本控制系統(tǒng)。你有可能已經(jīng)以這種或那種方式使用了git:由于它的分布式性質(zhì),它是代碼版本控制的事實標(biāo)準(zhǔn),與集中式的Apache Subversion(SVN)相對。

[[419568]]

從本質(zhì)上講,Git可以記錄文本的變化,但其定義是一個版本控制系統(tǒng)。你有可能已經(jīng)以這種或那種方式使用了git:由于它的分布式性質(zhì),它是代碼版本控制的事實標(biāo)準(zhǔn),與集中式的Apache Subversion(SVN)相對。

安裝 git

要檢查是否安裝了Git,在終端運(yùn)行:

  1. $ git version 
  2. git version 2.27.0.rc1.windows.1 

如果沒有安裝,請按照 https://git-scm.com/downloads 上的說明。Mac用戶可以用brew來安裝它:brew install git。

配置 git

我們只需要配置一些東西

  1. git config --global user.name "前端小智" && # 你的名字 
  2. git config --global user.email johndoe@example.com && # 你的郵箱 
  3. git config --global init.defaultbranch main # 默認(rèn)分支名稱,與GitHub兼容 

可以用下面命令查看當(dāng)前的全局配置

  1. git config --global --list 
  2. # Type ":q" to close 

git在純文本中存儲配置,如果你想直接修改,可以直接在~/.gitconfig或~/.config/git/config中編輯全局配置。

正如命令所建議的那樣,去掉--global會使這些命令的適用范圍擴(kuò)大到當(dāng)前文件夾。但要測試這一點(diǎn),我們需要一個存儲庫。

創(chuàng)建新存儲庫

存儲庫只是一個文件夾,里面有我們想跟蹤的所有東西。通過命令創(chuàng)建:

  1. mkdir gitexample &&  
  2. cd gitexample &&  
  3. git init 
  4. # gitexample git:(main) 

這個命令在gitexample文件夾內(nèi)創(chuàng)建了一個.git文件夾。這個隱藏的.git文件夾就是版本庫:所有的本地配置和修改都存儲在這里。

改變

在存儲庫中創(chuàng)建一些東西:

  1. echo "Hello, Git " >> hello.txt 

運(yùn)行g(shù)it status,我們會看到新創(chuàng)建的未被追蹤的文件。

  1. git status 
  2. On branch main 
  3. #  
  4. No commits yet 
  5. #  
  6. # Untracked files: 
  7. #  (use "git add <file>..." to include in what will be committed
  8. #   hello.txt 
  9. # nothing added to commit but untracked files present (use "git add" to track) 

根據(jù)提示建議,我們添加文件:

  1. git addd .  

如果我們不想要所有文件添加可以使用。

  1. git add hello.txt  

如果你現(xiàn)在檢查版本庫的狀態(tài),你會看到文件已經(jīng)被添加了(又稱staged),但還沒有提交。

  1. git status 
  2. On branch main 
  3. #  
  4. No commits yet 
  5. #  
  6. # Changes to be committed
  7. #  (use "git rm --cached <file>..." to unstage) 
  8. #   new file:   hello.txt 

為了記錄這些變化,我們來提交它。

  1. git commit -m "Add hello.txt" 
  2. # [main (root-commit) a07ee27] Adds hello.txt 
  3. # 1 file changed, 2 insertions(+) 
  4. create mode 100644 hello.txt 

git commit -m 是一個簡短的命令,你可以用git commit打開編輯器(主要是vim),提供詳細(xì)的提交描述。

檢查提交記錄:

  1. git log 
  2.  
  3. # Author: qq449245884 <44924566884@qq.com> 
  4. Date:   Sat Jul 17 14:57:24 2021 +0800 
  5. #    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>

  1. git checkout -b dev # 切換到一個名為“dev”的新分支 
  2. # Switched to a new branch 'dev' 
  3. # gitexample git:(dev) 

我們在Hello.txt文件中更改一些內(nèi)容并提交更改:

  1. echo "\nHello, Git Branch" >> hello.txt && 
  2. git commit -am "Change hello.txt" 

現(xiàn)在,切換到主分支:

  1. git checkout main && 
  2. cat hello.txt 
  3. # Switched to branch 'main' 
  4. # Hello, Git 

正如你所看到的,文件內(nèi)容仍然和原來一樣。為了比較分支,我們可以運(yùn)行。

  1. git diff dev 
  2. # diff --git a/hello.txt b/hello.txt 
  3. index 360c923..b7aec52 100644 
  4. --- a/hello.txt 
  5. # +++ b/hello.txt 
  6. # @@ -1,3 +1 @@ 
  7. # Hello, Git 
  8. # - 
  9. # -Hello, Git Branch 
  10. # (END
  11. # type ":q" to close 

我們在主分支中進(jìn)行更改:

  1. echo "\nHi from Main Branch" >> hello.txt && 
  2. git commit -am "Change hello.txt from main" 
  3. # [main 9b60c4b] Change hello.txt from main 
  4. # 1 file changed, 2 insertions(+) 

現(xiàn)在讓我們試著把這些變化合并起來。

  1. git merge dev 
  2. # Auto-merging hello.txt 
  3. # CONFLICT (content): Merge conflict in hello.txt 
  4. # Automatic merge failed; fix conflicts and then commit the result. 

因為文件在同一個地方被修改了兩次,我們就產(chǎn)生了沖突??纯催@個文件:

  1. cat hello.txt 
  2. <<<<<<< HEAD 
  3. Hello, Git 
  4.  
  5. Hi from Main Branch 
  6. ======= 
  7. Hello, Git 
  8. >>>>>>> dev 

還有一個命令可以單獨(dú)查看更改:

  1. git diff --ours # :q to close  
  2. git diff --theirs #:q to close 

你可以手動編輯文件并提交修改,但我們設(shè)想一下,我們只想要其中一個版本。我們就從中止合并開始。

  1. git merge --abort 

并以 "theirs"策略重新啟動合并,這意味著在發(fā)生沖突時,我們將使用傳入的分支所堅持的東西。

  1. git merge -X theirs dev 
  2. # Auto-merging hello.txt 
  3. # Merge made by the 'recursive' strategy. 
  4. # hello.txt | 5 +---- 
  5. # 1 file changed, 1 insertion(+), 4 deletions(-) 

與此策略相反的是 "ours"。將這兩個改動合并在一起,需要手動編輯(或使用git mergetool)。

查看所有分支運(yùn)行的列表

  1. git branch # type :q to close 
  2. #  dev 
  3. # * main 

最后,刪除分支運(yùn)行:

  1. git branch -d dev 
  2. # Deleted branch dev (was 6259828). 

重置分支

分支從 git 歷史中的某一點(diǎn)開始 "生長(grow)",rebase 允許改變這個點(diǎn)。我們再創(chuàng)建一個分支,并在hello.txt上添加一些改動。

  1. git checkout -b story && 
  2. echo "Once upon a time there was a file">>story.txt && 
  3. git add story.txt && 
  4. git commit -m "Add story.txt" 
  5. # Switched to a new branch 'story' 
  6. # [story eb996b8] Add story.txt 
  7. # 1 file changed, 1 insertion(+) 
  8. create mode 100644 story.txt 

現(xiàn)在,我們回到主分支并添加更改:

  1. git checkout main && 
  2. echo "Other changes" >> changes.txt && 
  3. git add changes.txt && 
  4. git commit -m "Add changes.txt" 

重置我們在main到story分支所做的更改:

  1. git checkout story && 
  2. git rebase main 
  3. # Successfully rebased and updated refs/heads/story. 

可以看到在主分支創(chuàng)建的新文件被添加到story 分支。

  1. ls 
  2. # changes.txt hello.txt   story.txt 

注意:不要rebase 別人可能使用過的分支,例如主分支。另外,請記住,在遠(yuǎn)程版本庫上進(jìn)行的每一次歷史操作都需要強(qiáng)制這些修改生效。

遠(yuǎn)程存儲庫

如果你還沒有,請創(chuàng)建一個GitHub賬戶,登錄并創(chuàng)建一個新的空倉庫(私有或公共)。

假設(shè)版本庫的名字是 "example",運(yùn)行以下命令(改成你的用戶名)。

  1. git remote add origin git@github.com:USERNAME/example.git && 
  2. git push -u origin main 

你可以刷新頁面,看到主分支的文件。要把所有本地分支推送到遠(yuǎn)程倉庫,請運(yùn)行。

  1. git push --all origin 

我們在GitHub上編輯一些東西:只要點(diǎn)擊任何文件和鉛筆圖標(biāo)。添加一行你想要的任何文字,然后按 "提交修改"。

在本地運(yùn)行這個命令,以獲得遠(yuǎn)程的變化。

  1. git checkout main && 
  2. git pull 

管理未提交的更改

如果你想保存你的本地修改以便以后使用,你可以使用git stash。

  1. echo "Changes" >> hello.txt && 
  2. git stash 

現(xiàn)在你可以使用以下命令來檢查、應(yīng)用或放棄這些變化。

  1. git stash list 
  2. # stash@{0}: WIP on main: 92354c8 Update changes.txt 
  3. git stash pop # 應(yīng)用更改 
  4. git stash drop # 撤銷修改 

你可以使用 stash 編號,即git stash pop 0來應(yīng)用一個特定的儲藏庫,或者git stash drop 0來撤銷。

如果你想放棄所有的本地修改,只需恢復(fù)版本庫到最后提交的修改,請運(yùn)行。

  1. git restore . 

管理提交的更改

一旦你創(chuàng)建了一個提交,這個變化就會保存在本地的git歷史中。如前所述,所有影響遠(yuǎn)程歷史的修改都需要git push --force。以下所有命令都要記住這一點(diǎn)。

我們從編輯最后的提交信息開始。

  1. git commit --amend # type :wq to save and close 
  2. # Press "i" to edit, "Esc" to stop editing 

我們把一切重設(shè)到最開始怎么樣?

要找到第一次提交的ID,請運(yùn)行這個命令并滾動(向下箭頭)到最后。

  1. git log --abbrev-commit 
  2. commit a07ee27 
  3. # Author: Your Name <your@email.address> 
  4. Date:   Sun Jul 11 11:47:16 2021 +0200 
  5.  
  6.     Adds hello.txt 
  7. (END
  8. # type ":q" to close 

現(xiàn)在運(yùn)行這個來重置版本庫,但保持所有的修改不被緩存。

  1. 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,它以樹的形式打印出一個漂亮的歷史日志。

  1. git config --global alias.tree 'log --graph --decorate --pretty=oneline --abbrev-commit' 
  2. # Try it with `git tree` 

另一個有用的別名是刪除所有合并的分支。

  1. 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

 

責(zé)任編輯:姜華 來源: 大遷世界
相關(guān)推薦

2009-11-11 16:08:21

ADO.NET數(shù)據(jù)服務(wù)

2009-07-06 15:50:01

微軟Windows 7操作系統(tǒng)

2022-05-02 17:43:23

Java編程語言

2023-07-19 17:19:37

2020-11-23 16:23:59

CSS設(shè)計技術(shù)

2020-05-13 21:09:10

JavaScript前端技術(shù)

2021-08-28 11:47:52

json解析

2019-08-07 16:50:38

SQLjoingroup

2017-11-21 15:34:15

Linux 開發(fā)開源

2023-11-26 17:47:00

數(shù)據(jù)分析

2024-04-28 11:22:18

2021-10-27 17:57:35

設(shè)計模式場景

2016-08-03 16:01:47

GitLinux開源

2025-03-13 06:22:59

2022-06-17 08:05:28

Grafana監(jiān)控儀表盤系統(tǒng)

2017-09-13 09:15:28

蘋果iPhone8

2021-09-23 15:13:02

Spring依賴Java

2022-07-04 08:01:16

OSPFRIPBGP

2021-10-19 07:27:08

HTTP代理網(wǎng)絡(luò)

2017-09-13 15:00:32

蘋果
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號