git的简单使用:
本地git库的创建和使用
- git库的创建:cd进入创建git库的文件夹输入
git init
完成git库的创建,此时该文件夹下会多出.git文件,请不要改动此文件,以避免不必要的麻烦。
- 添加文件至git仓库:(首先在git文件夹中写好代码)输入:
git add 文件名
现在文件进入待提交状态。
- 文件提交:输入:
git commit -m "提交备注"
其中备注为必填项
另:若是第一次提交git代码,会出现报错:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'leo@leo-NB50TJ1-TK1.(none)')
这时需要输入用户名和邮箱名,但git并不会发验证码进行检查。
git config --global user.name "Your name"
git config --global user.email "Your e-mail"
提交成功后会反馈提交信息:
[master (root-commit) 19764e5] Beginning
2 files changed, 9 insertions(+)
create mode 100644 neuron.py
create mode 100644 read.md
显示我提交了两个文件:neuron.py and read.md
删除git中的文件
既然添加文件需要先add,后commit,删除文件自然也需要一些仪式感的呀:
git rm filename
git commit -m "Delet xxxx file"
本地git库的操作:
- 查看git库的状态:
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: read.md
no changes added to commit (use "git add" and/or "git commit -a")
提醒您change了一个文件,需要进行提交。
- 查看文件有哪些改变:(不支持二进制文件)
git diff
显示如下:
diff --git a/read.md b/read.md
index fe26048..6a4d7a7 100644
--- a/read.md
+++ b/read.md
@@ -1,4 +1,3 @@
# This is a test neuron system
* It can recognize numbers from 0 to 9.
* It's only a test program.
-* I wanna add a new line to test my git.
表明我删除了read.md的最后一行。
- 查看git所有版本:
git log
显示如下:
commit 0f3d7f1a563be027909541b29d6be9fd35eca668
Author: confidentOH <confidentoh@foxmail.com>
Date: Tue Feb 26 12:27:54 2019 +0800
return
commit e3982a9629e62047b727dd3c06fea2f5e7593f44
Author: ceng yangqing <confidentoh@foxmail.com>
Date: Mon Feb 25 23:56:29 2019 +0800
append a line
commit 19764e542495c7241fd3de7b6f7655b2eec7f175
Author: ceng yangqing <confidentoh@foxmail.com>
Date: Mon Feb 25 22:44:09 2019 +0800
Beginning
上图我们可以看到,git会显示个版本提交人和邮箱地址以及提交时间。
- 那如果我们想坐一坐时光机,回退至以前的某一版本呢?我们可以这样,先找到那个版本的commit id,然后输入:(例如)
git reset --hard 19764e542495c7241fd3de7b6f7655b2eec7f175
显示如下:
HEAD is now at 19764e5 Beginning
然后再次查看版本库:显示如下:
commit 19764e542495c7241fd3de7b6f7655b2eec7f175
Author: ceng yangqing <confidentoh@foxmail.com>
Date: Mon Feb 25 22:44:09 2019 +0800
Beginning
也就是说,跳过的版本消失了!如果操作失误,也不要急,只要你的命令行没有关,可以找到之前的id跳回未来
git reset --hard 0f3d7f1a563be027909541b29d6be9fd35eca668
再次:git log
,显示如下:
commit 0f3d7f1a563be027909541b29d6be9fd35eca668
Author: confidentOH <confidentoh@foxmail.com>
Date: Tue Feb 26 12:27:54 2019 +0800
return
commit e3982a9629e62047b727dd3c06fea2f5e7593f44
Author: ceng yangqing <confidentoh@foxmail.com>
Date: Mon Feb 25 23:56:29 2019 +0800
append a line
commit 19764e542495c7241fd3de7b6f7655b2eec7f175
Author: ceng yangqing <confidentoh@foxmail.com>
Date: Mon Feb 25 22:44:09 2019 +0800
Beginning
又回来啦!
- git的超级后悔药:
git reflog
如果,你已经关掉了命令行,甚至关掉电脑后,突然后悔了,想退回去怎么办。
这条命令行会显示你做的所有命的记录:输入
git reflog
输出:
0f3d7f1 HEAD@{0}: reset: moving to 0f3d7f1a563be027909541b29d6be9fd35eca668
19764e5 HEAD@{1}: reset: moving to 19764e542495c7241fd3de7b6f7655b2eec7f175
0f3d7f1 HEAD@{2}: commit: return
e3982a9 HEAD@{3}: commit: append a line
19764e5 HEAD@{4}: commit (initial): Beginning
- git 撤销工作区的修改
git checkout -- filename
例如:
leo@leo-NB50TJ1-TK1:~/python/neuron$ 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: read.md
no changes added to commit (use "git add" and/or "git commit -a")
leo@leo-NB50TJ1-TK1:~/python/neuron$ git checkout -- read.md
leo@leo-NB50TJ1-TK1:~/python/neuron$ git status
On branch master
nothing to commit, working directory clean
leo@leo-NB50TJ1-TK1:~/python/neuron$
可见,使用git checkout -- filename
后,之前更改待提交的信息消失了,且文件回到了上次提交时的版本。
- 清除暂存区的某一git文件版本:(名称为filename)
git reset HEAD filename