guosihan@guosihandeMacBook-Pro gitTUT % git config user.name
Guo Sihan
guosihan@guosihandeMacBook-Pro gitTUT % cat ./gitconfig
cat: ./gitconfig: No such file or directory
guosihan@guosihandeMacBook-Pro gitTUT % cat gitconfig
cat: gitconfig: No such file or directory
guosihan@guosihandeMacBook-Pro gitTUT % git config user.email
727101565@email.com
guosihan@guosihandeMacBook-Pro gitTUT % mkdir project
guosihan@guosihandeMacBook-Pro gitTUT % cd project
guosihan@guosihandeMacBook-Pro project % git init
Initialized empty Git repository in /Users/guosihan/Desktop/gitTUT/project/.git/
guosihan@guosihandeMacBook-Pro project %
这是初始化文件夹
guosihan@guosihandeMacBook-Pro project % cd ..
guosihan@guosihandeMacBook-Pro gitTUT % cd project
guosihan@guosihandeMacBook-Pro project % git config core.editor
guosihan@guosihandeMacBook-Pro project %
这是建立一个内部配置的编译器
guosihan@guosihandeMacBook-Pro project % cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
editor = sublime
配置成功
这样就可以编译项目了:
Git操作有这几个区:
工作区 – 暂存区 – 本地仓库 – 远程仓库
说明:
- 工作区就是程序员写代码的地方
- 前三个都是在自己的主机上,远程仓库是在别人的主机上
- 暂存区是我们写的代码暂存和提交 暂时保存你工作时的一个状态
- 提交到本地仓库 就可以保存或者查看你工作时的各个节点时的状态了
- 也可以从别人的主机上获取代码 别人的主机就相当于远程仓库
git基本命令:
git init
这就是初始化仓库 整个就叫仓库
在某个目录下初始化仓库后会自动产生.git目录 该目录下工作的所有文档 即可以使用gif进行管理
查看分支状态
guosihan@guosihandeMacBook-Pro project % git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
master就是主分支的意思 没有别的就是默认主分支master
git永远希望你的工作区和你的本地仓库是保持一致的
guosihan@guosihandeMacBook-Pro project % echo 'This is a git test' > Readme.txt
guosihan@guosihandeMacBook-Pro project % ls
Readme.txt
guosihan@guosihandeMacBook-Pro project % cat Readme.txt
This is a git test
guosihan@guosihandeMacBook-Pro project % git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
Readme.txt
nothing added to commit but untracked files present (use "git add" to track)
guosihan@guosihandeMacBook-Pro project %
查看状态 但是这么显示还未提交到暂存区
guosihan@guosihandeMacBook-Pro project % git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
Readme.txt
nothing added to commit but untracked files present (use "git add" to track)
guosihan@guosihandeMacBook-Pro project % git add
Nothing specified, nothing added.
Maybe you wanted to say 'git add .'?
guosihan@guosihandeMacBook-Pro project % git add Readme.txt
guosihan@guosihandeMacBook-Pro project % git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: Readme.txt
guosihan@guosihandeMacBook-Pro project %
现在显示已经到暂存区中,所以:
文件的提交
git add [file]
- 将文件提交到暂存区
- 提交内容可以是一个文件 多个文件用空格分开
- 如果是 * 表示所有文件,也可以是目录
git rm --cached Readme.txt
这个的意思是删除暂存区某个文件提交记录
git add *
批量次添加文件到暂存区
文件同步到本地仓库:
guosihan@guosihandeMacBook-Pro project % git commit -m "Add some files"
[master (root-commit) ac8c629] Add some files
1 file changed, 1 insertion(+)
create mode 100644 Readme.txt
guosihan@guosihandeMacBook-Pro project %
- 同步时需要附加一些同步信息 在后面-m “你想附加的内容”
- 所有对工作区的修改如果想同步到本地仓库 都需要 add --> commit
提交完之后会出现干净的工作区:
guosihan@guosihandeMacBook-Pro project % git status
On branch master
nothing to commit, working tree clean
guosihan@guosihandeMacBook-Pro project %
如果产生新的文件 还得必须经过add – commit 才能提交到本地仓库
提交日志的查看,查看comit日志
guosihan@guosihandeMacBook-Pro project % git log
commit ac8c629c9caa21cf57ff4b7daecbebdf13bc1bf4 (HEAD -> master)
Author: Guo Sihan <727101565@email.com>
Date: Tue Mar 24 23:47:43 2020 +0900
Add some files
guosihan@guosihandeMacBook-Pro project %
第二行commit id 这是全球唯一的 一般是用前七位
所以说如果指定ID的话 我们可以跳回到原来此ID时的状态
guosihan@guosihandeMacBook-Pro project % git log --pretty=oneline
ac8c629c9caa21cf57ff4b7daecbebdf13bc1bf4 (HEAD -> master) Add some files
这就是查看单行日志
本地操作:
guosihan@guosihandeMacBook-Pro project % git diff Readme.txt
diff --git a/Readme.txt b/Readme.txt
index 4e3d78a..dc8a350 100644
--- a/Readme.txt
+++ b/Readme.txt
@@ -1 +1,3 @@
This is a git test
+
+你好 我很好 你还好吗
guosihan@guosihandeMacBook-Pro project %
git diff 命令可以展现出工作区和本地仓库之间的文件内容的差异
guosihan@guosihandeMacBook-Pro project % ls
Readme.txt random_test.py
guosihan@guosihandeMacBook-Pro project % rm Readme.txt
guosihan@guosihandeMacBook-Pro project % ls
random_test.py
guosihan@guosihandeMacBook-Pro project % git checkout Readme.txt
Updated 1 path from the index
guosihan@guosihandeMacBook-Pro project % ls
Readme.txt random_test.py
guosihan@guosihandeMacBook-Pro project % cat Readme.txt
This is a git test
guosihan@guosihandeMacBook-Pro project %
如果一个文件不小心被删除了,那么可以用checkout来恢复这个文件,但是只能说本地仓库中什么样子的,恢复的文件就成什么样子,因为是从本地仓库中恢复的。
文件(本地仓库中的)的移动和删除:
guosihan@guosihandeMacBook-Pro project % git mv Readme.txt ftp
guosihan@guosihandeMacBook-Pro project % ls
ftp random_test.py
guosihan@guosihandeMacBook-Pro project % git rm random_test.py
rm 'random_test.py'
guosihan@guosihandeMacBook-Pro project % git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: Readme.txt -> ftp/Readme.txt
deleted: random_test.py
guosihan@guosihandeMacBook-Pro project % git comiit -m 'rm random_test.py'
git: 'comiit' is not a git command. See 'git --help'.
The most similar command is
commit
guosihan@guosihandeMacBook-Pro project % git commit -m 'rm random_test.py'
[master 5cfcd51] rm random_test.py
2 files changed, 6 deletions(-)
rename Readme.txt => ftp/Readme.txt (100%)
delete mode 100644 random_test.py
guosihan@guosihandeMacBook-Pro proje
- 其实用法和mv rm 命令相同。操作后直接commit即可工作区和本地仓库同步
丢弃工作区修改:
git check -- file
版本控制命令:
如何回到之间的版本呢?
guosihan@guosihandeMacBook-Pro project % git reset --hard HEAD^
HEAD is now at 5cfcd51 rm random_test.py
guosihan@guosihandeMacBook-Pro project %
- 说明: HEAD后面的^ ,有几个^ 就代表回到几个的上阶版本
guosihan@guosihandeMacBook-Pro project % git reset --hard 57e0ea8
HEAD is now at 57e0ea8 Add some files
guosihan@guosihandeMacBook-Pro project %
通过这种方法,就可以回到指定的版本,使用commit前七位即可,git reset
可以快速回到指定的版本。
guosihan@guosihandeMacBook-Pro project % git reflog
57e0ea8 (HEAD -> master) HEAD@{0}: reset: moving to 57e0ea8
5cfcd51 HEAD@{1}: reset: moving to HEAD^
0788f61 HEAD@{2}: commit: test.c
5cfcd51 HEAD@{3}: commit: rm random_test.py
57e0ea8 (HEAD -> master) HEAD@{4}: commit: Add some files
ac8c629 HEAD@{5}: commit (initial): Add some files
guosihan@guosihandeMacBook-Pro project %
git reflog
是查看所有的历史版本号,新的操作永远排在上面,老的操作永远排在下面。- 这个命令我们可以看到始终会有所有的操作记录
- HEAD类似于指针的样子 指针指向哪 版本控制就在哪
标签的管理和使用
什么是标签:
- 就是这在当前位置添加快照 保存工作状态 一般用于版本的迭代
- 一般重要的事情完成之后 就会添加一个标签
创建新标签:git tag 1.0
- 默认在最新的commit_id处标签 恢复也是恢复到这个id
- 也可以 -m ‘标签的信息’
查看标签:git tag
- 列出当前标签
- 也可以用show v1.0 显示标签的具体信息 如下:
guosihan@guosihandeMacBook-Pro project % git show v1.0
commit 57e0ea8354c5934c103ea3236c359b2d7aed3d20 (HEAD -> master, tag: v1.0)
Author: Guo Sihan <727101565@email.com>
Date: Wed Mar 25 09:09:41 2020 +0900
Add some files
diff --git a/random_test.py b/random_test.py
new file mode 100644
index 0000000..f964f4d
--- /dev/null
+++ b/random_test.py
@@ -0,0 +1,6 @@
+import random
+
+L = list(range(10))
+print(L)
+random.shuffle(L)
+print(L)
\ No newline at end of file
guosihan@guosihandeMacBook-Pro project %
删除标签: git tag -d v1.0
给指定的ID处打标签:git tag v9.0 [commit_id]
去往某个标签版本:git reset --hard v9.0
guosihan@guosihandeMacBook-Pro project % git reset --hard v9.0
HEAD is now at ac8c629 Add some files
guosihan@guosihandeMacBook-Pro project %
创建保存临时工作区创建: git stash
查看保存的工作区: git stash list
Saved working directory and index state WIP on master: ac8c629 Add some files
guosihan@guosihandeMacBook-Pro project % git stash list
stash@{0}: WIP on master: ac8c629 Add some files
stash@{1}: WIP on master: ac8c629 Add some files
guosihan@guosihandeMacBook-Pro project % git stash apply stash@{1}
error: Your local changes to the following files would be overwritten by merge:
Readme.txt
Please commit your changes or stash them before you merge.
Aborting
guosihan@guosihandeMacBook-Pro project % git stash apply stash@{0}
error: Your local changes to the following files would be overwritten by merge:
Readme.txt
Please commit your changes or stash them before you merge.
Aborting
guosihan@guosihandeMacBook-Pro project %
- 这里应用哪个工作区,就选择哪个工作区。 最
- 新的在最上面 最老的在最下面。
删除工作区: git stash drop stash@{}
还有删除所有的命令: git stash clear