Git学习笔记

本文是作者学习Git的笔记,所有内容均来自网络,版权归原作者,如有冒犯,请联系删除。

Git 狂神说复现笔记,原文档在微信公众号,点击下面链接跳转

视频同步笔记:狂神聊Git (qq.com)


学习git之前,我们需要先明白一个概念

版本控制!

1. 什么是版本控制

版本控制(Revision control)是一种在开发的过程中用于管理我们对文件、目录或工程等内容的修改历史,方便查看更改历史记录,备份以便恢复以前的版本的软件工程技术。

  • 实现跨区域多人协同开发

  • 追踪和记载一个或者多个文件的历史记录

  • 组织和保护你的源代码和文档

  • 统计工作量

  • 并行开发、提高开发效率

  • 跟踪记录整个软件的开发过程

  • 减轻开发人员的负担,节省时间,同时降低人为错误

简单说就是用于管理多人协同开发项目的技术。

没有进行版本控制或者版本控制本身缺乏正确的流程管理,在软件开发过程中将会引入很多问题,如软件代码的一致性、软件内容的冗余、软件过程的事物性、软件开发过程中的并发性、软件源代码的安全性,以及软件的整合等问题。

无论是工作还是学习,或者是自己做笔记,都经历过这样一个阶段!我们就迫切需要一个版本控制工具!

多人开发就必须要使用版本控制!

2. 常见的版本控制工具

我们学习的东西,一定是当下最流行的!主流的版本控制器有如下这些:

  • Git

  • SVN(Subversion)

  • CVS(Concurrent Versions System)

  • VSS(Micorosoft Visual SourceSafe)

  • TFS(Team Foundation Server)

  • Visual Studio Online

版本控制产品非常的多(Perforce、Rational ClearCase、RCS(GNU Revision Control System)、Serena Dimention、SVK、BitKeeper、Monotone、Bazaar、Mercurial、SourceGear Vault),现在影响力最大且使用最广泛的是Git与SVN

3. 版本控制分类

1、本地版本控制

记录文件每次的更新,可以对每个版本做一个快照,或是记录补丁文件,适合个人用,如RCS。

2、集中版本控制  SVN

所有的版本数据都保存在服务器上,协同开发者从服务器上同步更新或上传自己的修改

所有的版本数据都存在服务器上,用户的本地只有自己以前所同步的版本,如果不连网的话,用户就看不到历史版本,也无法切换版本验证问题,或在不同分支工作。而且,所有数据都保存在单一的服务器上,有很大的风险这个服务器会损坏,这样就会丢失所有的数据,当然可以定期备份。代表产品:SVN、CVS、VSS

3、分布式版本控制 Git

每个人都拥有全部的代码!安全隐患!

所有版本信息仓库全部同步到本地的每个用户,这样就可以在本地查看所有版本历史,可以离线在本地提交,只需在连网时push到相应的服务器或其他用户那里。由于每个用户那里保存的都是所有的版本数据,只要有一个用户的设备没有问题就可以恢复所有的数据,但这增加了本地存储空间的占用。

不会因为服务器损坏或者网络问题,造成不能工作的情况!

4. Git与SVN的主要区别

SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而工作的时候,用的都是自己的电脑,所以首先要从中央服务器得到最新的版本,然后工作,完成工作后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,对网络带宽要求较高。

Git是分布式版本控制系统,没有中央服务器,每个人的电脑就是一个完整的版本库,工作的时候不需要联网了,因为版本都在自己电脑上。协同的方法是这样的:比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。Git可以直接看到更新了哪些代码和文件!

Git是目前世界上最先进的分布式版本控制系统。

聊聊Git的历史

同生活中的许多伟大事物一样,Git 诞生于一个极富纷争大举创新的年代。

Linux 内核开源项目有着为数众广的参与者。绝大多数的 Linux 内核维护工作都花在了提交补丁和保存归档的繁琐事务上(1991-2002年间)。到 2002 年,整个项目组开始启用一个专有的分布式版本控制系统 BitKeeper 来管理和维护代码。

Linux社区中存在很多的大佬!破解研究 BitKeeper !

到了 2005 年,开发 BitKeeper 的商业公司同 Linux 内核开源社区的合作关系结束,他们收回了 Linux 内核社区免费使用 BitKeeper 的权力。这就迫使 Linux 开源社区(特别是 Linux 的缔造者 Linus Torvalds)基于使用 BitKeeper 时的经验教训,开发出自己的版本系统。(2周左右!) 也就是后来的 Git!

Git是目前世界上最先进的分布式版本控制系统。

Git是免费、开源的,最初Git是为辅助 Linux 内核开发的,来替代 BitKeeper!

Linux和Git之父李纳斯·托沃兹(Linus Benedic Torvalds)1969、芬兰

Git环境配置

5. 软件下载

打开 [git官网] https://git-scm.com/,下载git对应操作系统的版本。

所有东西下载慢的话就可以去找镜像!

官网下载太慢,我们可以使用淘宝镜像下载:http://npm.taobao.org/mirrors/git-for-windows/ 

6. 启动Git

安装成功后在开始菜单中会有Git项,菜单下有3个程序:任意文件夹下右键也可以看到对应的程序!

Git Bash:Unix与Linux风格的命令行,使用最多,推荐最多

Git CMD:Windows风格的命令行

Git GUI:图形界面的Git,不建议初学者使用,尽量先熟悉常用命令

7. 常用的Linux命令

平时一定要多使用这些基础的命令!

1)、cd : 改变目录。

2)、cd . . 回退到上一个目录,直接cd进入默认目录

3)、pwd : 显示当前所在的目录路径。

4)、ls(ll):  都是列出当前目录中的所有文件,只不过ll(两个ll)列出的内容更为详细。

5)、touch : 新建一个文件 如 touch index.js 就会在当前目录下新建一个index.js文件。

6)、rm:  删除一个文件, rm index.js 就会把index.js文件删除。

7)、mkdir:  新建一个目录,就是新建一个文件夹。

8)、rm -r :  删除一个文件夹, rm -r src 删除src目录

rm -rf / 切勿在Linux中尝试!删除电脑中全部文件!

9)、mv 移动文件, mv index.html src index.html 是我们要移动的文件, src 是目标文件夹,当然, 这样写,必须保证文件和目标文件夹在同一目录下。

10)、reset 重新初始化终端/清屏。

11)、clear 清屏。

12)、history 查看命令历史。

13)、help 帮助。

14)、exit 退出。

15)、#表示注释

8. Git配置

所有的配置文件,其实都保存在本地!

查看配置 git config -l

查看不同级别的配置文件:


#查看系统config
git config --system --list
  
#查看当前用户(global)配置
git config --global  --list

Git相关的配置文件:

1)、Git\etc\gitconfig  :Git 安装目录下的 gitconfig     --system 系统级

2)、C:\Users\Administrator\ .gitconfig    只适用于当前登录用户的配置  --global 全局

这里可以直接编辑配置文件,通过命令设置后会响应到这里。

9. 设置用户名与邮箱(用户标识,必要)

当你安装Git后首先要做的事情是设置你的用户名称和e-mail地址。这是非常重要的,因为每次Git提交都会使用该信息。它被永远的嵌入到了你的提交中:

git config --global user.name "kuangshen"  #名称git config --global user.email 24736743@qq.com   #邮箱
只需要做一次这个设置,如果你传递了--global 选项,因为Git将总是会使用该信息来处理你在系统中所做的一切操作。如果你希望在一个特定的项目中使用不同的名称或e-mail地址,你可以在该项目中运行该命令而不要--global选项。总之--global为全局配置,不加为某个项目的特定配置。

当你安装Git后首先要做的事情是设置你的用户名称和e-mail地址。这是非常重要的,因为每次Git提交都会使用该信息。它被永远的嵌入到了你的提交中.

以下内容来自B站UP-The CW的git视频教程

10. 最经典的命令:

# 1. Initialized empty Git repository in /home/dom/Documents/sos.git
git init 

# 2. check status
git status

# 3. track some file change
git add index.html # let git track index.html
git add .          # let git track the whole folder
# 4. If you changed some file which was added before, you need to re-add it again

# 5. check the difference to the original file
git diff

# 6. If you want to withdrew the change you make, or you don't want git track this change
git reset

# 7. When you push the change to the remote Git repository, you need to config the user name and email of Git
git config --globle user.name "Dom li"
git config --globle user.email "12345@qq.com"

# 8.
git commit -m "add README.md"

# 9. change core.editor of Git if needed
git config --global core.editor emacs

# 10. If you don't want git track some file, create a .gitignore file and add the file name to it line by line
vim .gitignore

# 11. If you want git stop to track some file
git rm --cashed newfile

# 12. push your repository to the cloud
git push

11. Git分支:

# 13. create branch
git branch branchName

# 14. move to branch
git checkout branchName

# 15. when I checkout to master branch, magic things happened.
git checkout master 

# 16. merge the child-branch to master and the branch is still in there
git merge branchName

# 17. If there are some conflicts in merging, this is complicated
# 18. If you type: git branch now, the branch branchName is still in there
git branch

# 19. If you want to delete the branch branchName, then type -d command. However, if there are some conflict when you delete the branch, Git will remind you and disallow you to delete. 
git branch -d branchName

# 20. If I want to invite a co-developer: In Github repository, Settings >> Collatorators >> search the co-developer's name: biganiki >> send a invitation email to biganiki


# 21. Tell git where your git repository is, THE WEBSITE ADDRESS
git remote add origin https://github.com/theniceboy/sos.git

# 22. push now, Git may ask you input the user-name and user-email
git push --set-upstream origin master

# 23. If you want git remember you user-name and user-email
git config credential.helper store

# 20.1 Let check to biganiki's cp, he can open the git-invatation email and clone the repository to his local folder
git clone https://github.com/theniceboy/sos.git

# 20.2 biganiki made some change about sos repository, then 
git add .

# 20.3 git commit -a is submit the file which exist before, not include the file which newly created.
git commit -a "the command you want to add."

# 24. After I got the info from biganiki, he says he has done the tasks, I need to check the change, I need to pull the repository first
git pull


Gitlab用户在组中有五种权限:Guest、Reporter、Developer、Master、Owner - 勤学如春起之苗 - 博客园

12. Git用户权限管理

Gitlab用户在组中有五种权限:Guest、Reporter、Developer、Master、Owner
Gitlab权限管理
Gitlab用户在组中有五种权限:Guest、Reporter、Developer、Master、Owner

Guest:可以创建issue、发表评论,不能读写版本库
Reporter:可以克隆代码,不能提交,QA、PM可以赋予这个权限
Developer:可以克隆代码、开发、提交、push,RD可以赋予这个权限
Master:可以创建项目、添加tag、保护分支、添加项目成员、编辑项目,核心RD负责人可以赋予这个权限
Owner:可以设置项目访问权限 - Visibility Level、删除项目、迁移项目、管理组成员,开发组leader可以赋予这个权限
Gitlab中的组和项目有三种访问权限:Private、Internal、Public

Private:只有组成员才能看到
Internal:只要登录的用户就能看到
Public:所有人都能看到
开源项目和组设置的是Internal

在本地的git项目中查看github的远程地址:

git remote -v

git对已经提交过的文件添加到.gitignore_zhipengit的博客-CSDN博客_git ignore 已经提交的

13. Git对已经提交过的仓库添加.gitignore文件

Git仓库的同步最好发生在.gitignore文件完成后进行,如果已经提交过代码到远程Github或者Gitee或者Gitlab了,那么就需要拉下来重新提交一次,不过请放心,你在Git里还是可以在命令行很优雅的完成这项工作。以上链接有详细介绍,本文也摘录如下:

# git之前已经提交过的文件,在开发过程中,此文件需要忽略,添加到了.gitignore,但是再次提交的时候,该文件依旧被跟踪。处理办法如下:
# step1: 为了避免冲突,先pull远程仓库到本地
git pull

# 删除本地项目目录的缓存
git rm -r --cached   .

# 编辑gitignore,将需要忽略的添加进来, 再次add仓库路径下的所有文件
git add .

# exec commit command
git commit -m "add .gitignore"

# Finally, submit the new repository to remote mirror-one
git push

14. Git拉取包含子模块submodule的仓库命令

怎么拉取一个包含很多子模块的Git项目呢?如果我们直接用git clone是不能把所有字母块中的最新版本直接拉过来的,不知道为什么Git为何要如此设置默认clone命令,anyway,有效的方式有两种方式:

#1. when clone the git project, add a param in the end
git clone https://github.com/username/project-main.git --recurse-submodules 

#2. If You already cloned the project and find that there isn't any submodule file in the specific folder, then you can do this more elegant.
git submodule init
git submodule  update

15. 在WIN系统上git clone时如果仓库文件名有空格的处理过程

git restore --staged .\
git reset HEAD --hard
cd .\benchmark\
rm .\benchmark\
git restore --staged .\benchmark\mc_pp\
git checkout master
git pull
git clean
git clean -f
git config core.protectNTFS false
git restore --staged .
git status 
git restore .

16. 我在本地的代码文件怎么和远程的一个gitee或者github的仓库关联起来并且用远程仓库管理我的本地文件呢?

# 首先在本地启动git bash here
git init 

git add .

git commit -m "Init the xx repository."

git remote add origin git@gitee.com/username/xxx.git 

git pull --rebase orgin master

git push origin master

TO BE CONTINUED.........

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值