Git 概述
Git 是一個免費的、開源的分布式版本控制系統,可以快速高效地處理從小型到大型的各種項目。
Git 的特色是分支,人家圖標上都敢這么畫了。 對了,這里要提一下,Git 和 Linux 是同一個爸爸,所以 Linux 系統上能跑的原生指令 Git 上面也都可以,反之也一樣。
git 分區原理
工作區。新文件沒被add到暫存區,顯紅色。
暫存區。進倉庫前暫時存放區域,未對本地倉庫生效。對暫存區文件修改,顯藍色。第一次創建并add到暫存區的文件,由于遠程倉庫沒同步,顯綠色。(注:存放在 “.git/index目錄”。)
本地倉庫。暫存區commit代碼放入本地倉庫,通過 push 推送到遠程倉庫。(注:存在“.git”目錄。)
遠程倉庫。在前面的話,倉庫里都有些什么東西只有你自己知道,但是一旦把本地倉庫推到遠程倉庫之后,倉庫里有什么東西大家就都知道了。
Git 常用指令
(本篇講解包括但不限于這些命令。)
設置用戶簽名
開頭兩個命令是用來設置用戶簽名的,這個一般就用一次就完事兒了,因為你要提交代碼的時候是需要責任跟蹤的。
這個 name,email 啥的,人家也不會去認證,不過組里面肯定是會有規范的,也不會讓你想起啥名就隨便起吧。
初始化本地庫
新手一般就老老實實 git init 起手嘛,昨天我也是這么問我導師的,他說不用,直接 git clone 就好了。我想想也是,團隊開發嘛。
查看本地庫狀態
git status首次查看,工作區是沒有什么文件的:
$ git status On branch master No commits yet nothing to commit (create/copy files and use "git add" to track)當我們新增了一個文件之后,再查看狀態, 會檢測到未追蹤的文件:
$ git status On branch master No commits yet Untracked files: (use "git add..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track)
添加暫存區
git add 文件名
$ git add hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory.再次查看狀態(檢測到暫存區有新文件:)
$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached..." to unstage) new file: hello.txt
提交本地庫
git commit -m "日志信息" 文件名
$ git commit -m "my first commit" hello.txt warning: LF will be replaced by CRLF in hello.txt. The file will have its original line endings in your working directory. [master (root-commit) 86366fa] my first commit 1 file changed, 16 insertions(+) create mode 100644 hello.txt再查看狀態(沒有文件需要提交):
$ git status On branch master nothing to commit, working tree clean
修改文件
修改一下文件之后再查看狀態:
$ git status On branch master Changes not staged for commit: (use "git add那么接下來就需要再次將修改的文件提交到暫存區,然后提交到本地倉庫。..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: hello.txt no changes added to commit (use "git add" and/or "git commit -a")
查看歷史版本
git reflog 查看版本信息 git log 查看版本詳細信息如果是想快速瀏覽版本信息,可以使用 reflog 即可,畢竟公司項目版本迭代信息可能已經很多了,想看詳細日志也得看你有沒有那個精力了。
$ git reflog 087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit ca8ded6 HEAD@{1}: commit: my second commit 86366fa HEAD@{2}: commit (initial): my first commit
版本
以前我只知道版本回退,現在我知道原來還能向新。
git reset --hard 版本號版本號是什么?版本號,查看一下日志就能看到了。
$ git reflog 087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit ca8ded6 HEAD@{1}: commit: my second commit 86366fa HEAD@{2}: commit (initial): my first commit
--切換到 86366fa 版本,也就是我們第一次提交的版本 $ git reset --hard 86366fa HEAD is now at 86366fa my first commit
--切換完畢之后再查看歷史記錄,當前成功切換到了 86366fa 版本 $ git reflog 86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa 087a1a7 HEAD@{1}: commit: my third commit ca8ded6 HEAD@{2}: commit: my second commit 86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit --然后查看文件 hello.txt,發現文件內容已經變化Git 切換版本,底層其實是移動的 HEAD 指針。
Git 分支操作
在版本控制過程中,同時推進多個任務,為每個任務,我們就可以創建每個任務的單獨分支。使用分支意味著程序員可以把自己的工作從開發主線上分離開來,開發自己分支的時候,不會影響主線分支的運行。對于初學者而言,分支可以簡單理解為副本,一個分支就是一個單獨的副本。(分支底層其實也是指針的引用。)
分支基本操作
查看分支
小伙子,不要一上來就想著創建分支嘛。先看看前輩們已經達到了哪個高度了嘛。
git branch -v
$ git branch -v * master 087a1a7 my third commit (*代表當前所在的分區)
創建分支
git branch 分支名
$ git branch hot-fix $ git branch -v hot-fix 087a1a7 my third commit (剛創建的新的分支,并將主分支 master 的內容復制了一份) * master 087a1a7 my third commit
分支開發
接下來就可以在分支上進行開發了,開發完之后記得將代碼提交到本地倉庫。
切換分支
git checkout 分支名
$ git checkout hot-fix Switched to branch 'hot-fix' --發現當先分支已由 master 改為 hot-fix
合并分支
git merge 分支名這里有個要注意的點:不僅是可以把旁支合并到主支上,主支也可以被合并到分支上,分支之間也可以互相合并。 反正你愛怎么玩怎么玩,都是在你本地倉庫上,又沒有推送到遠程倉庫。 那現在我們想把旁支合并到主支上要怎么做呢?
1、切換到主支 2、git merge 旁支名這樣就萬事大吉了嗎?不一定的。腦子清醒的話很容易想到一個問題:主支修改了一個文件,分支也修改了同一個文件,現在以誰的為準? 腦子更清醒的人就要說了:你傻呀,我 clone 下來就切到分支去操作了,在我本地倉庫我又不會去動主支的。 腦子再清醒一點的人已經懶的跟他說了:大哥,咱這是團隊開發,咱的本地倉庫早晚是都要合并到主線上的。
合并沖突解決
沖突產生的表現:后面狀態為 MERGING 打開沖突文件可以看到沖突的地方:
hello git! hello atguigu! <<<<<<< HEAD hello git! hello atguigu! master test hello git! hello atguigu! ======= hello git! hello atguigu! hello git! hello atguigu! hot-fix test >>>>>>> hot-fixHEAD 到 === 的部分是主支的,后面是 hot-fix 的 沖突產生的原因: 合并分支時,兩個分支在同一個文件的同一個位置有兩套完全不同的修改。Git 無法替我們決定使用哪一個。必須人為決定新代碼內容。 解決辦法:
1、刪掉沖突中不要的部分,留下最終的部分 2、git add 添加到暫存區 3、 git commit -m 注意,此時的 commit 后面不能帶文件名。帶文件名是幾個意思呢?到底用哪個呢? 然后就會發現合并成功了。
Git 團隊協作機制
團隊協作
跨團隊協作
遠程倉庫
這里不說 github,因為還有 gitlab,公司內部代碼怎么能放到廣域網上呢!!!
別名
為什么要別名呢?其實起不起別名都無所謂,主要是有個別名方便操作。你說是記一個網址容易還是記一個自己起的名字容易嘞?
$ git remote -v $ git remote add ori https://github.com/xxx.git $ git remote -v ori https://github.com/xxx.git (fetch) ori https://github.com/xxx.git (push)
推送本地倉庫到遠程倉庫
當文件還在本地倉庫的時候,咱怎么玩都行。但是一旦推送上去了,那就產生歷史版本了,大家都看得到了。 不過這個推送啊,也不是咱想推送就能推送的,還得管理員給咱授權,這個不難理解吧。
git push 別名 分支
$ git push ori master Logon failed, use ctrl+c to cancel basic credential prompt. Username for 'https://github.com': atguiguyueyue Counting objects: 3, done. Delta compression using up to 12 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 276 bytes | 276.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/atguiguyueyue/git-shTest.git * [new branch] master -> master
克隆遠程倉庫到本地
這不用說吧,這個很簡單。咱雖然說之前不會推送,但是拉取還是沒有問題的。 clone 會做如下操作: 1、拉取代碼。 2、初始化本地倉庫。 3、創建別名。 會創建別名,你 clone 之后用 git remote -v 看一下,一般是 origin。
拉取遠程庫內容
這個有兩個辦法,昨天我用的是先 clone,在 checkout 的方式選擇我要的分支。 也可以直接 pull:
git pull 遠程庫地址別名 遠程分支名
$ git pull ori master remote: Enumerating objects: 5, done. remote: Counting objects: 100% (5/5), done. remote: Compressing objects: 100% (1/1), done. remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/atguiguyueyue/git-shTest * branch master -> FETCH_HEAD 7cb4d02..5dabe6b master -> ori/master Updating 7cb4d02..5dabe6b Fast-forward hello.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
-
Linux
+關注
關注
87文章
11345瀏覽量
210385 -
開源
+關注
關注
3文章
3402瀏覽量
42711 -
Git
+關注
關注
0文章
201瀏覽量
15818 -
控制系數
+關注
關注
0文章
4瀏覽量
2340
原文標題:一聽就懂的Git詳解
文章出處:【微信號:技術讓夢想更偉大,微信公眾號:技術讓夢想更偉大】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論