Git使用

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章