GIt學習筆記

一、安裝GIt

1.一、Win下安裝git:

下載:https://git-scm.com/download/winhtml

基本上一路回車,安裝成功後,點擊Git Bash,出現以下圖說明安裝成功:git

1.二、設置名字和郵件:

git config --global user.name ldq
git config --global user.email 」ldq@qq.com「

二、建立版本庫

版本庫又名倉庫,英文名repositorygithub

小結:vim

git init   #初始化gitbash

git add  file1 dom

git commit -m 「註釋」  #提交版本信息ssh

Administrator@WSQRUUOEZ1H434O MINGW64 ~
$ mkdir learngit#建立版本庫文件

Administrator@WSQRUUOEZ1H434O MINGW64 ~
$ cd learngit/

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit
$ pwd
/c/Users/Administrator/learngit

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit
$ git init#初始化版本庫文件
Initialized empty Git repository in C:/Users/Administrator/learngit/.git/

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ ls -al#多了一個.git文件
total 20
drwxr-xr-x 1 Administrator 197121 0 9月  25 17:07 ./
drwxr-xr-x 1 Administrator 197121 0 9月  25 17:07 ../
drwxr-xr-x 1 Administrator 197121 0 9月  25 17:07 .git/

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ vi readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git add readme.txt#新增文件
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ ls
readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git commit -m "wrote a readme file"#提交代碼-m註釋
[master (root-commit) bcb8042] wrote a readme file
 1 file changed, 2 insertions(+)
 create mode 100644 readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$

三、時光機穿梭

小結:ide

git status  #查看git狀態測試

git add  file1 #新增代碼文件設計

git diff readme.txt #比較代碼文件

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ vi readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git add readme.txt
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   readme.txt


Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git commit -m "test readme.txt"
[master ba06e36] test readme.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git diff readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ vi readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git diff readme.txt
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory
diff --git a/readme.txt b/readme.txt
index c9d14fd..4aed2d2 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1,2 @@
-Git is a test version control system.
+Git is a test2 version control system.
 Git is free software.

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (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:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git add readme.txt
warning: LF will be replaced by CRLF in readme.txt.
The file will have its original line endings in your working directory

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git commit -m "test2 readme.txt"
[master 0908706] test2 readme.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$

一、版本回退

git用hard來指定那個版本,使用git reset來指定回退那個版本

git reset --hard HEAD^  #指定會回到上一個版本 上上個版本^^  上10個版本~10

git reset  --hard 0809  #指定版本號

git log   #查看版本信息

git log  --pretty=oneline #美化查看信息

git reflog  #查看歷史信息

Administrator@WSQRUUOEZ1H434O MINGW64 ~
$ cd learngit/

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git log
commit 09087062f3a05bad86ac8274652655370941ec82 (HEAD -> master)
Author: ldq <「961811769@qq.com>
Date:   Tue Sep 25 17:32:05 2018 +0800

    test2 readme.txt

commit ba06e367f0b657030314918fe5e0249dafe89880
Author: ldq <「961811769@qq.com>
Date:   Tue Sep 25 17:30:30 2018 +0800

    test readme.txt

commit bcb80425fd26bda94e289953d4c552f3ba1a1c02
Author: ldq <「961811769@qq.com>
Date:   Tue Sep 25 17:18:55 2018 +0800

    wrote a readme file

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git log --pretty=oneline
09087062f3a05bad86ac8274652655370941ec82 (HEAD -> master) test2 readme.txt
ba06e367f0b657030314918fe5e0249dafe89880 test readme.txt
bcb80425fd26bda94e289953d4c552f3ba1a1c02 wrote a readme file

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git reset --hard HEAD^
HEAD is now at ba06e36 test readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git log --pretty=oneline
ba06e367f0b657030314918fe5e0249dafe89880 (HEAD -> master) test readme.txt
bcb80425fd26bda94e289953d4c552f3ba1a1c02 wrote a readme file

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ cat readme.txt
Git is a test version control system.
Git is free software.

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git reset --hard 0908
HEAD is now at 0908706 test2 readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git log --pretty=oneline
09087062f3a05bad86ac8274652655370941ec82 (HEAD -> master) test2 readme.txt
ba06e367f0b657030314918fe5e0249dafe89880 test readme.txt
bcb80425fd26bda94e289953d4c552f3ba1a1c02 wrote a readme file

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ cat readme.txt
Git is a test2 version control system.
Git is free software.

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git reflog
0908706 (HEAD -> master) HEAD@{0}: reset: moving to 0908
ba06e36 HEAD@{1}: reset: moving to HEAD^
0908706 (HEAD -> master) HEAD@{2}: commit: test2 readme.txt
ba06e36 HEAD@{3}: commit: test readme.txt
bcb8042 HEAD@{4}: commit (initial): wrote a readme file

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$

二、工做區和暫存區

Git和其餘SVN不一樣之處在於還有一個暫存區的概要。

工做區:看到的文件夾eg:learngit

版本庫:learngit 下有個隱藏文件.git的文件夾是Git版本庫

暫存區:stage或者index #git add file1 file2 後就把file1和file2  存放到了stage

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git status
On branch master
nothing to commit, working tree clean

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ vim readme.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ vim test.txt

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (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:   readme.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        test.txt

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git add readme.txt  test.txt
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ git commit -m "add-test.txt-index"
[master 47759dc] add-test.txt-index
 2 files changed, 2 insertions(+)
 create mode 100644 test.txt

三、管理修改

Git比其餘版本控制系統設計的優秀,由於Git跟蹤並管理的是修改,而非文件。

若是第一次修改完執行git add 而後再對他修改,修改完了,直接執行git commit ,提交的是第一次修改完的內容,而非第二次修改的,因此第二次修改完之後,須要再次執行add操做。

第一次修改 -> git add -> 第二次修改 -git add -> git commit

 

四、撤銷修改

敲代碼時候,修改錯文件了,想把這個文件的修改給撤銷了,應該怎麼辦:

一、git checkout -- file      #沒有執行過git add 的時候

二、git reset HEAD <file>  #執行過git add的時候

      git checkout -- file  

三、git reset --hard HEAD^  #執行過commit

 

五、刪除文件

rm test.txt

git rm test.txt  #git add test.txt 效果同樣

git commit -m "remove test.txt" 

刪除錯了想恢復怎麼辦?

git checkout -- test.txt   

git checkout       #用版本庫裏的版本替換工做區的版本,不管工做區是修改仍是刪除,均可以「一鍵還原」。

 

三、添加遠程庫 

一、新建本地倉庫

這裏用以前作實驗的,就不在新建了,新建方法以下:

echo "# test" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:ldqchw/test.git
git push -u origin master

二、本地建立SSH公鑰

ssh-keygen -t rsa -C "ldqchw"

-C   "GItHub用戶名"

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ ssh-keygen -t rsa -C "ldqchw"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:2Ts7qFAQNiJyKd0Joos+eu4ZKfRAWgXWHlagErsfaHg ldqchw
The key's randomart image is:
+---[RSA 2048]----+
|=.***+.          |
|+Bo=*o           |
|+ooo..           |
|=*  ..   o       |
|B+E   . S .      |
|+ooo .     .     |
|.o+..    .o      |
|..oo .  . .o     |
|.++   ..  ..     |
+----[SHA256]-----+

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ cat /c/Users/Administrator/.ssh/
id_rsa       id_rsa.pub   known_hosts

Administrator@WSQRUUOEZ1H434O MINGW64 ~/learngit (master)
$ cat /c/Users/Administrator/.ssh/id_rsa.pub
damaXXXXXXXXXXXXXXXXXXXXXdama

三、將SSH公鑰導入GitHub

四、GitHub上新建一個項目

五、本地同步到遠程

第一次同步:

git remote add origin git@server-name:path/repo-name.git  或者  git remote add origin https://github.com/ldqchw/learngit.git

eg:

git remote add origin https://github.com/GitHub名字/倉庫名字.git

ssh -T git@github.com #測試是否能通訊

git push -u origin master

之後同步:

git push  origin master

origin  #俗稱默認遠程可改

六、從遠程克隆到本地

git clone git@github.com:michaelliao/gitskills.git 

參考:

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

https://www.cnblogs.com/qcwblog/p/5709720.html

相關文章
相關標籤/搜索