简单介绍
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-allCentos或RHEL
$ sudo dnf install git-allMac
安装命令行工具(如已安装Xcode,命令行工具会在首次启动Xcode时提示安装)
在 Mac 上安装 Git 有多种方式。 最简单的方法是安装 Xcode Command Line Tools。 Mavericks (10.9) 或更高版本的系统中,在 Terminal 里尝试首次运行 git 命令即可。
$ git --versionhomebrew
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 --system2.~/.gitconfig(很常用),命令:
$ git config --global3.针对特定项目的,命令:
$ 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.txtgit 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 cleangit 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.txtA@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