GIT

﹥>﹥吖頭↗ 提交于 2019-11-26 13:31:53

GIT

一、简介

1. 什么是git?

git是一个开源的、分布式版本控制系统,用于高效的管理各种大小项目和文件

2.代码管理工具的用途

  • 防止代码丢失,做备份
  • 项目的版本管理和控制,可以通过设置节点进行跳转
  • 建立各自的开发环境分支,互不影响,方便合并
  • 在多终端开发时,方便代码的相互传输

3. git的特点

  • git是开源的,躲在*nix下使用,可以管理各种文件
  • git是分布式

二、使用

1. 基本概念

  • 工作区:项目操作的目录,可以随意修改代码
  • 暂存区:用于记录工作区的修改内容
  • 仓库区:备份区域,将提交之前的代码备份至本地
  • 远程仓库:服务器上托管代码的地方

注意:在本地仓库中,git总是希望工作区的内容与仓库区保持一致,而且只有仓库区的内容才能和远程仓库交互

2. 初始配置

git config

  1. 配置所有用户git config --system [选项]

    配置文件位置:/etc/gitconfig

  2. 配置当前用户:git config --global [选项]

    配置文件位置:~/.gitconfig

  3. 配置当前项目:git config [选项]

    配置文件位置:project/.git/config

2.1 配置用户名

将用户名设置为Chancey(该配置需要root权限)

[root@localhost ~]# git config --system user.name Chancey [root@localhost ~]# cat /etc/gitconfig  [user]     name = Chancey 

2.2 配置邮箱

将邮箱配置为全局可用

[root@localhost ~]# git config --global user.email 943870433@qq.com [root@localhost ~]# cat ~/.gitconfig  [user]     email = 943870433@qq.com

2.3 配置编译器

配置使用pycharm编辑器

[root@localhost mongo]# git init 初始化空的 Git 版本库于 /root/project/less/mongo/.git/ [root@localhost mongo]# ls -al 总用量 324 drwxr-xr-x  3 root root    105 8月   6 09:33 . drwxr-xr-x 16 root root    244 8月   5 14:13 .. -rw-r--r--  1 root root 315737 7月  31 20:15 demo.jpg -rw-r--r--  1 root root    268 8月   5 17:16 get_file.py drwxr-xr-x  7 root root    119 8月   6 09:33 .git -rw-r--r--  1 root root      0 8月   5 17:18 mm.jpg -rw-r--r--  1 root root   2744 8月   5 16:58 mongodb.py -rw-r--r--  1 root root    383 8月   5 17:12 save_file.py [root@localhost mongo]# cat .git/config  [core]     repositoryformatversion = 0     filemode = true     bare = false     logallrefupdates = true     editor = pycharm

2.4 查看配置

[root@localhost mongo]# git config --list user.name=Chancey user.email=943870433@qq.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true core.editor=pycharm

3.基本命令

3.1 初始化

初始化仓库

git init

意义:将项目目录编程git仓库目录,生成git本地仓库。即该项目目录可以使用git管理

3.2 查看状态

查看本地仓库状态

git status

[root@localhost mongo]# git status # 位于分支 master # # 初始提交 # # 未跟踪的文件: #   (使用 "git add <file>..." 以包含要提交的内容) # #   demo.jpg #   get_file.py #   mm.jpg #   mongodb.py #   save_file.py 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

3.3 记录暂存

将工作内容记录到暂存区

git add [files...]

将mm.jpg记录到暂存区 [root@localhost mongo]# git add mm.jpg   [root@localhost mongo]# git status # 位于分支 master # # 初始提交 # # 要提交的变更: #   (使用 "git rm --cached <file>..." 撤出暂存区) # #   新文件:    mm.jpg # # 未跟踪的文件: #   (使用 "git add <file>..." 以包含要提交的内容) # #   demo.jpg #   get_file.py #   mongodb.py #   save_file.py  将所有文件(不包含隐藏文件)记录到暂存区 [root@localhost mongo]# git add . [root@localhost mongo]# git status # 位于分支 master # # 初始提交 # # 要提交的变更: #   (使用 "git rm --cached <file>..." 撤出暂存区) # #   新文件:    demo.jpg #   新文件:    get_file.py #   新文件:    mm.jpg #   新文件:    mongodb.py #   新文件:    save_file.py #

隐藏文件需要指明添加

3.4 撤回暂存

撤回暂存记录,也就是将暂存区的文件回退到操作区

git rm --chched [files...]

[root@localhost mongo]# git rm --cached mm.jpg rm 'mm.jpg' [root@localhost mongo]# git status # 位于分支 master # # 初始提交 # # 要提交的变更: #   (使用 "git rm --cached <file>..." 撤出暂存区) # #   新文件:    demo.jpg #   新文件:    get_file.py #   新文件:    mongodb.py #   新文件:    save_file.py # # 未跟踪的文件: #   (使用 "git add <file>..." 以包含要提交的内容) # #   mm.jpg

3.5 同步本地

将暂存区记录同步到本地仓库区

git commit [files...] -m '提交说明'

-m 表示添加一些同步信息,表达同步内容

同步单个文件 [root@localhost mongo]# git commit mm.jpg -m "add mm.jpg" [master(根提交) 3c84545] add mm.jpg  1 file changed, 0 insertions(+), 0 deletions(-)  create mode 100644 mm.jpg [root@localhost mongo]# git status # 位于分支 master # 要提交的变更: #   (使用 "git reset HEAD <file>..." 撤出暂存区) # #   新文件:    demo.jpg #   新文件:    get_file.py #   新文件:    mongodb.py #   新文件:    save_file.py #   同步所有文件 [root@localhost mongo]# git commit . -m "add *" [master 65a7fb2] add *  4 files changed, 131 insertions(+)  create mode 100644 demo.jpg  create mode 100644 get_file.py  create mode 100644 mongodb.py  create mode 100644 save_file.py [root@localhost mongo]# git status # 位于分支 master 无文件要提交,干净的工作区

3.6 查看commit日志

git log查看所有commit日志

[root@localhost mongo]# git log commit 65a7fb259cc47be9a19087c631ae4fc763c48756 Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:14:03 2019 +0800      add *  commit 3c84545912e889598268b43bf010df65952625dd Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:03:24 2019 +0800      add mm.jpg

git log --pretty=oneline格式化查看

[root@localhost mongo]# git log --pretty=oneline 65a7fb259cc47be9a19087c631ae4fc763c48756 add * 3c84545912e889598268b43bf010df65952625dd add mm.jpg 

3.7 比较文件

比较工作区文件和仓库区文件的差异

git diff [file]

[root@localhost mongo]# git diff d diff --git a/d b/d index 28139cb..444200b 100644 --- a/d +++ b/d @@ -1 +1,2 @@ -hello world !!! +hello world !!!!!!! +hello world !!!!!!!

+表示添加的内容,-表示减少的内容

3.8 放弃修改

git checkout -- [file]

[root@localhost mongo]# cat d hello world !!!! hello world !!!! [root@localhost mongo]# git checkout -- d [root@localhost mongo]# cat d hello world !!! [root@localhost mongo]# 

3.9 找回文件

git check [file]

[root@localhost mongo]# ls d  demo.jpg  get_file.py  mongodb.py  save_file.py [root@localhost mongo]# git checkout mm.jpg [root@localhost mongo]# ls d  demo.jpg  get_file.py  mm.jpg  mongodb.py  save_file.py

只能恢复已经同步到仓库的文件

3.10 移动删除

git mv [file] [path]

git rm [files]

[root@localhost mongo]# git mv d dir [root@localhost mongo]# git status # 头指针分离于 HEAD # 要提交的变更: #   (使用 "git reset HEAD <file>..." 撤出暂存区) # #   新文件:    d #   新文件:    dir/d # # 尚未暂存以备提交的变更: #   (使用 "git add/rm <file>..." 更新要提交的内容) #   (使用 "git checkout -- <file>..." 丢弃工作区的改动) # #   删除:      dir/d # 
[root@localhost mongo]# git rm mm.jpg rm 'mm.jpg' [root@localhost mongo]# git status # 头指针分离于 HEAD # 要提交的变更: #   (使用 "git reset HEAD <file>..." 撤出暂存区) # #   新文件:    d #   新文件:    dir/d #   删除:      mm.jpg # # 尚未暂存以备提交的变更: #   (使用 "git add/rm <file>..." 更新要提交的内容) #   (使用 "git checkout -- <file>..." 丢弃工作区的改动) # #   删除:      dir/d # [root@localhost mongo]# git commit . -m "rm file" [分离头指针 b717327] rm file  2 files changed, 1 insertion(+)  create mode 100644 d  delete mode 100644 mm.jpg [root@localhost mongo]# git status # 头指针分离自 65a7fb2 无文件要提交,干净的工作区 

这两个操作会修改工作区的内容,同时将操作记录提交到暂存区

3.11 查看所有操作

git reflog

作用:如果回退的版本并不是自己想要的,可以通过该命令找到之前commit的id值,然后根据该id来再次回退,就可以回到之前commit的内容

[root@localhost mongo]# git reflog 3c84545 HEAD@{0}: reset: moving to 3c84545 65a7fb2 HEAD@{1}: reset: moving to HEAD^ b717327 HEAD@{2}: commit: rm file 65a7fb2 HEAD@{3}: checkout: moving from master to HEAD 65a7fb2 HEAD@{4}: commit: add * 3c84545 HEAD@{5}: commit (initial): add mm.jpg

4.版本控制

将项目回退到指定的地方

4.1 版本回退

git reset --hard HEAD^

回退到上一个commit

[root@localhost mongo]# git log commit b717327aac44b7f6998e1f7254924a4ea886e3e2 Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:50:36 2019 +0800      rm file  commit 65a7fb259cc47be9a19087c631ae4fc763c48756 Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:14:03 2019 +0800      add *      [root@localhost mongo]# git reset --hard HEAD^ HEAD 现在位于 65a7fb2 add * [root@localhost mongo]# git log commit 65a7fb259cc47be9a19087c631ae4fc763c48756 Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:14:03 2019 +0800      add *  [root@localhost mongo]# git status # 头指针分离于 HEAD 无文件要提交,干净的工作区

^表示回退的commit,一个^表示回退到最新的commit,两个^表示回退到最近第二次的commit

HEAD表示最新的commit,一般在git log 返回的最上边的commit

git reset --hard [commit_id]

回退到指定的commit

[root@localhost mongo]# git log commit 65a7fb259cc47be9a19087c631ae4fc763c48756 Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:14:03 2019 +0800      add *  commit 3c84545912e889598268b43bf010df65952625dd Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:03:24 2019 +0800      add mm.jpg [root@localhost mongo]# git status # 头指针分离于 HEAD 无文件要提交,干净的工作区 [root@localhost mongo]# git log commit 65a7fb259cc47be9a19087c631ae4fc763c48756 Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:14:03 2019 +0800      add *  commit 3c84545912e889598268b43bf010df65952625dd Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:03:24 2019 +0800      add mm.jpg [root@localhost mongo]# git reset --hard 3c84545 HEAD 现在位于 3c84545 add mm.jpg [root@localhost mongo]# ls dir  mm.jpg [root@localhost mongo]# git log commit 3c84545912e889598268b43bf010df65952625dd Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:03:24 2019 +0800      add mm.jpg

4.2 标签

在项目的重要的commit位置添加快照,保存当时的工作状态,一般用于版本的迭代。

4.2.1 创建标签

git tag [tag_name] -m [message]

commit_id可以不写则默认标签表示最新的commit_id的位置,message也可以不写,但是最好添加

标签实际上是在仓库区打标签

[root@localhost mongo]# git tag v1.0 -m "版本1" [root@localhost mongo]# git log --pretty=oneline 3c84545912e889598268b43bf010df65952625dd add mm.jpg [root@localhost mongo]# git tag v0.9 3c84545 -m "add d" [root@localhost mongo]# git tag v0.9 v1.0
4.2.2 查看标签

git tag查看标签列表

git show [tag_name]查看标签详细信息

[root@localhost mongo]# git tag v0.9 v1.0
[root@localhost mongo]# git show v0.9 tag v0.9 Tagger: Chancey <943870433@qq.com> Date:   Tue Aug 6 11:24:52 2019 +0800  add d  commit 3c84545912e889598268b43bf010df65952625dd Author: Chancey <943870433@qq.com> Date:   Tue Aug 6 10:03:24 2019 +0800      add mm.jpg  diff --git a/mm.jpg b/mm.jpg new file mode 100644 index 0000000..e69de29 
4.2.3 去往标签节点

git reset --hard [tag_name]

[root@localhost mongo]# git reset --hard v0.9 HEAD 现在位于 3c84545 add mm.jpg [root@localhost mongo]# git reset --hard v1.0 HEAD 现在位于 3c84545 add mm.jpg
4.2.4 删除标签

git tag -d [tag_name]

[root@localhost mongo]# git tag -d v0.9 已删除 tag 'v0.9'(曾为 89ea664) [root@localhost mongo]# git tag v1.0

4.3 保存工作区

git stash保存工作区内容

git stash list查看工作区列表

git statsh apply [stash@{n}]应用某个工作区

git stash drop [stash@{n}]删除某一个工作区

git stash clear 删除所有保存的工作区

说明:将工作区未提交的修改封存,让工作区回到修改前的状态

[root@localhost mongo]# git stash list stash@{0}: WIP on (no branch): a0537c8 4444 [root@localhost mongo]# rm -rf dir/demo.txt   [root@localhost mongo]# git stash save "删除demo.txt" Saved working directory and index state On (no branch): 删除demo.txt HEAD 现在位于 a0537c8 4444 [root@localhost mongo]# git stash list stash@{0}: On (no branch): 删除demo.txt stash@{1}: WIP on (no branch): a0537c8 4444 [root@localhost mongo]# rm -rf dir/b   [root@localhost mongo]# git stash save "删除b" Saved working directory and index state On (no branch): 删除b HEAD 现在位于 a0537c8 4444 [root@localhost mongo]# git stash list stash@{0}: On (no branch): 删除b stash@{1}: On (no branch): 删除demo.txt stash@{2}: WIP on (no branch): a0537c8 4444 [root@localhost mongo]# ls dir/ b  demo.txt  [root@localhost mongo]# git stash apply stash@{1} 删除 dir/demo.txt # 头指针分离自 65a7fb2 # 尚未暂存以备提交的变更: #   (使用 "git add/rm <file>..." 更新要提交的内容) #   (使用 "git checkout -- <file>..." 丢弃工作区的改动) # #   删除:      dir/demo.txt # 修改尚未加入提交(使用 "git add" 和/或 "git commit -a") [root@localhost mongo]# ls dir  mm.jpg [root@localhost mongo]# ls dir/ b  [root@localhost mongo]# git stash list stash@{0}: On (no branch): 删除b stash@{1}: On (no branch): 删除demo.txt stash@{2}: WIP on (no branch): a0537c8 4444 [root@localhost mongo]# git stash drop stash@{0} 丢弃了 stash@{0} (a7983051b52fe9fb8e3026a8717ba6bd8f08def8) [root@localhost mongo]# git stash list stash@{0}: On (no branch): 删除demo.txt stash@{1}: WIP on (no branch): a0537c8 4444  [root@localhost mongo]# git stash clear [root@localhost mongo]# git stash list [root@localhost mongo]# 

4.4 本地分支

4.4.1 查看分支情况

git branch

[root@localhost mongo]# git branch * (分离自 65a7fb2)   master

前边带*的分支表示当前工作的分支

4.4.2 创建分支

git branch [分支名]

[root@localhost mongo]# git status

头指针分离自 65a7fb2

无文件要提交,干净的工作区[root@localhost mongo]# git branch Jame_dev

[root@localhost mongo]# git branch

*(分离自 65a7fb2)

Jame_dev

master

基于a分支创建b分支,此时b分支会拥有a分支全部内容,在创建b分支时最好保持a分支“干净”的状态

4.2.3 切换工作分支

git checkout [branch_name]

[root@localhost mongo]# git checkout Jame_dev  切换到分支 'Jame_dev'  [root@localhost mongo]# git status  位于分支 Jame_dev  无文件要提交,干净的工作区

4.2.2和4.2.3可以同时操作,即创建分支并切换分支

git checkout -b [branch_name]

[root@localhost mongo]# git status # 位于分支 master 无文件要提交,干净的工作区 [root@localhost mongo]# git checkout -b Tom_dev 切换到一个新分支 'Tom_dev'
[root@localhost mongo]# git status # 位于分支 Tom_dev 无文件要提交,干净的工作区  [root@localhost mongo]# vim tom_work  [root@localhost mongo]# git add *  [root@localhost mongo]# git commit -m "Tom Add Files" [Tom_dev f0c0d3a] Tom Add Files  1 file changed, 1 insertion(+)  create mode 100644 tom_work   [root@localhost mongo]# git status # 位于分支 Tom_dev 无文件要提交,干净的工作区  [root@localhost mongo]# git checkout master 切换到分支 'master'  [root@localhost mongo]# ls demo.jpg  get_file.py  mm.jpg  mongodb.py  save_file.py [root@localhost mongo]# git merge Tom_dev 更新 65a7fb2..f0c0d3a Fast-forward  tom_work | 1 +  1 file changed, 1 insertion(+)  create mode 100644 tom_work   [root@localhost mongo]# ls demo.jpg  get_file.py  mm.jpg  mongodb.py  save_file.py  tom_work
解决冲突问题  [root@localhost mongo]# git checkout Jame_dev 切换到分支 'Jame_dev'  [root@localhost mongo]# vim jame.txt  [root@localhost mongo]# git add *  [root@localhost mongo]# git commit -m "Jame add files"  [Jame_dev 6b2504e] Jame add files  1 file changed, 1 insertion(+)  create mode 100644 jame.txt   [root@localhost mongo]# git status # 位于分支 Jame_dev 无文件要提交,干净的工作区  [root@localhost mongo]# git checkout master 切换到分支 'master'  [root@localhost mongo]# git merge Jame_dev error: cannot run pycharm: ????????? error: unable to start editor 'pycharm' 未提交合并,使用 'git commit' 完成此次合并。  [root@localhost mongo]# git status # 位于分支 master # 所有冲突已解决但您仍处于合并中。 #   (使用 "git commit" 结束合并) # # 要提交的变更: # #   新文件:    dir/b #   新文件:    jame.txt #  [root@localhost mongo]# git commit -m "merge jame" [master f1c89d6] merge jame

当分支合并时,原分支和以前发生了变化就会产生冲突

当合并分支时添加新的模块(文件),这种情况可以自动解决,只需要自己决定commit操作即可

当合并分支时,两个分支修改了同一个文件,则需要手动解决冲突(VSCode可以更加方便的管理)

当两个分支对同一文件进行修改,则会产生冲突,该冲突只能手动修改

4.2.4 删除分支

git branch -d [branch]

git branch -D [branch]强制删除分支

 

[root@localhost mongo]# git branch -d Tom_dev
已删除分支 Tom_dev(曾为 d33ac72)。
[root@localhost mongo]# git branch
Jame_dev

*master
[root@localhost mongo]# git branch -D Jame_dev
已删除分支 Jame_dev(曾为 6b2504e)。
[root@localhost mongo]# git branch

*master

5.远程仓库

5.1 概述

远程主机上的git仓库,git是分布式仓库,每台主机的git仓库结构类似,只是把别人主机上的git仓库称为远程仓库。

5.2 共享仓库

在git仓库中bare属性为true的共享仓库可以很好的和远程仓库进行交互

创建步骤

  • 选择共享仓库目录,将该目录属性主设置为当前用户

    mkdir gitrepo chown chancey:chancey gitrepo
  • 将该目录初始化为git共享目录

    cd gitrepo git init --bare tedu.git # redu为项目名称,.git为通用的结尾后缀

  • 将git配置目录与项目目录设置为相同的属主

    chown -R root:root chancey.git

5.3 远程仓库操作

所有操作都是在本地仓库下进行

  • 添加远程仓库

git remote add origin root@127.0.0.1:/root/gitrepo/chancey.git

origin 为连接名称

root 为用户名

127.0.0.1 为远程仓库的IP

/root/gitrepo/chancey.git 为远程仓库的绝对路径

可以连接多个远程仓库,但是连接名不能重复,默认使用ssh协议

创建连接在本地的需要连接的路径下执行该命令

  • 删除远程主机

git remote rm [origin]

  • 查看连接的主机

git remote rm [origin]

注意:一个git项目连接的远程主机名不会重复

  • 将本地分支推送给远程仓库

git push -u origin master推送分支

git push origin v1.0推送标签

git push --force origin强行推送(本地git落后于远程git)

[root@localhost mongo]# git push -u origin master The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established. ECDSA key fingerprint is SHA256:KsN7XiKG8NHgwzCEnFc0COfJdEq6oosEdjLXTE326XA. ECDSA key fingerprint is MD5:31:b0:e6:3e:e1:3e:d2:85:1e:ea:3b:a7:25:24:c1:06. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts. root@127.0.0.1's password:  fatal: '/root/gitrepo.git' does not appear to be a git repository fatal: Could not read from remote repository.  Please make sure you have the correct access rights and the repository exists.

-u 只有第一次和远程主机创建连接的时候需要,表示与远程对应分支建立自动关联

  • 删除远程分支

git branch -a查看所有分支(包括远程分支)

git push origin [:branch]删除远程分支

  • 从远程获取项目

git clone root@127.0.0.1:/root/gitrepo/chancey.git

注意:获取到本地i的项目会自动和远程仓库建立连接。且获取的项目本身也是个git项目

  • 从远程获取代码

git pull

git fetch origin master:tmp 将远程分支master拉取到本地,作为tmp分支

pull将远程内容直接拉取到本地,并和对应分支内容进行合并

fetch将远程内容拉取到本地,但是不会和本地对应分支合并,可以自己判断后再使用merge合并

三、GitHub

开源的项目社区网站,拥有全球最多的开源项目。开发者可以注册网站在GitHub建立自己的项目仓库

1. 获取项目

  • 搜索项目
  • 获取git连接
  • 通过clone下载

2. 创建仓库

  • 右上角加好下拉菜单,new repository
  • 填写基本的仓库信息,点击创建
  • 创建之后GitHub就是远程仓库

3. 删除仓库

  • 选择仓库之后,点击settings
  • 下拉,删除

4.秘钥添加

  • 个人主机上使用ssh-keygen 获取秘钥对(通常在~/.ssh下)
  • 将id_rsa.pub(公钥)内容复制,添加到GitHub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!