*為什麼使用git
*安裝git
*git基本操作
*log與diff
*回到從前(reset)
*回到從前(checkout針對單個文件)
*分支(branch)
*merge分支衝突
*rebase分支衝突
*臨時修復(stash)
*Github
*下載github檔案
*切換git bash目錄
*git教學網站
=================================
*為什麼使用git?
Git版本管理#1為什麼使用git? (教學教程教程)
https://www.youtube.com/watch?v=kQSzft2Jj8Y&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-
https://morvanzhou.github.io/tutorials/others/git/
=================================
*安裝git:
Git版本管理#2安裝(教學教程教程)
https://www.youtube.com/watch?v=BMKlINSwEas&index=2&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-
https://morvanzhou.github.io/tutorials/others/git/1-2-install/
=================================
*git基本操作:
Git版本管理#3第一個版本管理庫(教學教程教程)
https://www.youtube.com/watch?v=obbH1hGB5GI&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-&index=3
https://morvanzhou.github.io/tutorials/others/git/2-1-repository/
-----------------------------
*第一個版本庫
創建版本庫(init)
添加文件管理(add)
提交改變(commit)
流程圖
-----------------------------
*建立資料夾
git2
*切換git bash目錄
cd /d/git2
*設定name和email
git config --global user.name "eric"
git config --global user.email "eric@email.com"
*查name和email
git config user.name
git config user.email
*初始化git管理庫
切換到要設定git的目錄
git init
*查檔案
ls -al
*新增空白檔案
touch 1.txt
------------------
*查git管理庫狀態
git status
訊息:
Untracked files
未加入git管理庫的檔案
Changes to be committed
這個修改(檔案)已經可以被提交
------------------
*添加檔案到git管理庫
git add 1.txt
*添加全部檔案到git管理庫
git add .
*提交檔案
git commit -m "create 1.txt"
=================================
*log與diff:
Git版本管理#4記錄修改(log&diff)(教學教程教程)
https://www.youtube.com/watch?v=qSql8KDTEuY&index=4&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-
https://morvanzhou.github.io/tutorials/others/git/2-2-modified/
------------------
*打開1.txt檔案並加入:
a = 1
*查git管理庫狀態
git status
顯示1.txt檔案為modified狀態:
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: 1.txt
no changes added to commit (use "git add" and/or "git commit -a")
*添加檔案到git管理庫
git add 1.txt
*提交檔案
git commit -m "change 1"
*查log
git log
------------------
查檔案更改的細節:
*將1.txt做第2次修改
a = 2
b = 1
*查git管理庫狀態(-s為用縮寫的方式顯示訊息)
git status -s
顯示:(M表示modified狀態)
M 1.txt (註: M 為紅色)
*查目前與之前版本內容的不同
git diff
顯示:
diff --git a/1.txt b/1.txt
index d25d49e..61ce15f 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
-a = 1 (註: - 表示刪除)
\ No newline at end of file
+a = 2 (註: + 表示增加)
+b = 1 (註: + 表示增加)
\ No newline at end of file
*add全部的改變
git add .
*查git管理庫狀態(-s為用縮寫的方式顯示訊息)
git status -s
顯示:(M表示modified狀態)
M 1.txt (註: M 為綠色:表示可以被commit)
*git add後,用 git diff 會顯示不出與上一個版本的不同處
要用 git diff --cached 才能看出差異
------------------
同時觀察staged狀態與unstaged狀態:
*將1.txt做第3次修改(加入c = b)
a = 2
b = 1
c = b
*查git管理庫狀態(-s為用縮寫的方式顯示訊息)
git status -s
顯示:(M表示modified狀態)
MM 1.txt (註: 第1個M為綠色,第2個M為紅色)
第1個M為staged狀態(已執行add動作)
第2個M為unstaged狀態(未執行add動作)
*同時顯示staged狀態與unstaged狀態
git diff HEAD
顯示:
diff --git a/1.txt b/1.txt
index d25d49e..ac13cf6 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,3 @@
-a = 1 (註: 已staged)
\ No newline at end of file
+a = 2 (註: 已staged)
+b = 1 (註: 已staged)
+c = b (註: 未staged)
\ No newline at end of file
*只顯示staged狀態
git diff --cached
顯示:
diff --git a/1.txt b/1.txt
index d25d49e..61ce15f 100644
--- a/1.txt
+++ b/1.txt
@@ -1 +1,2 @@
-a = 1
\ No newline at end of file
+a = 2
+b = 1
\ No newline at end of file
*只顯示unstaged狀態
git diff
顯示:
diff --git a/1.txt b/1.txt
index 61ce15f..ac13cf6 100644
--- a/1.txt
+++ b/1.txt
@@ -1,2 +1,3 @@
a = 2
-b = 1
\ No newline at end of file
+b = 1
+c = b
\ No newline at end of file
*執行
git add .
git commit -m "change 2"
=================================
*回到從前(reset):
Git版本管理#5回到從前(重置)(教學教程教程)
https://www.youtube.com/watch?v=RhJLfEJV36w&index=5&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-
https://morvanzhou.github.io/tutorials/others/git/3-1-reset/
-----------------------
git log --oneline
顯示有3個訊息被commit:
b5407c4 (HEAD -> master) change 2
e746d23 change 1
98f9ff1 create 1.txt
*修改已 commit 的版本:
檔案已經commit了卻發現在這個commit
中忘了附上另一個檔案。
處理方式:
複製1.txt 這個文件, 改名為2.txt(要加入的文件)
並把2.txt 變成staged, 然後使用--amend 將這次改變合併
到之前的change 2 中。
git add 2.txt
git commit --amend --no-edit #"--no-edit": 不編輯, 直接合併到上一個 commit
git log --oneline #"--oneline": 每個 commit 內容顯示在一行
輸出:
27e4452 (HEAD -> master) change 2 #合併過的 change 2(id由b5407c4變為27e4452)
e746d23 change 1
98f9ff1 create 1.txt
------------------
------------------
*reset 回到 add 之前:
已經add的檔案,想補充一些內容再add,
如何讓此檔案回到未add狀態,
補充一些內容再add ?
先在1.txt加入一行資料:
d = 3
將1.txt add:
git add 1.txt
查狀態:
git status
顯示: (staged狀態)
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: 1.txt
-----------------
讓此檔案回到未add狀態:
git reset 1.txt
顯示:
Unstaged changes after reset:
M 1.txt
-----------------
查狀態:
git status
顯示: (unstaged狀態)
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: 1.txt
no changes added to commit (use "git add" and/or "git commit -a")
-----------------
-----------------
*reset 回到 commit 之前:
git log --oneline
顯示:
27e4452 (HEAD -> master) change 2
e746d23 change 1
98f9ff1 create 1.txt
git add 1.txt
git status
顯示:
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: 1.txt
回到上一次的 commit:
git reset --hard HEAD
顯示:
HEAD is now at 27e4452 change 2
git status
顯示:
On branch master
nothing to commit, working tree clean
*回到 e746d23 change 1
方法1:
git reset --hard HEAD^
方法2:
git reset --hard e746d23
git log --oneline
顯示:
e746d23 (HEAD -> master) change 1
98f9ff1 create 1.txt
查id:
git reflog
顯示:
e746d23 (HEAD -> master) HEAD@{0}: reset: moving to e746d23
27e4452 HEAD@{1}: reset: moving to HEAD
27e4452 HEAD@{2}: reset: moving to HEAD
27e4452 HEAD@{3}: commit (amend): change 2
b5407c4 HEAD@{4}: commit: change 2
e746d23 (HEAD -> master) HEAD@{5}: commit: change 1
98f9ff1 HEAD@{6}: commit (initial): create 1.txt
*回到 27e4452 HEAD@{3}: commit (amend): change 2
git reset --hard 27e4452
git log --oneline
顯示:
27e4452 (HEAD -> master) change 2
e746d23 change 1
98f9ff1 create 1.txt
=================================
*回到從前(checkout針對單個文件):
Git版本管理#6回到從前(checkout)(教學教程教程)
https://www.youtube.com/watch?v=QEuqlpMOL9E&index=6&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-
https://morvanzhou.github.io/tutorials/others/git/3-2-checkout/
-----------------------
git log --oneline
顯示:
27e4452 (HEAD -> master) change 2
e746d23 change 1
98f9ff1 create 1.txt
1.txt回到之前版本的內容:
git checkout e746d23 -- 1.txt
在1.txt加入資料:
#I went back to change 1
git add 1.txt
git commit -m "back to change 1 and add comment for 1.txt"
git log --oneline
顯示:
4163907 (HEAD -> master) back to change 1 and add comment for 1.txt
27e4452 change 2
e746d23 change 1
98f9ff1 create 1.txt
=================================
*分支(branch):
Git版本管理#7分支(branch)(教學教程教程)
https://www.youtube.com/watch?v=68CMwz3wMRE&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-&index=7
https://morvanzhou.github.io/tutorials/others/git/4-1-branch/
--------------------------------
git log --oneline --graph
顯示:(有*號)
* 4163907 (HEAD -> master) back to change 1 and add comment for 1.txt
* 27e4452 change 2
* e746d23 change 1
* 98f9ff1 create 1.txt
git log --oneline
顯示:(沒有*號)
4163907 (HEAD -> master) back to change 1 and add comment for 1.txt
27e4452 change 2
e746d23 change 1
98f9ff1 create 1.txt
建立分支:
git branch dev
查分支:
git branch
顯示:(目前在master分支)
dev
* master
切換到dev分支:
git checkout dev
顯示:
Switched to branch 'dev'
*刪除dev分支:
1.先切換到master分支
git checkout master
2.再刪除dev分支
git branch -d dev
顯示:
Deleted branch dev (was 4163907).
*用另一種方法建立分支:(建立分支後,會切換到新分支)
git checkout -b dev
顯示:
Switched to a new branch 'dev'
git branch
顯示:
* dev
master
*在1.txt檔案中加入下列資料:
#I was changed in dev branch
*add+commit:
git commit -am "change 3 in dev"
顯示:
[dev 3101d1c] change 3 in dev
1 file changed, 1 insertion(+)
git checkout master
顯示:
Switched to branch 'master'
此時1.txt的內容不會有下列資料:
#I was changed in dev branch
因為已經切換到master分支
*把開發版(dev)推到正式版(master)
要確定已切換到正式版(master)
git branch
顯示:
dev
* master
git merge --no-ff -m "keep merge info" dev
顯示:
Merge made by the 'recursive' strategy.
1.txt | 1 +
1 file changed, 1 insertion(+)
*查log:
git log --oneline --graph
顯示:
* d128079 (HEAD -> master) keep merge info
|\
| * 3101d1c (dev) change 3 in dev
|/
* 4163907 back to change 1 and add comment for 1.txt
* 27e4452 change 2
* e746d23 change 1
* 98f9ff1 create 1.txt
=================================
*merge分支衝突:
Git 版本管理 #8 分支衝突 (merge) (教學 教程 tutorial)
https://www.youtube.com/watch?v=gN0E9SQmySw&index=8&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-
https://morvanzhou.github.io/tutorials/others/git/4-2-merge-conflict/
要先下載檔案:
https://morvanzhou.github.io/static/results/git/initial-files/for_gitTUT_4-2.zip
----------------------------
git branch
顯示:(目前在master分支)
dev
* master
1.py內容:(最後一行是 # edited in master)
a = 1
# I went back to change 1
# edited in master
切換到dev分支:
git checkout dev
1.py內容:(最後一行是 # edited in dev)
a = 1
# I went back to change 1
# edited in dev
--------------------
切換到master分支:
git checkout master
查master分支的log:
git log --oneline --graph
顯示:(第一行與dev不同)
* 3d7796e (HEAD -> master) change 4 in master
* 47f167e back to change 1 and add comment for 1.py
* 904e1ba change 2
* c6762a1 change 1
* 13be9a7 create 1.py
--------------------
切換到dev分支:
git checkout dev
查dev分支的log:
git log --oneline --graph
顯示:(第一行與master不同)
* f7d2e3a (HEAD -> dev) change 3 in dev
* 47f167e back to change 1 and add comment for 1.py
* 904e1ba change 2
* c6762a1 change 1
* 13be9a7 create 1.py
--------------------
切換到master分支:
git checkout master
合併分支:(merge)
git merge dev
顯示:(有衝突 CONFLICT)
Auto-merging 1.py
CONFLICT (content): Merge conflict in 1.py
Automatic merge failed; fix conflicts and then commit the result.
1.py內容:
a = 1
# I went back to change 1
<<<<<<< HEAD
# edited in master
=======
# edited in dev
>>>>>>> dev
--------------------
解決衝突:(用人工合併)
改1.py內容:(第3行)
a = 1
# I went back to change 1
# edited in master and dev
--------------------
提交檔案:
git commit -am "solve conflict"
顯示:(衝突已解決)
[master 88b2084] solve conflict
查log:
git log --oneline --graph
顯示:
* 88b2084 (HEAD -> master) solve conflict
|\
| * f7d2e3a (dev) change 3 in dev
* | 3d7796e change 4 in master
|/
* 47f167e back to change 1 and add comment for 1.py
* 904e1ba change 2
* c6762a1 change 1
* 13be9a7 create 1.py
=================================
*rebase分支衝突:
Git 版本管理 #9 分支衝突 (rebase) (教學 教程 tutorial)
https://www.youtube.com/watch?v=fLvr4BRoJ8I&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-&index=9
https://morvanzhou.github.io/tutorials/others/git/4-3-rebase/
----------------
git log --oneline --graph
顯示:
* 3d7796e (HEAD -> master) change 4 in master
* 47f167e back to change 1 and add comment for 1.py
* 904e1ba change 2
* c6762a1 change 1
* 13be9a7 create 1.py
git checkout dev
顯示:
Switched to branch 'dev'
git log --oneline --graph
顯示:
* f7d2e3a (HEAD -> dev) change 3 in dev
* 47f167e back to change 1 and add comment for 1.py
* 904e1ba change 2
* c6762a1 change 1
* 13be9a7 create 1.py
git checkout master
顯示:
Switched to branch 'master'
------------------------
git rebase dev
顯示:
First, rewinding head to replay your work on top of it...
Applying: change 4 in master
error: Failed to merge in the changes.
Using index info to reconstruct a base tree...
M 1.py
Falling back to patching base and 3-way merge...
Auto-merging 1.py
CONFLICT (content): Merge conflict in 1.py
Patch failed at 0001 change 4 in master
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
------------------------
git branch
顯示:
* (no branch, rebasing master)
dev
master
git add 1.py
git rebase --continue
顯示:
Applying: change 4 in master
git log --oneline --graph
顯示:
* 60de8b1 (HEAD -> master) change 4 in master
* f7d2e3a (dev) change 3 in dev
* 47f167e back to change 1 and add comment for 1.py
* 904e1ba change 2
* c6762a1 change 1
* 13be9a7 create 1.py
=================================
*臨時修復(stash):
Git 版本管理 #10 臨時修復 (stash) (教學 教程 tutorial)
https://www.youtube.com/watch?v=mqjAMciBrdU&index=10&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-
https://morvanzhou.github.io/tutorials/others/git/4-4-stash/
-------------
git branch
顯示:
dev
* master
-------------
1.py內容:
a = 1
# I went back to change 1
# edited in master and dev
-------------
git checkout dev
顯示:
Switched to branch 'dev'
-------------
1.py內容:
a = 1
# I went back to change 1
# edited in dev
-------------
1.py內容:(加入 # feel happy)
a = 1
# I went back to change 1
# edited in dev
# feel happy
-------------
git status -s
顯示:
M 1.py
-------------
將dev分支的檔案1.py放到暫存區:
git stash
顯示:
Saved working directory and index state WIP on dev: f7d2e3a change 3 in dev
-------------
git status
顯示:
On branch dev
nothing to commit, working tree clean
-------------
建立新分支:
git checkout -b boss
顯示:
Switched to a new branch 'boss'
-------------
1.py內容:(加入 # lovely boss)
a = 1
# I went back to change 1
# edited in dev
# lovely boss
-------------
提交檔案1.py:
git commit -am "job from boss"
顯示:
[boss e14cb20] job from boss
1 file changed, 1 insertion(+)
-------------
merge分支boss到master:
*切換到master:
git checkout master
顯示:
Switched to branch 'master'
*1.py內容:(未merge)
a = 1
# I went back to change 1
# edited in master and dev
*執行merge:
git merge --no-ff -m "merged boss job" boss
顯示:(有衝突)
Auto-merging 1.py
CONFLICT (content): Merge conflict in 1.py
Automatic merge failed; fix conflicts and then commit the result.
-------------
*1.py內容:(已merge,但有衝突)
a = 1
# I went back to change 1
<<<<<<< HEAD
# edited in master and dev
=======
# edited in dev
# lovely boss
>>>>>>> boss
-------------
*修改1.py如下:
a = 1
# I went back to change 1
# edited in master and dev
# lovely boss
-------------
提交檔案1.py:
git commit -am "solve conflict"
顯示:
[master 9c83e91] solve conflict
-------------
git log --oneline --graph
顯示:(merge完成)
* 9c83e91 (HEAD -> master) solve conflict
|\
| * e14cb20 (boss) job from boss
* | 2d1961f change 4 in master
|/
* f7d2e3a (dev) change 3 in dev
* 47f167e back to change 1 and add comment for 1.py
* 904e1ba change 2
* c6762a1 change 1
* 13be9a7 create 1.py
-------------
從暫存區拿回原本的檔案:
*切換到dev:
git checkout dev
顯示:
Switched to branch 'dev'
*刪除boss分支:(任務已完成,可已刪除掉分支)
git branch -D boss
顯示:
Deleted branch boss (was e14cb20).
*從暫存區拿回原本的檔案:
git stash pop
顯示:
On branch dev
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: 1.py
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (7d1010335a20efd0edfc9d19d024ca056965a136)
*1.py內容:
a = 1
# I went back to change 1
# edited in dev
# feel happy
=================================
*Github:
Git版本管理#11 Github(教學教程教程)
https://www.youtube.com/watch?v=oWUvXRheaeY&list=PLXO45tsB95cKysjmSNln65YoUt9lwEl7-&index=11
https://morvanzhou.github.io/tutorials/others/git/5-1-github/
https://github.com/
https://github.com/ericgame/
----------------
*在github新增repository:
按右上角 + 號,選擇 New repository
Repository name: git-demo
Description: demo for the usage of github
按 Create repository
----------------
repository網址:
https://github.com/ericgame/git-demo.git
----------------
設定本地(自己的電腦) repository:
D:\gitTUT
切換目錄:
cd /d/gitTUT
----------------
複製並執行github的網頁提示:
把本地repository資料push到github repository
(…or push an existing repository from the command line)
git remote add origin https://github.com/ericgame/git-demo.git
git push -u origin master
註:origin是指github的repository
顯示:
Counting objects: 24, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (17/17), done.
Writing objects: 100% (24/24), 2.01 KiB | 0 bytes/s, done.
Total 24 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To https://github.com/ericgame/git-demo.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
----------------
*刷新網頁即可看到已經上傳的資料
目前只有上傳master分支的資料
----------------
*上傳dev分支的資料:
git push -u origin dev
----------------
*在github查檔案的版本與內容差異:
1.點選檔案
2.按右邊的"History"
----------------
*在1.py增加1行字並上傳github:
git commit -am "change 5"
git push -u origin dev
=================================
*下載github檔案:
git clone https://github.com/ericgame/git-demo.git
=================================
*切換git bash目錄:
cd /d/git2
cd /d/gitTUT
=================================
*git教學網站:
Git 與 Github 版本控制基本指令與操作入門教學
https://blog.techbridge.cc/2018/01/17/learning-programming-and-coding-with-python-git-and-github-tutorial/
github基本教學 - 從無到有
https://www.youtube.com/watch?v=py3n6gF5Y00
連猴子都能懂的Git入門指南
https://backlog.com/git-tutorial/tw/intro/intro2_4.html
沒有留言:
張貼留言