1、指定用户名和邮箱
git config --global user.name "accountName"
git config --global user.email "xxx@yyy.com"
--global
参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。
2、创建版本仓库 git init
//切换到目标目录上
D:\workspace\myRepository>
D:\workspace\myRepository>git init
Initialized empty Git repository in D:/workspace/myRepository/.git/
myRepository
目录下多了一个.git
的目录,这个目录是Git来跟踪管理版本库的,不要轻易删除
3、git add
在myRepository
目录下创建readme.txt文件,然后执行如下命令
readme.txt
Git is a version control system.
Git is free software.
git add readme.txt
执行上面的命令,没有任何显示,说明添加成功。
4、git commit 提交到本地仓库
//git commit -m "wrote a readme.txt"
D:\workspace\myRepository>git commit -m "wrote a readme.txt"
[master (root-commit) 952c723] wrote a readme.txt
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
-m
后面输入的是本次commit
的说明
1 file changed
:1个文件被改动(添加了readme.txt文件)
2 insertions
:添加了两行内容(readme.txt有两行内容)
5、git status 查看当前仓库状态
修改readme.txt 文件内容如下:
readme.txt
Git is a distributed version control system.
Git is free software.
//git status
D:\workspace\myRepository>git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt(这里颜色是红色)
no changes added to commit (use "git add" and/or "git commit -a")
readme.txt被修改过了,但还没有准备提交的修改。
6、git diff <file> 查看指定文件被修改的地方
//git diff readme.txt
D:\workspace\myRepository>git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 7950452..013b5bc 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
-Git is a version control system.
\ No newline at end of file
+Git is a distributed version control system.
+Git is free software.
\ No newline at end of file
7、git log 显示从最近到最远的提交日志
如下代表提交两次的内容和日志
//git log
D:\workspace\myRepository>git log
commit xxxxx7f8f8628a306f09dbc1e053079xxxx(HEAD -> master)
Author: hah <xxx@xxx.com>
Date: Sat Nov 30 22:02:07 2024 +0800
append GPL
commit xxxx2359417e4ad333ea58xxxxxxxx
Author: hah <xxx@xxx.com>
Date: Sat Nov 30 00:10:44 2024 +0800
wrote a readme.txt
git log --pretty=oneline
输出的日志更加整洁
// git log --pretty=oneline
D:\workspace\myRepository> git log --pretty=oneline
xxxx7f8f8628a306f09dbc1e05307xxxxxx (HEAD -> master) append GPL
xxxx59417e4ad333ea58d4a9769a5xxxx wrote a readme.txt
8、git reset 版本回退
在Git中,用HEAD
表示当前版本,也就是最新的提交375ee4e...
(这里是我的提交ID),上一个版本就是HEAD"^"
,上上一个版本就是HEAD"^^"
,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100
//git reset --hard HEAD^
//查看提交日志
D:\workspace\myRepository>git log --pretty=oneline
375ee4e7f8f8628a306f09dbc1e053079605221c (HEAD -> master) append GPL
952c72359417e4ad333ea58d4a9769a5f1f899fa wrote a readme.txt
D:\workspace\myRepository>git reset --hard HEAD"^"
HEAD is now at 952c723 wrote a readme.txt
//再次产看提交日志
D:\workspace\myRepository>git log --pretty=oneline
952c72359417e4ad333ea58d4a9769a5f1f899fa (HEAD -> master) wrote a readme.txt
--hard
会回退到上个版本的已提交状态,--soft
会回退到上个版本的未提交状态,--mixed
会回退到上个版本已添加但未提交的状态。
//git reset --hard commitId...
D:\workspace\myRepository>git reset --hard 375ee4e7f
HEAD is now at 375ee4e append GPL
//查看日志以后发现又灰太狼还会回来,what fuck ??
D:\workspace\myRepository>git log --pretty=oneline
375ee4e7f8f8628a306f09dbc1e053079605221c (HEAD -> master) append GPL
952c72359417e4ad333ea58d4a9769a5f1f899fa wrote a readme.txt
以上前提必须要知道commitId
,但是我不知道怎么办呢??(⊙o⊙)?
Git提供了一个命令git reflog
用来记录你的每一次命令
9、git reflog 查看每一次git命令
//git reflog
D:\workspace\myRepository>git reflog
375ee4e (HEAD -> master) HEAD@{0}: reset: moving to 375ee4e7f
952c723 HEAD@{1}: reset: moving to HEAD^
375ee4e (HEAD -> master) HEAD@{2}: commit: append GPL
952c723 HEAD@{3}: commit (initial): wrote a readme.txt
10、工作区和暂存区
版本库(Repository)
工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage
(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master
,以及指向master
的一个指针叫HEAD
把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用
git add
把文件添加进去,实际上就是把文件修改添加到暂存区;第二步是用
git commit
提交更改,实际上就是把暂存区的所有内容提交到当前分支。
现在,在工作区新增一个LICENSE.txt,git status
查看文件状态,发现新增的LICENSE.txt
状态是Untracked
D:\workspace\myRepository>git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
LICENSE.txt
no changes added to commit (use "git add" and/or "git commit -a")
LICENSE.txt和readme.txt add后如下:[图片上传中...(repo.png-bd8d0e-1732980223052-0)]
D:\workspace\myRepository>git add LICENSE.txt
D:\workspace\myRepository>git add readme.txt
D:\workspace\myRepository>git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: LICENSE.txt
modified: readme.txt
暂存区的状态就变成这样如下:
一旦提交后,如果你又没有对工作区做任何修改,那么工作区就是“干净”的:
D:\workspace\myRepository>git commit -m "understand how stage works"
[master d8a8127] understand how stage works
2 files changed, 3 insertions(+), 1 deletion(-)
create mode 100644 LICENSE.txt
D:\workspace\myRepository>git status
On branch master
nothing to commit, working tree clean
现在版本库变成了这样:
11、 撤销修改
- 撤销工作区的修改
git checkout -- file
修改readme.txt 内容如下
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
D:\workspace\myRepository>git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
D:\workspace\myRepository>git checkout -- readme.txt
D:\workspace\myRepository>git status
On branch master
nothing to commit, working tree clean
命令git checkout -- readme.txt
意思就是,把readme.txt
文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
一种是readme.txt
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态,即add到暂存区的内容存在,但是工作区的修改被撤销。
总之,就是让这个文件回到最近一次git commit
或git add
时的状态。
现在,看看readme.txt
的文件内容:
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.
My stupid boss still prefers SVN.
已经添加到暂存区后,又作了修改,如下:
D:\workspace\myRepository>git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
D:\workspace\myRepository>git add readme.txt
D:\workspace\myRepository>git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
D:\workspace\myRepository>git checkout -- readme.txt
D:\workspace\myRepository>git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
- 把暂存区恢复到工作区
git reset HEAD readme.txt
D:\workspace\myRepository>git add readme.txt
D:\workspace\myRepository>git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: readme.txt
D:\workspace\myRepository>git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
D:\workspace\myRepository>git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")