简单介绍
Git官网:https://git-scm.com/
想学习Git,个人建议有Linux的基础,因为Git本来就是Linux大牛所创造的,所以大部分命令都与Linux有关,它是一个分布式版本控制系统(DVCS)。
Git GitHub GitLab介绍
Git是一个版本控制软件
GitHub与GitLab都是用于管理版本的服务端软件
GitH提供免费服务(代码需要公开)及付费服务(代码私有化)
GitLab用于在企业内部管理Git版本库,功能上类似于GitHub
文件的三种状态
已修改(modified)
已缓存(staged)
已提交(committed)
Git安装
Linux(Ubantu)
$ sudo apt install git-all
Centos或RHEL
$ sudo dnf install git-all
Mac
安装命令行工具(如已安装Xcode,命令行工具会在首次启动Xcode时提示安装)
在 Mac 上安装 Git 有多种方式。 最简单的方法是安装 Xcode Command Line Tools。 Mavericks (10.9) 或更高版本的系统中,在 Terminal 里尝试首次运行 git 命令即可。
$ git --version
homebrew
macports
Windows
通过msysGit:https://git-scm.com/
建议使用Git命令行,方便快捷简单,GUI繁琐个人感觉花里胡哨,本人是在Windows10下进行的Git,安装完Git后,直接右键就可以看到一个Git Bash here
创建你的第一个本地厂库
我是在e盘下创建了一个Gitfile的文件目录,你们可以直接Git命令行mkdir,然后cd到目录下,先初始化这文件目录为master,命令:
$ git init
就能看到此文件目录为master了,可以通过命令查看到隐藏文件,一个名叫.git的文件
如果我们使用命令删除.git
文件,就会发现Gitfile文件不再是master文件
$ rm -rf .git
可以通过命令查看.git
文件下有许多文件,这些文件代表着你的Gitfile文件是由Git所管理的
$ cd .git
$ ls -al
我们回到上一目录,在Gitfile目录下创建一个名为test.txt
文件
$ vim test.txt
然后在test.txt
文件中写入hello world
,并使用cat命令查看
$ cat test.txt
这时候就有个重要命令了,此命令可以查看到Git文件的状态,命令就是:
$ git status
输入命令后可以查看到:
Administrator@8_60 MINGW64 /e/Gitfile (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
这句话翻译过来的意思就是:在master分支上,还有没提交,还未追踪到的文件test.txt
,没有任何提交,单出现了未追踪的文件,使用git add
git add
这个命令就是,将你工作目录中修改的文件放入到缓存区中,我们使用git add
将test.txt
放入缓存区
$ git add test.txt
再次用git status
查看
Administrator@8_60 MINGW64 /e/Gitfile (master)
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test.txt
就已经显示test.txt
已经提交到了缓存区,并且也给出了使用git rm --chached
命令把test.txt
从缓存区删除,放回到工作区,使用命令:
$ git rm --cached test.txt
再次使用命令查看Git文件状态
$ git status
接下来我们就要将缓存区里的文件提交到本机的版本库中
对于user.name
与user.email
来说,有3个地方可以设置
1./etc/gitconfig
(几乎不会使用),命令:
$ git config --system
2.~/.gitconfig
(很常用),命令:
$ git config --global
3.针对特定项目的,命令:
$ git config --local
这三条命令的优先级是从低到高,例如我使用第三条命令,再cat /.git/config
显示如下:
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile/.git (GIT_DIR!)
$ cat config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = YQHP-YuKi
email = ********@qq.com
首先是需要记载你的email与提交人的名字
$ git config --global user.email "**************@qq.com"
$ git config --global user.name "YQHP-YuKi"
而git commit
这个命令是将你的缓存区里面的所有文件都提交到本机版本库,-m
后面加注释
,例:
Administrator@8_60 MINGW64 /e/Gitfile (master)
$ git commit -m "this is my first commit test.txt"
[master (root-commit) 3ac93fd] this is my first commit test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
翻译:一个文件已修改,增加了1行
我再次输入git status
,发现已经提交成功
Administrator@8_60 MINGW64 /e/Gitfile (master)
$ git status
On branch master
nothing to commit, working tree clean
使用git log
就可以查看到以往的提交信息了
Administrator@8_60 MINGW64 /e/Gitfile (master)
$ git log
commit 3ac93fdea5963ec35e30f5e116f82bb3e63687e4 (HEAD -> master)
Author: YQHP <***********@qq.com>
Date: Wed Nov 11 11:56:30 2020 +0800
this is my first commit test.txt
注:上方的那一串数字是Git的提交id(commit id),是一个摘要值,这个摘要实际上是一个sha1计算出来的值
然后我继续vim test.txt
在下方添加I am YQHP-YuKi
,再git status
:
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ 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: test.txt
这时候显示文件已经被修改,如果你想改回原来的版本,则可以执行命令git checkout -- 文件名
,例子:
$ git checkout -- test.txt
再vim test.txt
或者git status
又回到了初始状态
我们echo
一下,继续修改,然后git add
,并且git ststus
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: test.txt
如果我们想把缓存区中修改的文件再放回到工作区,也可以用这个命令git reset HEAD
:
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git reset HEAD test.txt
Unstaged changes after reset:
M test.txt
相当于git rm --cached
两者比较:
$ git rm --cached test.txt
rm 'test.txt'
$ git reset HEAD test.txt
Unstaged changes after reset:
M test.txt
然后继续git add test.txt
和git commit -m
,并且使用git log
查看日志:
$ git log
commit b079cdb4554f8e1fe04cf4780fe9bc9584dad1d5 (HEAD -> master)
Author: YQHP-YuKi <********@qq.com>
Date: Wed Nov 11 16:47:11 2020 +0800
This is my test0.2
commit 20bc893146cb23040c5b0b83180be73c7c0bc5d1
Author: YQHP-YuKi <********@qq.com>
Date: Wed Nov 11 16:19:53 2020 +0800
This is my test0.1
就会发现我们提交的两次版本
我们再次创建一个文件名叫test2.txt
,并对其进行git add
和git commit
,然后使用git log
对其进行查看
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git log
commit e93c2027de92a1faec3902af4a13352dc7347174 (HEAD -> master)
Author: YQHP-YuKi <*******@qq.com>
Date: Thu Nov 12 15:43:19 2020 +0800
This test is fot Trek
commit b079cdb4554f8e1fe04cf4780fe9bc9584dad1d5
Author: YQHP-YuKi <*******@qq.com>
Date: Wed Nov 11 16:47:11 2020 +0800
This is my test0.2
commit 20bc893146cb23040c5b0b83180be73c7c0bc5d1
Author: YQHP-YuKi <********@qq.com>
Date: Wed Nov 11 16:19:53 2020 +0800
This is my test0.1
可以查看到test0.1
与test0.2
是与test.txt
有关的版本注释,而 This test is fot Trek
是test2.txt
的版本注释,如果我们想删除版本库中的某个版本,可以使用git rm
这个命令,例:
$ git rm test2.txt
$ git status
查看到此文件又回到了缓存区
要把test.txt
放回工作目录,命令git reset HEAD
,例:
$ git reset HEAD test2.txt
要把工作区的文件重新放回到版本库,相当于撤销之前的两次删除操作,命令git checkout --
,例:
$ git checkout -- test2.txt
git rm
与rm
的区别:
git rm
是直接将文件从版本库删除到暂存区:
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git rm test2.txt
rm 'test2.txt'
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test2.txt
若想回到工作区,则是git reset HEAD
:
$ git reset HEAD test2.txt
Unstaged changes after reset:
D test2.txt
若想回到版本库,则必须是先回到工作区,才能回到版本库git checkout
:
$ git checkout -- test2.txt
而rm
是直接将文件从版本库删除回到工作区,如果想回到版本库,则直接git checkout
就能回到版本库:
$ git checkout -- test2.txt
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git status
On branch master
nothing to commit, working tree clean
git mv
与mv
的区别:
git mv
将test.txt修改为test3.txt
:
$ git mv test.txt test3.txt
查看状态:
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: test.txt -> test3.txt
如果想不改名了,还是用test.txt
,需要做的操作恢复test.txt
并且删除test3.txt
:
$ git reset HEAD test3.txt
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: test.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
这时候需要2步操作,1:恢复test.txt
,2:删除test3.txt
:
操作1:
$ git reset HEAD test.txt
Unstaged changes after reset:
D test.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
no changes added to commit (use "git add" and/or "git commit -a")
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git checkout -- test.txt
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
nothing added to commit but untracked files present (use "git add" to track)
现在之剩下还在工作区的test3.txt
了,于是进行操作2:
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ rm test3.txt
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git status
On branch master
nothing to commit, working tree clean
如果你想修改test.txt
成为test3.txt
,则需要两个步骤:
步骤1:mv
文件名
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git mv test.txt test3.txt
步骤2:将暂存区里的test3.txt
提交到版本库
A@DESKTOP-6DP8MG1 MINGW64 /e/Gitfile (master)
$ git commit -m 'change to test3.txt'
[master 3b5830c] change to test3.txt
1 file changed, 0 insertions(+), 0 deletions(-)
rename test.txt => test3.txt (100%)
用git log
能够查到改名的记录,ls
能查到test.txt
文件变为test3.txt
并且已删除test.txt
,总结:git mv
相当于将你源文件从版本库删除出来,并且复制出一个新的名字不同的源文件,再将它提交到版本库
mv
:
直接
$ mv test.txt test3.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
test3.txt
这时候可以看出来,mv
是直接将test.txt
变为了test3.txt
,但这两个文件却是都没有放入缓存区和版本库的,如果不想改名,则应该像这样test.txt
用git checkout --
,而test3.txt
直接rm
:
$ git checkout -- test.txt
$ rm test3.txt
$ git status
On branch master
nothing to commit, working tree clean
如果继续改名,则应该git add
两个文件:
$ git add test.txt test3.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: test.txt -> test3.txt
接下来就git commit --
跟操作git mv
一样了
一次性添加多个文件每个都写很麻烦,可以命令git add .
把当前目录下的所有文件都纳入暂存区
git commit --amend -m
命令修改提交信息
例子:
$ git commit --amend -m'change again'
git log
可以带的参数
-p | 展开显示每次提交的内容差异 |
---|---|
-n | 仅显示最近的n次更新 |
--stat | 仅显示简要的增该行数统计 |
--pretty=oneline | 只显示版本号与版本注释 |
--pretty=format:"%h-%an,%ar:%s" | 只显示版本号头几个数字、提交者、和距离当前的日期 |
查看git
帮助文档
两条命令:
$ git help config
$ git config --help