1. 程式人生 > >git 學習指南 學習資料筆記

git 學習指南 學習資料筆記

學習資料地址 王爵的技術小黑屋 https://www.youtube.com/watch?v=29q6zwRGywk

01.什麼是Git

Git是一個分散式的版本管理系統
每次修改後必須commit
本地資料庫
圖片名稱

圖片名稱

02.萌新也看的懂的 Git 基本操作

  • 安裝git : brew install git
    升級 upgrade
  • 配置全域性變數 (使用者名稱): git config --global user.name "jcgit"
    重新配置 : git config --global --replace-all user.name "JcOpenSource"
    (郵箱): git config --global user.email "
    [email protected]
    "
  • 檢視配置檔案:cat ~/.gitconfig
    [alias] 操作命令的別名
    [http]
    [https] proxy = http://127.0.0.1:1087 代理
  • 建立目錄: mkdir
  • 初始化一個空的倉庫 git init
    master 分支
  • 建立檔案 touch README.md
  • 顯示倉庫的狀態: git status
  • 新增檔案到暫存區管理:git add README.md
  • 移檔案從暫存區管理除掉:git rm --cached README.md
  • 將當前檔案目錄下的所有檔案都放到版本控制中:git add -A

暫存區

  • 檢視隱藏檔案:ll -la
  • cd .git
  • ll
  • 提交檔案到伺服器:git commit -m "add readme.md"

提交到遠端github上
git remote add origin https://github.com/JcOpenSource/git.git
git push -u origin master
*與遠端的伺服器先建立連線: git remote add origin https://github.com/JcOpenSource/git.git
origin 遠端伺服器的名字

  • 檢視遠端連線:git remote -v
  • 將本地的檔案提交到伺服器上:git push -u origin master
    -u 以後直接輸入 git push 不用加遠端的連線和分支名字
  • ssh免密校驗【沒有講解】

  • 將遠端程式碼克隆到本地:git clone https://github.com/JcOpenSource/git.git git_demo
    後面加新的檔名稱

  • 將遠端的程式碼拉到本地:git pull
    git pull origin(遠端的連結) master(分之的名字)

03.Git 中的分支

branch 分支
HRAD 當前在使用的分支中的最後一次更新
圖片名稱

  • 建立分支:git branch feature1

  • 檢視分支:git branch
    按q退出

  • 跳轉分支名:git checkout 'feature1'

  • 可以在feature1下開分支feature2,feature2有feature1新增的檔案
  • git checkout -b feature3 等價於 git branch feature3 以及 git checkout feature2

  • 刪除分支:git branch -d feature2
    在命令列中大寫表示非常慎重的操作
    feature3 提交了內容但是沒有合併到master 主分支上所以刪除分支feature3 會出現錯誤

  • 將分支feature3裡面的東西合併到分支master上
    Fast-forward 模式
    將master 的head 指向 feature3的head 做一個相互合併
  • 衝突解決【沒有講】

遠端的分支只有一個

  • 將分支提交到遠端

    注意origin 是遠端的連線

  • 在本地刪除遠端的分支 刪除:
  • 建立遠端分支並且重新命名成f1:git push origin feature1:f1

04 Git 中的合併

  • 檢視日誌: git log
    git log --oneline
    git log --oneline -3
    日誌地址:https://stackoverflow.com/questions/1057564/pretty-git-branch-graphs
  • 配置檔案:git log --all --decorate --oneline --graph


    跳轉到master分支下檢視


    但是我們不知道fb是哪個分支提交的

    預設儲存

    --no-ff
    檢視新的分支結構提交結果

    把master 和 f1 分支提交到遠端

    在遠端修改後f1必須merge master 才能與遠端同步

在master 做修改


將f1移動到master最後一次提交,然後把master 的所有提交併入過來: git rebase master

絕對不要在公共的分支上使用 git rebase

解決衝突:

使用衝突解決工具



05 Git中的回滾撤銷

回到當前版本:git reset master^

^代表上一次、前一次
回退兩次可以加兩個這個符號^
一次類推
git reset master~5 往後倒退5步

絕對撤回

git reset 回到某一個版本

06 gitignore 和 fork 同步

程式碼中的.setting .ide 等配置檔案如何忽略

  • 建立.gitignore 檔案
  • 編寫檔案:
    【1】忽略系統生成的檔案,如IDE的配置
    【2】忽略編譯生成的中間檔案、可執行檔案等
    【3】忽略敏感的配置檔案和本地不想提交的指令碼
*.sh
.settings/
!*.txt


/a/*.class

!*.txt 所有的txt檔案不被忽略

自動生成忽略檔案:https://www.gitignore.io/

[email protected] ~/workspace/git (master*) $ git add -A .
[email protected] ~/workspace/git (master*) $ git commit -m "add gitignore"
[master 2c89780] add gitignore
 4 files changed, 17 insertions(+), 6 deletions(-)
 create mode 100644 .gitignore
 rename m1.txt => Hello.java (100%)
 create mode 100644 a.txt.orig
[email protected] ~/workspace/git (master) $ git push
Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 4 threads
Compressing objects: 100% (14/14), done.
Writing objects: 100% (15/15), 1.50 KiB | 1.50 MiB/s, done.
Total 15 (delta 8), reused 0 (delta 0)
remote: Resolving deltas: 100% (8/8), completed with 2 local objects.
To https://github.com/JcOpenSource/git.git
   c7e0996..2c89780  master -> master
[email protected] ~/workspace/git (master) $
  • git fetch 將上游倉庫拉下來

07 實現 Github 免密推送

ssh免密提交
gitn 中有4個協議:

  • 本地協議
  • HTTP協議
  • ssh協議
    *git協議

生成ssh公鑰:
https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/

ssh-keygen -t rsa -b 4096 -C "[email protected]"

密碼不用輸入為了方便

  • 後臺開啟ssh代理:eval "$(ssh-agent -s)"

自己下載上傳專案失敗!!!

08 Git 中的工作流

https://github.com/xirong/my-git/blob/master/git-workflow-tutorial.md#23-gitflow

gitflow 大型專案

#09 Git 常用圖形化工具

三大工具:

  • sourcetree
  • vscode
  • ide

視訊中的 git emoji 外掛地址:https://plugins.jetbrains.com/plugin/...

  1. Git Community Book 中文版:http://gitbook.liuhui998.com/
  2. Git Tutorial:http://www.vogella.com/tutorials/Git/...
  3. 猴子都能懂的 Git 指南:https://backlog.com/git-tutorial/cn/
  4. Git 簡明指南:http://rogerdudler.github.io/git-guid...

自己提交的git 文件過 Cher 呢

[email protected] ~/Desktop/python-data-visualization (master*) $ git init
Reinitialized existing Git repository in /Users/lijuncheng/Desktop/python-data-visualization/.git/
[email protected] ~/Desktop/python-data-visualization (master*) $ git add -A

移除多餘的 remote
[email protected] ~/Desktop/python-data-visualization (master*) $ git remote add origin https://github.com/JcOpenSource/python-data-visualization.git
fatal: remote origin already exists.
[email protected] ~/Desktop/python-data-visualization (master*) $ git remote -v
origin  https://github.com/JcOpenSource/python-.git (fetch)
origin  https://github.com/JcOpenSource/python-.git (push)
[email protected] ~/Desktop/python-data-visualization (master*) $ git remote remove origin


[email protected] ~/Desktop/python-data-visualization (master*) $ git remote add origin https://github.com/JcOpenSource/python-data-visualization.git
[email protected] ~/Desktop/python-data-visualization (master*) $ git remote -v 
origin  https://github.com/JcOpenSource/python-data-visualization.git (fetch)
origin  https://github.com/JcOpenSource/python-data-visualization.git (push)


[email protected] ~/Desktop/python-data-visualization (master*) $ git commit -m "add python file"
[master (root-commit) 7c13cce] add python file
 11 files changed, 250 insertions(+)
 create mode 100644 1.py
 create mode 100644 10.py
 create mode 100644 2.py
 create mode 100644 3.py
 create mode 100644 4.py
 create mode 100644 5.py
 create mode 100644 6.py
 create mode 100644 7.py
 create mode 100644 8.py
 create mode 100644 9.py
 create mode 100644 README.md
[email protected] ~/Desktop/python-data-visualization (master) $ git push -u origin master
Enumerating objects: 13, done.
Counting objects: 100% (13/13), done.
Delta compression using up to 4 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (13/13), 4.36 KiB | 4.36 MiB/s, done.
Total 13 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/JcOpenSource/python-data-visualization.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
  • 初始化 git init
  • 新增本地 git add
  • 提交本地管理 git commit -m "
  • 獲得連結 git remote add origin https://github.com/JcOpenSource/p
  • 提交雲端 git push -u origin master