Git的官網:https://git-scm.com/downloadsgit
下載安裝完成後打開,以下圖界面:命令行
再進行用戶和郵件的設置:3d
$ git config --global user.name "jin" $ git config --global user.email "akipa11@163.com"
--global參數,表示你這臺機器上全部的Git倉庫都會使用這個配置。rest
版本庫又名倉庫,英文名repository。版本庫的全部文件均可以被Git管理起來,每一個文件的修改、刪除,Git都能跟蹤,以便任什麼時候刻均可以追蹤歷史,或者在未來某個時刻能夠「還原」。日誌
建立版本庫:code
$ mkdir learn_git Jin@Jin-PC MINGW64 ~ $ cd learn_git Jin@Jin-PC MINGW64 ~/learn_git $ pwd /c/Users/Jin/learn_git
使用命令git init對版本庫進行初始化:blog
Jin@Jin-PC MINGW64 ~/learn_git $ git init Initialized empty Git repository in C:/Users/Jin/learn_git/.git/
這時候查看目錄,會發現當前目錄下多了一個.git的目錄,這個目錄是Git來跟蹤管理版本庫的,如無必要,不要對這個目錄進行修改。ip
Jin@Jin-PC MINGW64 ~/learn_git (master) $ ls -al total 12 drwxr-xr-x 1 Jin 197121 0 四月 19 19:50 ./ drwxr-xr-x 1 Jin 197121 0 四月 19 19:49 ../ drwxr-xr-x 1 Jin 197121 0 四月 19 19:50 .git/
下面咱們新建一個文件test.txt,內容以下:get
Hello World! Git is a version control system. I'm learning Git.
用git add命令把文件test.txt添加到倉庫中。it
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git add test.txt warning: LF will be replaced by CRLF in test.txt. The file will have its original line endings in your working directory.
命令git commit則是把文件提交到倉庫中。
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git commit -m "wrote a test file" [master (root-commit) ecd1e29] wrote a test file 1 file changed, 3 insertions(+) create mode 100644 test.txt
參數-m後面輸入的是本次提交的說明,能夠輸入任意內容,固然最好是有意義的,這樣你就能從歷史記錄裏方便地找到改動記錄。
咱們把test.txt文件的內容進行修改,內容以下:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ cat test.txt Git is a version control system. I'm learning Git.
運行git status命令:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ 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: test.txt no changes added to commit (use "git add" and/or "git commit -a")
git status命令可讓咱們時刻掌握倉庫當前的狀態,能夠看到,文件test.txt已經被修改了,但尚未提交修改。
git diff能夠查看具體修改了什麼內容:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git diff test.txt warning: LF will be replaced by CRLF in test.txt. The file will have its original line endings in your working directory. diff --git a/test.txt b/test.txt index 049ab45..da0261d 100644 --- a/test.txt +++ b/test.txt @@ -1,3 +1,2 @@ -Hello World! Git is a version control system. I'm learning Git.
知道了文件做了哪些修改,此時就能夠放心把文件提交到倉庫了:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git add test.txt
此時再用git status命令查看,能夠看到,要被提交的修改包括文件test.txt:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: test.txt
提交到倉庫:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git commit -m "del first line" [master 88cb81a] del first line 1 file changed, 1 deletion(-)
再用git status查看狀態,能夠看到,當前沒有須要提交的修改,並且,工做目錄是乾淨(working tree clean)的。:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git status On branch master nothing to commit, working tree clean
git log能夠查看時間從最近到最遠的提交日誌:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git log commit 88cb81a0e46bb1ffae280fd38a26e0112edbdf82 (HEAD -> master) Author: jin <akipa11@163.com> Date: Fri Apr 19 20:16:15 2019 +0800 del first line commit ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f Author: jin <akipa11@163.com> Date: Fri Apr 19 20:01:31 2019 +0800 wrote a test file
若是輸出信息太多的話,能夠加上--pretty=oneline:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git log --pretty=oneline 88cb81a0e46bb1ffae280fd38a26e0112edbdf82 (HEAD -> master) del first line ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f wrote a test file
其中,前面的字符爲commit id(版本號),最後面的是提交的說明。
若是咱們想把文件退回到初始的版本,那麼,應該怎麼作呢?
首先,Git必須知道當前版本是哪一個版本,在Git中,用HEAD表示當前版本,也就是最新的提交88cb8...,上一個版本就是HEAD^,上上一個版本就是HEAD^^,固然往上100個版本寫100個^比較容易數不過來,因此寫成HEAD~100。
版本回退使用命令git reset:
$ git reset --hard HEAD^ HEAD is now at ecd1e29 wrote a test file Jin@Jin-PC MINGW64 ~/learn_git (master) $ cat test.txt Hello World! Git is a version control system. I'm learning Git.
能夠看到,test.txt文件已經退回到最初的版本了。
可是,咱們用git log查看的話,會發現,以前最新的版本del first line居然沒有了!
$ git log commit ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f (HEAD -> master) Author: jin <akipa11@163.com> Date: Fri Apr 19 20:01:31 2019 +0800 wrote a test file
這時候,若是咱們想退回最新版本就作不到了,若是命令行還沒關的話,能夠上翻查找到:
$ git log commit 88cb81a0e46bb1ffae280fd38a26e0112edbdf82 (HEAD -> master) Author: jin <akipa11@163.com> Date: Fri Apr 19 20:16:15 2019 +0800 del first line commit ecd1e29aee2a2ee04ffbdf5e4b000e90cc96c83f Author: jin <akipa11@163.com> Date: Fri Apr 19 20:01:31 2019 +0800 wrote a test file
再用命令git rest退回:
$ git reset --hard 88cb81a0e HEAD is now at 88cb81a del first line Jin@Jin-PC MINGW64 ~/learn_git (master) $ cat test.txt Git is a version control system. I'm learning Git.
能夠發現,又回到了最新的修改版本。但若是操做了太屢次數的話,以前的可能就被覆蓋了,那麼,就再也回不到這個版本了。
幸虧,git提供了一個命令git reflog用來記錄每一次的命令:
$ git reflog 88cb81a (HEAD -> master) HEAD@{0}: reset: moving to 88cb81a0e ecd1e29 HEAD@{1}: reset: moving to HEAD^ 88cb81a (HEAD -> master) HEAD@{2}: commit: del first line ecd1e29 HEAD@{3}: commit (initial): wrote a test file
能夠看到,使用git reflog能夠退回到任何一個版本。
使用git reset --hard命令改變了HEAD指向某個版本後,還能夠用git reflog來查看全部版本。
(1)、查看內容,直接修改文件內容;
(2)、用git status 命令查看狀態:
$ 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: test.txt no changes added to commit (use "git add" and/or "git commit -a")
能夠看到,咱們能夠用命令git checkout -- <file>來進行撤銷修改:
Jin@Jin-PC MINGW64 ~/learn_git (master) $ git checkout -- test.txt
$ cat test.txt Hello World! Git is d distributed version control system. Git is free software distributed under the GPL. Git is a version control system. I'm learning Git. ohhhhhh Jin@Jin-PC MINGW64 ~/learn_git (master) $ git add test.txt Jin@Jin-PC MINGW64 ~/learn_git (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: test.txt Jin@Jin-PC MINGW64 ~/learn_git (master) $ git reset HEAD test.txt Unstaged changes after reset: M test.txt Jin@Jin-PC MINGW64 ~/learn_git (master) $ git checkout -- test.txt Jin@Jin-PC MINGW64 ~/learn_git (master) $ cat test.txt Hello World! Git is d distributed version control system. Git is free software distributed under the GPL. Git is a version control system. I'm learning Git. Jin@Jin-PC MINGW64 ~/learn_git (master) $ git status On branch master nothing to commit, working tree clean
能夠看到,若是咱們已經用命令git add添加文件後,能夠前後用命令git reset HEAD <file>和git checkout -- <file>來進行撤銷修改。
能夠用命令git reflog查看版本歷史,並退回以前版本,前提是沒有推送到遠程倉庫。
在本地刪除一個文件後,用git status查看狀態:
$ rm test.txt Jin@Jin-PC MINGW64 ~/learn_git (master) $ 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: test.txt no changes added to commit (use "git add" and/or "git commit -a")
能夠看到,若是是刪錯了文件,還能夠用命令git checkout -- <file>把誤刪的文件恢復。
$ ls LICENSE Jin@Jin-PC MINGW64 ~/learn_git (master) $ git checkout -- test.txt Jin@Jin-PC MINGW64 ~/learn_git (master) $ ls LICENSE test.txt
而若是是想把版本庫的文件也同時刪除,能夠用命令git rm 刪除並用git commit提交。
$ git rm test.txt rm 'test.txt' Jin@Jin-PC MINGW64 ~/learn_git (master) $ git commit -m "del test.txt" [master 9b508c2] del test.txt 1 file changed, 5 deletions(-) delete mode 100644 test.txt Jin@Jin-PC MINGW64 ~/learn_git (master) $ git status On branch master nothing to commit, working tree clean Jin@Jin-PC MINGW64 ~/learn_git (master) $ ls LICENSE