1、在所選擇的地方創建空目錄
$ mkdir my_repository
$ cd my_repository
$ pwd
/users/renlingling/my_repository
2、將創建的目錄變成git可管理的倉庫
$ git init
Initialized empty Git repository in /Users/renlingling/my_repository/.git/
$ ls -ah
. .. .git
3、將創建的文件放入git倉庫
前提工作:創建新文件test.txt
$ touch test.txt
$ ls
test.txt
1⃣️將文件添加git倉庫
$ git add test.txt
2⃣️將文件提交到git倉庫
$ git commit -m 'it is a test file'
[master (root-commit) a91c9ea] it is a test file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
-m:本次提交的說明
1 file changed:1個文件被改動
0 insertions(+):無插入內容
0 deletions(-):無刪減內容
4、查看提交記錄
$ git log --pretty=oneline
4f8982749f919cecad4bccfe06dd12636cc13ca6 (HEAD -> master) add two lines infomation
a91c9ea4b6ac41cd49da3fc8ad2f003e8d895fe3 it is a test file
5、回退上一個版本文件
$ git reset --hard HEAD^
HEAD is now at e3e5693 3
$ cat test.txt
123
456`
PS:HEAD^表示上一個版本;HEAD ^ ^表示上上版本;HEAD~30:表示第前30個版本。
6、回退之後又想回到原先版本
$ git reflog
e3e5693 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
24253d1 HEAD@{1}: commit: 4
e3e5693 (HEAD -> master) HEAD@{2}: commit: 3
a91c9ea HEAD@{3}: reset: moving to HEAD^
4f89827 HEAD@{4}: commit: add two lines infomation
a91c9ea HEAD@{5}: commit (initial): it is a test file
$ git reset --hard 4f89
HEAD is now at 4f89827 add two lines infomation
$ cat test.txt
it is a test file
haha
add two lines infomation
by renlignling
7、查看是否有文件或內容未同步到git倉庫中
$ git status
On branch master
nothing to commit, working tree clean
當我創新一個新的文件之後,
$ touch license1
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
license1
nothing added to commit but untracked files present (use "git add" to track)
$ git add license1
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: license1
$ git commit -m 'this is license1'
[master 7a1f648] this is license1
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 license1
$ git status
On branch master
nothing to commit, working tree clean
8、檢查工作區和git倉庫內版本區別
$ git diff HEAD -- test.txt
diff --git a/test.txt b/test.txt
index 266359e..43e03c0 100644
--- a/test.txt
+++ b/test.txt
@@ -1,4 +1,4 @@
-hhhhhit is a test file
+rrrrrhhhhhit is a test file
haha
add two lines infomation
by renlignling
9、撤銷修改部分
撤銷分爲兩種情況:第一種爲還沒有執行$ git add
,第二種爲執行了$ git add
但是還沒有執行$ git commit
。
1⃣️
$ 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: license
modified: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
上面代碼第五行已經告訴使用 "git checkout -- <file>..."
撤銷工作區的改變。
$ git checkout -- license
$ 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")
2⃣️
$ git add license
renlinglingdeMacBook-Pro:my_repository renlingling$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: license
如上代碼所示,倒數第二行指出 "git reset HEAD <file>..."
改變可以從緩存區中撤銷。
$ git reset HEAD license
Unstaged changes after reset:
M license
$ 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: license
no changes added to commit (use "git add" and/or "git commit -a")
$ git checkout -- license
$ git status
On branch master
nothing to commit, working tree clean
撤銷完成!
10、刪除文件
刪除license1
$ git rm license1
rm 'license1'
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: license1
$ git commit -m 'delete'
[master a554cfe] delete
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 license1
$ git status
On branch master
nothing to commit, working tree clean
如果刪錯的話,就用git checkout -- license1
將誤刪的文件恢復到最新版本。
11、本地庫和遠程庫之間互傳文件
1⃣️本地庫向git傳文件(添加遠程庫)
$ git remote add origin [email protected]:username/my_repository.git
$ git push -u origin master
git push
將當前分支推送到遠程
第一次推送master
分支時,加上了-u
參數,Git不但會把本地的master
分支內容推送的遠程新的master
分支,還會把本地的master
分支和遠程的master
分支關聯起來,在以後的推送或者拉取時就可以簡化命令。
2⃣️將遠程庫的內容克隆到本地
$ git clone 你的git庫地址
12、分支
現在的分支只有master
$ git branch
* master
添加分支dev
,但當前分支依舊爲master
$ git branch dev
$ git branch
dev
* master
將master
分支轉換到dev
分支
$ git checkout dev
Switched to branch 'dev'
$ git branch
* dev
master
在dev
分支中修改的內容,在master
分支中是找不到的。所以可以將dev
分支合併到master
分支上。
$ git merge dev
Updating a554cfe..6287919
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
當dev分支沒用了,就可以刪除
$ git branch -d dev
Deleted branch dev (was 6287919).
將新建分支推送到遠程庫
$ git push origin test
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create pull request for test:
* [new branch] test -> test
小結
查看分支:git branch創建分支:git branch
切換分支:git checkout
創建+切換分支:git checkout -b
合併某分支到當前分支:git merge
刪除分支:git branch -d
13、標籤管理
1⃣️增加標籤
$ git tag v0.9
$ git tag
v0.9
2⃣️刪除標籤
$ git tag -d v0.9
Deleted tag 'v0.9' (was 78c8cd5)
3⃣️查看標籤
$ git show v0.9
commit 78c8cd5ab718054e3f7acba8859b572213fd9aad (HEAD -> master, tag: v0.9, origin/test, origin/master, test)
Author: ...
Date: Tue Apr 23 15:58:08 2019 +0800
master
diff --git a/test.txt b/test.txt
index 8541b37..5c4e091 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
+this is test
hhhhhit is a test file
haha
add two lines infomation
4⃣️推送標籤到遠程庫
(1)推送某個標籤
$ git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
* [new tag] v1.0 -> v1.0
(2)推送全部標籤
$ git push origin --tags
Total 0 (delta 0), reused 0 (delta 0)
* [new tag] v1.2 -> v1.2