一文搞定GIT版本管理

一文搞下GIT版本管理

有的時候使用git忘記了命令,所以整理一篇方便日後使用,與各位共享。


GIT簡介

廢話不多說,網上介紹很多,多的不說了,總結一下幾點:

  1. GIT和LINUX是同一個大神搞定的,所以開源免費。當然,免費的遠程倉庫GITHUB裏不花錢,託管的代碼也是開源的,想保密就得掏錢。
  2. GIT是用C語言開發,內部通過指針控制,所以速度很快。
  3. GIT是分佈式分佈式版本控制系統;相對於分佈式,當然就有集中式。那麼區別在哪呢?下面簡單介紹:
    • 集中式版本控制系統,版本庫是集中存放在中央服務器的,而幹活的時候,用的都是自己的電腦,所以要先從中央服務器取得最新的版本,然後開始幹活,幹完活了,再把自己的活推送給中央服務器。

在這裏插入圖片描述
- 分佈式版本控制系統根本沒有“中央服務器”,每個人的電腦上都是一個完整的版本庫,這樣,你工作的時候,就不需要聯網了,因爲版本庫就在你自己的電腦上。不同人操作後把操作的內容推送給對方就好了。
在這裏插入圖片描述
**注:圖片來及百度搜索


GIT安裝

  1. Linux:
sudo apt-get install git
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

按照上面命令操作就行。
2. Mac和Windos:有圖像界面直接上官網,有教程,一步一步跟着操作就OK。


GIT常用命令

git init:

初始化,將一個文件夾穿件爲GIT版本庫,該文件夾中所有內容都會被GIT管理。LINUX和MAC直接通過終端進入指定目錄後輸入該命令即可,windows在開始菜單打開GIT BASH進入指定目錄後輸入命令或者在指定文件夾右鍵,會出現GIT BASH,然後就可以直接在釐米昂輸入命令。例如:將~/test_git目錄創建爲GIT

wtzhu@ubuntu:~/test_git$ git init
Initialized empty Git repository in /home/wtzhu/test_git/.git/

git status:

查看工作區內容的狀態,和倉庫之間的差異。

git add filename/*:

將工作區的內容提交到暫存區,注意此時還沒有提交到倉庫,完全提交到倉庫需要下一條命令。‘*’表示所有文件

git commit -m ‘註釋’:

將暫存區的內容提交到倉庫。例:~/test_git目錄目前是個空文件夾,我需要在裏面創建一個test1.txt文件,並添加hello word進去,並提交到倉庫,需要以下操作:

wtzhu@ubuntu:~/test_git$ ls                 # 當前目錄下是空的
wtzhu@ubuntu:~/test_git$ git status         # 查看一下狀態,因爲空的,所以狀態也是空的
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
wtzhu@ubuntu:~/test_git$ vi test1.txt      # 創建test1.txt,並寫入hello world
wtzhu@ubuntu:~/test_git$ cat test1.txt     # 查看test1.txt內容爲hello world
hello world
wtzhu@ubuntu:~/test_git$ git add test1.txt # 將test1.txt提交到暫存區
wtzhu@ubuntu:~/test_git$ git status        # 再次查看狀態,顯示新提交一個test1.txt
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

	new file:   test1.txt

wtzhu@ubuntu:~/test_git$ git commit -m '第一次提交test1.txt'   # 將test1.txt提交到本地倉庫
[master (root-commit) 83d70bb] 第一次提交test1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
wtzhu@ubuntu:~/test_git$ git status    #再次查看狀態,提交完了就是空的
On branch master
nothing to commit, working directory clean

git diff filename:

查看文件的不同。例如我修改了test1.txt的內容爲hello everyone,再來操作

wtzhu@ubuntu:~/test_git$ cat test1.txt     # 查看內容,被修改爲了hello everyone
hello everyone
wtzhu@ubuntu:~/test_git$ git status        # 查看狀態
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   test1.txt                   # 顯示test1.txt被改動了

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git diff test1.txt    # 查看不同之處
diff --git a/test1.txt b/test1.txt
index 7fd5222..60e2264 100644
--- a/test1.txt
+++ b/test1.txt
@@ -1 +1 @@
-hello word                                 # 原本的內容
+hello everyone                             # 當前文件中的內容
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git status 
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   test1.txt

wtzhu@ubuntu:~/test_git$ git commit -m '修改內容爲hello everyone'
[master e8d0557] 修改內容爲hello everyone
 1 file changed, 1 insertion(+), 1 deletion(-)
wtzhu@ubuntu:~/test_git$ git status
On branch master
nothing to commit, working directory clean
wtzhu@ubuntu:~/test_git$ 

git log/git log --pretty=oneline:

查看提交歷史。git log查看詳細信息,git log --pretty=oneline以一行簡要的形式展現。

git reset --hard commitid/HEAD^

版本回退到指定版本,HEAD^表示上一個版本。例如,test1.txt中的內容已經被改爲了hello everyone,但是我現在覺得helle world更霸氣,需要回去。當然你可直接在文本中操作,但是如果開發,成堆代碼不嫌麻煩就沒事。GIT提供更方便的形式:

wtzhu@ubuntu:~/test_git$ git log       # 查看提交的日誌
commit e8d05571fc8984c36d7b5ccd9b8a7eab89548cdb # commitid
Author: wtzhu <[email protected]>       # usrinfo
Date:   Wed Mar 18 14:37:31 2020 +0800  # date

    修改內容爲hello everyone            # 註釋

commit 83d70bbc2e00448dd87eb008b246e21d337fed3a
Author: wtzhu <[email protected]>
Date:   Wed Mar 18 14:27:07 2020 +0800

    第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline  # 一行顯示
e8d05571fc8984c36d7b5ccd9b8a7eab89548cdb 修改內容爲hello everyone
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git reset --hard 83d70bb  # 穿梭回到過去
HEAD is now at 83d70bb 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word                                          # 穿梭成功
wtzhu@ubuntu:~/test_git$ 

git checkout – filename:

把工作區的內容回撤到更改前,此時未被提交的暫存區。例如,在test1.txt中不小心加入了err,想要刪掉,可以手動刪掉,但是如果修改了代碼裏的部分參數,發現之前的參數更好,一個個換回之前的參數太費勁或者忘了改了哪些,此時就用改命令

wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
err                                                 # 不小心修改的內容
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   test1.txt

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git checkout -- test1.txt # 撤銷修改
wtzhu@ubuntu:~/test_git$ cat test1.txt             # 修改的內容已經改回來了
hello word

git reset HEAD :

與上一條的作用相同,不同之處在於這個是撤銷已經提交到暫存區的內容,將暫存區的內容返還到工作區,如果需要修改會最初狀態,接接着使用上一條命令

hello word
err
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	modified:   test1.txt

wtzhu@ubuntu:~/test_git$ git reset HEAD test1.txt      # 將緩衝區的內容撤回到工作區
Unstaged changes after reset:
M	test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt                 # 此時工作區內容還是錯誤的
hello word
err
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	modified:   test1.txt

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git checkout -- test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word                                              # 完全恢復正常

git rm:

刪除倉庫某個文件。例如在當前目錄下創建test2.txt並提交到倉庫,然後在工作區又刪掉了test2.txt,需要把倉庫也改回來,就使用這個,當然這個和git add一樣,操作完後需要git commit一下

wtzhu@ubuntu:~/test_git$ touch test2.txt
wtzhu@ubuntu:~/test_git$ ls
test1.txt  test2.txt
wtzhu@ubuntu:~/test_git$ git add *
wtzhu@ubuntu:~/test_git$ git commit -m 'add test2.txt'
[master 9f7f5fc] add test2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test2.txt
wtzhu@ubuntu:~/test_git$ git status
On branch master
nothing to commit, working directory clean
wtzhu@ubuntu:~/test_git$ rm test2.txt 
wtzhu@ubuntu:~/test_git$ ls
test1.txt
wtzhu@ubuntu:~/test_git$ git status            # 查看狀態顯示刪除了一個文件
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

	deleted:    test2.txt

no changes added to commit (use "git add" and/or "git commit -a")
wtzhu@ubuntu:~/test_git$ git rm test2.txt      # 刪除一個文件
rm 'test2.txt'
wtzhu@ubuntu:~/test_git$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

	deleted:    test2.txt

wtzhu@ubuntu:~/test_git$ git commit -m 'delete test2.txt'  # 提交到倉庫
[master e9789a4] delete test2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test2.txt
wtzhu@ubuntu:~/test_git$ git status            # 恢復原狀
On branch master
nothing to commit, working directory clean

git branch [name]:

git branch [name]創建一個名爲name的分支,git branch查看現有分支

wtzhu@ubuntu:~/test_git$ git branch fred
wtzhu@ubuntu:~/test_git$ git branch
  fred
* master

創建了一個名爲fred的分支,*所指是當前分支

git checkout :

切換到某個分支,例如從master分支切換到fred分支

wtzhu@ubuntu:~/test_git$ git checkout fred
Switched to branch 'fred'
wtzhu@ubuntu:~/test_git$ git branch
* fred
  master

git merge :

在當前分支合併某一分支,例如我再fred分支上修改了test1.TXT文件,並提交到倉庫,回到master分支還沒有改動,此時,把fred分支合併過來,master分支就會改過來。

wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git commit -m 'add fred branch in test1.txt'
[fred fa7b7bc] add fred branch in test1.txt
 1 file changed, 1 insertion(+)
 wtzhu@ubuntu:~/test_git$ git log --pretty=oneline
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt   # 此時在fred分支上已經別提交到倉庫
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git checkout master 
Switched to branch 'master'
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline                      # 回到master分支發現還沒有提交
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt     # 內容確實沒有被修改
hello word
wtzhu@ubuntu:~/test_git$ git merge fred    # 合併完成後就恢復過來了
Updating 16d2861..fa7b7bc
Fast-forward
 test1.txt | 1 +
 1 file changed, 1 insertion(+)
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch

git branch -d :

刪除分支,當一個分支用完了,不需要了就刪掉,例如fred分支的作用就是加一句,使命完成後不需要了就刪掉

wtzhu@ubuntu:~/test_git$ git branch -d fred    # 刪除fred分支
Deleted branch fred (was fa7b7bc).
wtzhu@ubuntu:~/test_git$ git branch            # 再次查看就沒有了
* master

當然在合併分支的時候也會出現意外衝突,例如,我再創建一個Tom分支,並添加一句add tom branch,在master分支我也添加了內容look master,此時合併就會出現問題
wtzhu@ubuntu:~/test_git$ git branch tom
wtzhu@ubuntu:~/test_git$ git checkout tom
Switched to branch 'tom'
wtzhu@ubuntu:~/test_git$ vi test1.txt 
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
add tom branch
wtzhu@ubuntu:~/test_git$ git add *
wtzhu@ubuntu:~/test_git$ git commit -m 'add tom branch in test1.txt'
[tom d80f9ce] add tom branch in test1.txt
 1 file changed, 1 insertion(+)
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline
d80f9cec849aec87e924e2e1d6524461ae98947d add tom branch in test1.txt  # Tom分支已經正常提交
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ git checkout master 
Switched to branch 'master'
wtzhu@ubuntu:~/test_git$ ls
test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
wtzhu@ubuntu:~/test_git$ vi test1.txt 
wtzhu@ubuntu:~/test_git$ git add *
wtzhu@ubuntu:~/test_git$ git commit -m 'add look master in test1.txt'
[master ccd8b57] add look master in test1.txt
 1 file changed, 1 insertion(+)
wtzhu@ubuntu:~/test_git$ git log --pretty=oneline 
ccd8b5747b637719bf0c16fce5721c48ed5ec780 add look master in test1.txt   # master分支正常提交
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
look master
wtzhu@ubuntu:~/test_git$ git merge tom     # 合併分支報錯,因爲兩個版本都做了修改,GIT不知道哪個纔是想要的,需要手動選擇一下
Auto-merging test1.txt
CONFLICT (content): Merge conflict in test1.txt
Automatic merge failed; fix conflicts and then commit the result.
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
<<<<<<< HEAD
look master
=======
add tom branch
>>>>>>> tom
wtzhu@ubuntu:~/test_git$ vi test1.txt      # 手動編輯一下test1.txt
wtzhu@ubuntu:~/test_git$ git add test1.txt 
wtzhu@ubuntu:~/test_git$ git commit -m 'merge tom to master'   # 編輯完成後再次提交一下,此時就講tom分支內容合併過來了
[master 56d07f4] merge tom to master
wtzhu@ubuntu:~/test_git$ cat test1.txt 
hello word
add fred branch
look master
add tom branch

wtzhu@ubuntu:~/test_git$ git log --pretty=oneline 
56d07f423affcbb9018762288e23ff23c0daef17 merge tom to master
ccd8b5747b637719bf0c16fce5721c48ed5ec780 add look master in test1.txt
d80f9cec849aec87e924e2e1d6524461ae98947d add tom branch in test1.txt
fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt

git log --pretty=oneline --grep:

查看提交記錄,用圖的形式展示分支合併

wtzhu@ubuntu:~/test_git$ git log --pretty=oneline --graph
*   56d07f423affcbb9018762288e23ff23c0daef17 merge tom to master
|\  
| * d80f9cec849aec87e924e2e1d6524461ae98947d add tom branch in test1.txt
* | ccd8b5747b637719bf0c16fce5721c48ed5ec780 add look master in test1.txt
|/  
* fa7b7bc19a08c55b8550560a146620f0f5f84a18 add fred branch in test1.txt
* 16d286199cc107e2a558f25febc974e611bf09f4 del 1.txt
* b25768d5e9ee468d899bb6a8ef0d0713c2b51508 add 1.txt
* e9789a48cd35b8bc8b5346f71725666f603a2265 delete test2.txt
* 9f7f5fc2dcd47392f49bb013a1bdba07efac14bd add test2.txt
* 83d70bbc2e00448dd87eb008b246e21d337fed3a 第一次提交test1.txt

遠程倉庫相關的命令,最直接的遠程倉庫就是github,可以拿這幾個來試試

  • git remote add origin git@server-name:path/repo-name.git關聯一個遠程庫
  • git push -u origin master第一次推送master分支的所有內容
  • git push origin master推送最新修改

友情提示:

記住以上命令基本上可以搞定git,平時使用時多看一下git的提示信息,裏面會給出很多重要提示。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章