上传到远程库

第一次上传

git add .
git commit -m"备注信息"

之后上传

git add .
git commit -m"备注信息"
git push origin  或 git -u push origin 指定origin为默认主机,后面就可以不加任何参数使用git push了

—————————————————

删除

$ git rm -h
用法:git rm [<选项>] [--] <文件>...-n, --dry-run         演习-q, --quiet           不列出删除的文件--cached              只从索引区删除-f, --force           忽略文件更新状态检查-r                    允许递归删除--ignore-unmatch      即使没有匹配,也以零状态退出

删除远程仓库文件:
1.git rm 文件名 ( -r 递归删除)
2.git commit -m"备注信息" 更新本地库
3.git push -u origin master 更新远程仓库
—————————————————
—————————————————
—————————————————
————————————————————————————————————

代码托管分两种,一种是SVN一种是GIT,二者都是一种技术;

集中式管理SVN(客户端:TottoiseSVN)
分布式管理GIT(客户端:sourcetree)

学习GIT不需要有SVN基础,但GIT都是Linux命令;
集中还是分散是针对每个开发者手中的代码而言的,具体不解释了。我常用的是GIT,使用命令行居多,偶尔使用图形化git–sourcetree;初学者可能对这一套东西不好理解,不会很深的体会到这么做对开发意义有多大。
代码托管说白了就是保管码书写的任意提交过的状态,那一刻的项目代码是什么样,我要是想回退到那一刻就可以回到哪。
之前代码遇到bug,但是要放下手头开发进度。这时可以另开一个hotfix热修复分支,没问题再合并到主分支上。git的log日志记录了这个项目的从生到死,每一步历程。N年后老子要回到V1.0版本也没人能拦住你。


GIT:版本控制

版本控制的思想(1.个人;2.团队)

个人:
代码修改的历史记录很多,如果已经写到了很复杂的状态,想进行修改,在复杂代码上修改很容易造成错误;
想回到过去,有没有后悔药吃?

团队:
团队中多人可能会一起开发同一个类(协同开发),若有人把别人写的优秀代码删除了,覆盖上了自己写的不合格的代码,这时就需要版本控制的思想来解决;

版本控制工具应该具备的功能:

  • 1.协同修改:多人一起修改服务端统一文件;
  • 2.数据备份:
  • 3.版本管理:保存每一个版本的文件信息需要 不保存重复数据 ,以节省存储空间,GIT采用文件快照方式,SVN采用增量式管理方式(保存修改部分);
  • 4.权限控制:对团队中开发人员进行权限区别;且GIT有独家功能:对团队外人员贡献的代码进行审核;
  • 5.历史记录:
  • 6.分支管理:让开发团队在工作过程中有多条生产线同时推进任务,提高效率;

    GIT的优势:
  • 大部分操作在本地完成,不需联网(集中式系统一定要联网);
  • 完整性保证:提交前的数据 - Hash - 提交后的数据(不管Hash运算多少次,得到的结果都是不变的;输入的数据变了一点,Hash提交后的数据就会变很多,这是Hash神奇之处);
  • 尽可能添加数据而不是删除或修改数据;(代码 删/修改 后就找不回了,GIT尽量进行添加,而不是修改)
  • 分支操作快捷流畅;
  • 与Linux命令全面兼容;

Git和代码托管中心,比如GitHub

代码托管中心的任务:维护远程库;
参照上一张图,项目提交到本地库后,还需要交互到远程库;

  • 提问:【本地库】和【远程库】如何交互?
    两种情况:

1.团队内 协同开发图
(注意:PULL = FETCH + MERGE)

2.跨团队 协作图;
注意fork和clone区别(图中有写)
(注意:PULL = FETCH + MERGE)

托管中心 - 在局域网内:
可搭建:GitLab服务器

托管中心 - 在外网中:
可搭建:GitHub / 码云


GIT操作

分为两部分:1.GIT命令行操作;2.GIT图形化界面操作;

GIT命令行操作

本地库操作
——————————————————

  • 提问:如何初始化本地仓库?

进入想建立目录的路径下;
git init
返回:Initialized empty Git repository in /xxx/Documents/Git/wechat/.git/
此路径下建了一个空的Git仓库;

Linux下文件名前带了个点的都是默认隐藏的,想查看他?
ls -a

看看.git里有什么?
ls .git

HEAD     description info        refs
config      hooks       objects

这就是本地库的效果!!!
注意!!
.git目录里存放的是本地库相关的子目录和文件,不要删除,要不要胡乱修改!!!

(知道它的存在即可,不要改动)

另外还需要设置签名;
形式:

  • 用户名:(必填)
  • Email:(选填)
    **作用:**只是为了区别程序员不同的身份;
    注意代码托管中心的账号密码和签名的用户密码没有任何关系!!
    这两个信息其实无关紧要,邮箱填不填都无所谓;
    一个项目里,碰到两个用户名和密码一样的人也是有可能的;
    代码托管网站的账户才是区分个人信息的标准;

设置签名的命令:
(默认系统用户级别)

  • 项目级别(仓库级别):仅在本地库范围内有效,比如/xxx/Documents/Git/wechat
  • 系统用户级别:git config --global登录当前Apple账号的用户范围;

git config user.name 名字
git config user.email 邮箱
git config --global user.name 名字
git config --global user.email 邮箱

查看 项目级别 的保存:
$cat .git/config

[core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = trueprecomposeunicode = true
[user]email = xxx@gmail.comname = xxxx

查看 系统用户级别 的保存:
进入系统本账户文件夹:
$ls -la|less(查看隐藏信息)
看到了drwx------ 3 xxxx staff 96 Mar 5 09:33 .config
$ cat .gitconfig

[user]name = xxxxemail = xxxx@gmail.com

——————————————————

git status 查看项目状态】

作用:查看工作区、暂存区状态;

进入项目路径:
$ cd /Users/chiu/Documents/Git/wechat
$ git status

返回:
On branch master 现在在master分枝上;
No commits yet 本地库无已commit的内容;
nothing to commit (create/copy files and use “git add” to track)(暂存区里没内容)

【路径内随便新建一个文档,再查看项目状态】:
$vim good.txt
$git status

返回:

On branch master
No commits yet
Untracked files: 发现了未追踪的文件(use "git add <file>..." to 【include】 in what will be committed)(提示:用 git add 把他提交到暂存区)good.txt (显示为红色)nothing added to commit but untracked files present (use "git add" to track)
(不能commit,即暂存区无内容;但未追踪的文件存在)
(新建文件时:要想track文件,必须先git add)

git add 后,查看项目状态】

git add .
git add *
ADD全部文件;

作用:把工作区的“新建/修改”添加到暂存区;

$git add good.txt
$git status

On branch masterNo commits yetChanges to be committed:(use "git rm --cached <file>..." to unstage) 用 git rm --cached 来撤回new file:   good.txt (显示为绿色,因为git add后到了暂存区)


试试看撤回!
$git rm --cached good.txt
rm ‘good.txt’

git commit 后,查看项目状态】

提示需要输入信息,类似代码中的写注释;于是有两种方式:

  • (1).git commit -m "信息内容" 文件名
  • (2).git commit 文件名,直接进入VIM编辑;

$git commit --amend:修改上一次已经commit的信息;

例子:
(1).git commit -m "My first commit of "good.txt",new file或者modified " good.txt
(2).

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   good.txt

下面进入VIM操作,输入完成保存退出即可;

:set nu 显示行号
i 进入Insert模式
输入信息,比如:My first commit new file: good.txt
esc 退出Insert模式
:wq 保存退出

返回:
root-commit:根提交 只在新建文件时出现,之后的修改都不会有root-commit出现;
0af5132:可以粗略认为是本次commit的版本号;
后面就是自己写的备注;
9 insertions(+):本次commit新增的行数(cat 文件名即可查看);
100644:文件权限;

[master (root-commit) 0af5132] My first commit new file: good.txt1 file changed, 9 insertions(+)create mode 100644 good.txt

$ git status
On branch master
nothing to commit, working tree clean 暂存区没有可Commit的;
—————————
以上是新建文件Commit,那么修改文件呢?

修改工作区的文件后,需要重新add到暂存区,commit到本地库;

$vim good.txt(比如我删除了几行)
$git status

On branch master
Changes not staged for commit:(use "git add <file>..." to 【update】 what will be committed)  (之前是【include】)(use "git checkout -- <file>..." to discard changes in working directory)modified:   good.txt (MODIFIED:被修改了)(红色)no changes added to commit (use "git add" 【and/or】 "git commit -a")
(可以先add再commit,也可以)
(修改文件时:若文件已经tracked,可以git add再git commit,也可以直接git commit)

接下来可以git add再git commit,或者直接git commit;

返回:

[master 8668d26] The 2nd Version of "good.txt".1 file changed, 1 insertion(+), 4 deletions(-)

——————————————————

git log,查看版本记录】

完整查看:$ git log
简便查看:$ git log --pretty=oneline
更更更简便查看:‘gitlog−−oneline‘便于版本的前进后退:` git log --oneline` 便于版本的 前进后退:‘gitlog−−oneline‘便于版本的前进后退:git reflog

退出:$Q

我做了6次修改和git commit

$git log

commit 3df425f78c4d1e380043aa71ed429e3e5399dd81 (HEAD -> master)
Author: xxxx <xxxx@gmail.com>
Date:   Thu Jul 18 12:02:53 2019 +0800the 6th version of good.txtcommit 4d10f3f70dcf9f94a710b85317ce1480090c97d8
Author: xxxx <xxxx@gmail.com>
Date:   Thu Jul 18 12:02:03 2019 +0800the 5th version  of good.txtcommit 69160945195e7115d4677c7165894c05ee7b7cb2
Author: xxxx <xxxx@gmail.com>
Date:   Thu Jul 18 12:00:54 2019 +0800the 4th version of good.txtcommit cc120886ce52e5db1256acffff6ada62883460e9
Author: xxxx <xxxx@gmail.com>
Date:   Thu Jul 18 11:34:10 2019 +0800My 3rd version of good.txt,modifiedcommit 8668d26c7c3ea4f8e1c58bab3cdf3fa246db55c8
Author: xxxx <xxxx@gmail.com>

内容太多了!!!不方便
(如何翻屏?空格+上下键 或者 ctrl b ctrl f更多地上下翻页)

$ git log --pretty=oneline
显示Hash值、指针、信息;

3df425f78c4d1e380043aa71ed429e3e5399dd81 (HEAD -> master) the 6th version of good.txt
4d10f3f70dcf9f94a710b85317ce1480090c97d8 the 5th version  of good.txt
69160945195e7115d4677c7165894c05ee7b7cb2 the 4th version of good.txt
cc120886ce52e5db1256acffff6ada62883460e9 My 3rd version of good.txt,modified
8668d26c7c3ea4f8e1c58bab3cdf3fa246db55c8 The 2nd Version of "good.txt".
0af5132fdec24924989c612dd14a5d62f862231d My first commit new file: good.txt

还能更简便:
$git log --oneline
部分Hash值、指针、信息;

3df425f (HEAD -> master) the 6th version of good.txt
4d10f3f the 5th version  of good.txt
6916094 the 4th version of good.txt
cc12088 My 3rd version of good.txt,modified
8668d26 The 2nd Version of "good.txt".
0af5132 My first commit new file: good.txt

$git reflog
重点在于:HEAD@{数字};他表明了若需要返回某版本,需要移动几步;
比如第二行:HEAD数字为1,表明返回该版本需要移动1步;

3df425f (HEAD -> master) HEAD@{0}: commit (amend): the 6th version of good.txt
ed81d07 HEAD@{1}: commit: the 5th version of good.txt
4d10f3f HEAD@{2}: commit: the 5th version of good.txt
6916094 HEAD@{3}: commit: the 4th version of good.txt
cc12088 HEAD@{4}: commit: My 3rd version of good.txt,modified
8668d26 HEAD@{5}: commit: The 2nd Version of "good.txt".
0af5132 HEAD@{6}: commit (initial): My first commit new file: good.txt

——————————————————

【查看版本记录后,如何前进后退?】

版本前进后退的方式有3种:

  • (1).基于索引值;(推荐)
    可前进可后退!!!
    $git reset --hard 某版本的Hash值
  • (2).^符号;(了解)
    只能回到老版本,不能去到新版本;
    $git reset --hard head^^^
    (退三个版本)
    ^符的数量代表:版本后退几步,数字填写参考git reflog

但如果后退的数字很大,^写太多很麻烦,用~来解决;

  • (3).~符号;(了解)
    只能回到老版本,不能去到新版本;
    $git reset --hard head~数字
    (数字代表:退几个版本,数字填写参考git reflog

git reset --hard 索引值
其中的索引值:
填写oneline的简写也就可以,因为简写也能唯一确认索引的全称,就能定位唯一版本;

$git reflog good.txt

3df425f (HEAD -> master) HEAD@{0}: commit (amend): the 6th version of good.txt
ed81d07 HEAD@{1}: commit: the 5th version of good.txt
4d10f3f HEAD@{2}: commit: the 5th version of good.txt good.txt
6916094 HEAD@{3}: commit: the 4th version of good.txt good.txt
cc12088 HEAD@{4}: commit: My 3rd version of good.txt,modified
8668d26 HEAD@{5}: commit: The 2nd Version of "good.txt".
0af5132 HEAD@{6}: commit (initial): My first commit new file: good.txt

$git reset --hard 6916094
HEAD is now at 6916094 the 4th version of good.txt good.txt
——————————————————

hardsoft 以及mixed参数】


上一张有疑问,版本前进后退时:$git reset --hard Hash
reset好理解,为什么要用到hard???

  • --soft
    只在【本地库】移动HEAD指针(不会接触暂存区和工作区);

  • --mixed
    在【本地库】移动HEAD指针;
    重置暂存区;

  • --hard
    在【本地库】移动HEAD指针;
    重置暂存区;
    重置工作区;

——————————————————

【z找回已commit到本地库的删除文件】

路径下创建一个文件a.txt,git add且commit,再把他删除;
$vim a.txt
$git add a.txt
$git commit -m"The 1st version of a.txt" a.txt
[master 2d024e8] The 1st version of a.txt
1 file changed, 2 insertions(+)
create mode 100644 a.txt
$git status
On branch master
nothing to commit, working tree clean
$rm a.txt (删除)

$git status

On branch master
Changes not staged for commit:(use "git add/rm <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)deleted:    a.txt (红色)no changes added to commit (use "git add" and/or "git commit -a")

其实只要记录进入了本地库(git commit),这条记录就永远不会被抹去;
GIT只会增加版本,不会删除任何版本;
查看git reflog,删除文件前的版本也可以看到;

$git reflog

2d024e8 (HEAD -> master) HEAD@{0}: commit: The 1st version of a.txt
3df425f HEAD@{1}: reset: moving to 3df425f
6916094 HEAD@{2}: reset: moving to 6916094
3df425f HEAD@{3}: commit (amend): the 6th version of good.txt
ed81d07 HEAD@{4}: commit: the 5th version of good.txt
4d10f3f HEAD@{5}: commit: the 5th version of good.txt
6916094 HEAD@{6}: commit: the 4th version of good.txt
cc12088 HEAD@{7}: commit: My 3rd version of good.txt,modified
8668d26 HEAD@{8}: commit: The 2nd Version of "good.txt".
0af5132 HEAD@{9}: commit (initial): My first commit new file: good.txt

第一条就是a.txt的信息,git reset --hard 他的哈希即可;
——————————————————

【add到暂存区的删除文件找回】

——————————————————

git diff】比较文件

git diff:把【工作区】的文件和【暂存区】进行比较;
git diff [ 本地库中历史版本 ] [ 文件名 ]:把【工作区】中的文件和【本地库】历史记录比较;

add后要这么写:git diff HEAD apple.txt
也可以跟指定历史版本比较:git diff HEAD^ apple.txt
——————————————————

【Branch 分支】

只要有本地库,就会有master分支;
分支是一个指针!!!
SVN中也有分支,但因为太难用,所以没人用;

  • $git branch -v :查看所有分支;
    哪个代表当前分支?分支名绿色,且名字前有个*号;

  • $git branch 分支名:【创建分支】;

  • $git checkout 分支名:【切换分支】;
    返回:Switched to branch ‘分支名’

  • $git merge 分支名:【合并分支】
    1.切换到接受修改的分枝上(如果分支2合并到分支1上,那么当前分支必须是分支1!!!)
    2.git merge 分支名
    (这两步的分支名是不相同的,注意!!!)

  • $git:【解决冲突】

我想在分支hot_fix上修改代码(现在也在hot_fix上)
$git branch -v

  • hot_fix a90ea80 The 1st version of apple.txt
    master a90ea80 The 1st version of apple.txt

在hot_fix上修改apple.txt,然后add commit;
$vim apple.txt
$git add apple.txt
$git commit apple.txt
[hot_fix 5ac1089] The 1st version of apple.txt by hot_fix
1 file changed, 1 insertion(+)

因为要把hot_fix的文件merge到master上,所以要切换到master分支;
$git checkout master
D a.txt
Switched to branch ‘master’

合并hot_fix的文件到master;
$git merge hot_fix
Updating a90ea80…5ac1089
Fast-forward
apple.txt | 1 +
1 file changed, 1 insertion(+)

查看合并后的文件apple.txt,发现合并成功;
$cat apple.txt

apple.com
apple.com
apple.com.cn
apple.com.cn
apple.com.cn aaaaaaaaaahhhhhhhhhh
this line edited by hot_fix

【Conflict 冲突】

1.编辑文件,删除特殊符号;
2.把文件改到满意程度,保存退出;
3.git add
4.git commit -m""(此处不能添加文件名!!!)

merge后产生了冲突怎么解决?

  • 提问:为什么会产生冲突?
    如果在多个分支下,都修改了同一行文字,而且都改得不一样;
    同时,这多处修改都不应该被轻易舍弃,那么git就不知道听谁的;

Master分支下:
vim good.txt(第一行最后添加edited by master
git add good.txt
git commit -m"The 7th version of good.txt by master" good.txt

hot_fix分支下:
vim good.txt(第一行最后添加edited by hot_fix
git add good.txt
git commit -m"Edited of good.txt by hot_fix" good.txt

在master中,merge hot_fix:
$git merge hot_fix

Auto-merging good.txt
CONFLICT (content): Merge conflict in good.txt
Automatic merge failed; fix conflicts and then commit the result.

自动合并失败!
合并失败后,文件保留依旧;

vim good.txt看一看:
出现了特殊标记:<<<<<<< HEAD=======

<<<<<<< HEAD
hahahahahhahahah edited by master
=======
hahahahahhahahah edited by hot_fix
。。。。。。

冲突的表现:

  • <<<<<<< HEAD
    当前分支的内容;
  • =======
    另外一个分支的内容;

就是因为这两个分支的内容造成了冲突,让我们自己决定应该选择谁;

我们接下来应该把这两个特殊符号删除,跟另一个分支的作者讨论,应该如何选择;
然后修改代码,保存退出vim;

$ git status

On branch master
【You have unmerged paths.】(fix conflicts and run "git commit")(use "git merge --abort" to abort the merge)Unmerged paths:(use "git add <file>..." to mark resolution) both modified:   good.txt (红色)no changes added to commit (use "git add" and/or "git commit -a")

提示git add,把冲突解决;

$git commit -m"resolved" good.txt
fatal: cannot do a partial commit during a merge.
【重要】解决冲突后的git commit不能带文件名,否则会报错!!!
只要写git commit加注释!!!

$git commit -m"resolved"
[master aade6c2] resolved

merge完毕

$git status
On branch master
nothing to commit, working tree clean
——————————————————

哈希算法 16 Bytes

把明文(音频、视频、图片都可)通过 哈希加密算法 转换为密文;

哈希是一系列的加密算法,虽然各个哈希算法加密强度不同,但是有这些共同点:

  • 1.不管输入数据有多大,输入同一个哈希算法(比如MD5),得到的加密长度都固定;
  • 2.哈希算法确定,输入数据确定,输出结果能保证不变;
  • 3.哈希算法确定,输入数据有变化,输出数据一定有变化,而且变化非常大;
  • 4.哈希算法不可逆;
  • 5.哈希算法底层是SHA-1算法;

哈希算法可以被用来验证文件,
——————————————————

【本地库和远程库】

这里选择github.com
此章节需要回顾之前画的图,页面搜索:1.团队内 协同开发图

本地新建一个文件夹,设置签名,写一个文档;
git init
git add 文件名
git commit -m"" 文件名

新建远程库(远程库随意命名)

本地库和远程库都有了,怎么PUSH?


本地别名:
这个远程库的URL很长,每次都复制也很麻烦,我们可以在本地用别名保存这个URL;
git remote -v
git remote add 名字 链接


——————————————————

【Push】

git push 远程库链接 分支名
比如:git push


——————————————————

【Clone】

作用:
1.远程库下载到本地;
2.创建远程库别名;
3.初始化本地库;

进入选定的路径后:
git clone 远程链接 (远程链接或者本地别名)

链接在此处:

Clone下来后,$ls -la就能看到:除了文档
还有.git文件,这个文件就代表本地库初始化好了;

而且克隆的同时,还自动把项目经理那的【本地别名】在本地也配置好了;
git remote -v即可查看;
——————————————————

【邀请成员加入团队】

Repository - Settings - Collaborators - Add

成员打开invite link后:

这样,成员就能git push 远程库链接 分支名了!!!
成员就有【写】的权限了;
——————————————————

【拉取 PULL = FETCH + MERGE】

(这里面:远程库别名和远程库链接可以互换)

  • git fetch 远程库链接 远程分支名
    FETCH只是把远程内容下载到本地,没有权限验证,本地工作区文件也不会变化;

项目比较复杂时,不要先贸然merge,先看看下下来的文件什么样子?

  • git checkout 远程库别名/分支
  • cat 文件名

合并远程文件;

  • git merge 远程库别名/分支

https://www.bilibili.com/video/av24441039/?p=38
——————————————————
——————————————————
——————————————————
——————————————————
——————————————————
——————————————————

GIT图形化界面操作




Git工作原理
对于任何一个文件,在 Git 内都只有三种状态:

  • 已提交(committed):表示该文件已经被安全地保存在本地数据库中了;
  • 已修改(modified):表示修改了某个文件,但还没有提交保存;
  • 已暂存(staged): 表示把已修改的文件放在下次提交时要保存的清单中。

Git 管理项目时,文件流转分为三个工作区域:

  • Git 的工作目录
  • 暂存区域(索引文件)
  • 本地仓库。

基本的Git工作流程:
1.在工作目录中修改文件。
2.暂存文件,将文件的快照放入暂存区域。
3.提交更新,找到暂存区域的文件,将快照永久性存储到Git仓库目录。

每个项目都有一个 Git 目录,它是 Git 用来保存元数据和对象数据库的地方。该目录非常重要,每次克隆镜像仓库的时候,实际拷贝的就是这个目录里面的数据。
从项目中取出某个版本的所有文件和目录,用以开始后续工作的叫做工作目录,这些文件实际上都是从 Git 目录中的压缩对象数据库中提取出来的,接下来就可以在工作目录中对这些文件进行编辑。
所谓的暂存区域只不过是个简单的文件,一般都放在 Git 目录中。有时候人们会把这个文件叫做索引文件,不过标准说法还是叫暂存区域。
————————————————————————————
什么是好的代码存储库?

版本控制系统(或存储库)可以有三种主要类型:

  • 1.本地 - 所有开发人员都在同一个文件系统中。
  • 2.集中式 - 项目团队在中央服务器上.具有单个项目副本,团队成员将更改提交给一个副本。
  • 3.分布式 - 开发人员在本地存储库中工作,更改在存储库之间共享。

Git repos需要重点考虑;
因为Git repos是对开发团队最有效率的功能,一个好的repo中存在如下组件:

1.Pull request:当用户对源代码进行更改并将其推送到存储库时,他们的协作者将被通知以获得讨论和审查的更改。
2.基于Web的代码审查
3.Editing:如果一个仓库建议实时和协作的编辑,它增加了很多回购的质量。
4.Bug tracking:没有任何项目没有bug。所以当存储库允许协同跟踪和解决错误时,这是非常好的。
5.支持缩写语法,使代码可读。
6.双因素身份验证,确保用户帐号的保护。
7.创建库的fork和clone
8.可与任何用户共享代码段或文件片段,包括团队成员,其他用户甚至自己。
9.与第三方服务集成
10.导入资料库:当用户从一个服务转移到另一个服务时,存储库允许他们导入现有项目。
11.开源许可证:有时,企业需要在自己的服务器上部署内部存储库,而不是使用现有的公共Web资源。
——————————————————————————
目前,所有程序员使用的Git存储库主要有三个:
Github / BitBucket / GitLab ,如何选择?

每个存储库都有其优缺点,也都有广大的程序员基础。
这三个存储库管理服务中,只有GitLab是开源的,该系统具有良好的界面,易于安装。GitLab社区版的源代码在他们的网站上可用,企业版本是专有的。拥有最大数量的开源项目的GitHub本身不是开源的。Bitbucket也不是开源的,但是购买stand-alone 版本时,将提供完整的源代码与产品配置选项。

综合多种情况,建议:

如果你需要开源解决方案,请选择GitLab 。
如果你喜欢使用Atlassian服务,BitBucket 是最佳选择。
GitHub 是承载开源代码项目的领先平台。

——————————————————————————

托管网站常见术语解释

Clone:Clone一个项目指仓库会存储在本地计算机上;
Fork:Fork一个项目指除了复制他的仓库,还复制了包括文件 / 提交历史 / issues和其余一些东西;复制后的仓库在你自己的托管网站帐号下,但是你本地计算机对这个仓库没有任何操作;

下列是托管网站:
——————————————————————————

Github (by Windows)

https://github.com
GitHub是git存储库和协作项目开发的首选网站。该系统旨在允许用户轻松创建基于Git的特殊版本控制系统。为什么这么受欢迎?Git通过非线性开发历史的可视化工具和导航工具的帮助,支持流畅的版本合并和分割。现在,GitHub拥有5000多万个开源项目。

缺点:
不开源;
大小限制:文件大小不能超过100Mb,存储库可以托管信息1Gb。
——————————————————————————

BitBucket (by Atlassian)

https://bitbucket.org/
BitBucket服务也非常类似于GitHub,但是它的大部分功能也略有不同。BitBucket最适合小型开发团队(5人以下,包括5人可免费),随着团队的成长,BitBucket提供了与GitHub和GitLab相比更温和的定价条件。BitBucket还为团队提供了灵活的部署模式。

优点:
对于小团队免费最:多5名成员的团队将获得无限数量的免费存储库;
缺点:
不开源;
系统不稳定
——————————————————————————

GitLab

https://about.gitlab.com/
GitLab服务也是基于Git版本控制开发的。尽管GitLab功能与其主要竞争对手GitHub类似,但仍有一些主要特点。GitLab有几种不同的形式,如适用于企业的GitLab SAAS,以及用户的个性化解决方案GitLab Community Edition。

优点:
开源;
免费,意味着可拥有无限数量的私有存储库;
缺点:
界面相对较慢;
存储库常见的技术问题;
——————————————————————————

OSChina

——————————————————————————

GITEE

https://gitee.com

——————————————————————————

参考博客:
http://tech.it168.com/a2017/1026/3176/000003176180.shtml

下列是客户端:
——————————————————————————

SourceTree

https://sourcetreeapp.com
老牌的Git GUI管理工具,也号称是最好用的Git GUI工具;
内置GitHub, BitBucket 和 Stash 的支持:直接绑定帐号即可操作远程repo;

——————————————————————————

——————————————————————————

——————————————————————————

——————————————————————————

Git安装(on Mac)

目录

安装git
创建ssh key、配置git
提交本地项目到GitHub

MAC安装于 /Users/你的用户名/.SSH
——————————————————————————

查看本机是否已安装GIT
Terminal输入:git

若已安装则会出现:

usage: git [--version] [--help] [-C <path>] [-c <name>=<value>][--exec-path[=<path>]] [--html-path] [--man-path] [--info-path][-p | --paginate | --no-pager] [--no-replace-objects] [--bare][--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]<command> [<args>]These are common Git commands used in various situations:start a working area (see also: git help tutorial)clone      Clone a repository into a new directoryinit       Create an empty Git repository or reinitialize an existing onework on the current change (see also: git help everyday)add        Add file contents to the indexmv         Move or rename a file, a directory, or a symlinkreset      Reset current HEAD to the specified staterm         Remove files from the working tree and from the indexexamine the history and state (see also: git help revisions)bisect     Use binary search to find the commit that introduced a buggrep       Print lines matching a patternlog        Show commit logsshow       Show various types of objectsstatus     Show the working tree statusgrow, mark and tweak your common historybranch     List, create, or delete branchescheckout   Switch branches or restore working tree filescommit     Record changes to the repositorydiff       Show changes between commits, commit and working tree, etcmerge      Join two or more development histories togetherrebase     Reapply commits on top of another base tiptag        Create, list, delete or verify a tag object signed with GPGcollaborate (see also: git help workflows)fetch      Download objects and refs from another repositorypull       Fetch from and integrate with another repository or a local branchpush       Update remote refs along with associated objects'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

通过HOMEBREW安装GIT

brew install git
——————————————————————————

创建ssh key、配置git:

1.设置username和email(一台电脑只需配置一次,除非重装了系统)
因为Github是分布式版本控制系统,所以使用前都需要自报家门,即用户名和邮箱;

git config --global user.name "写用户名"
git config --global user.email "写邮箱"

注:其中的“–global”是指整台计算机的配置(本机所有仓库),也可以专门配置在某仓库下。

附:查看用户名和邮箱有否配置成功:
git config --list
会返回一长串数据,最后有两行是 user.nameuser.email

2.查看username和email

git config --global user.name
返回usernamegit config user.email
返回email

或者像上一部分说的在git config --list中查看;

3.检查SSH keys是否存在
输入下面的命令(# Lists the files in your .ssh directory, if they exist)
ls -al ~/.ssh
如果有文件id_rsa.pubid_dsa.pub,则进入步骤5:将SSH key添加到GitHub中;
否则进入第4步:生成SSH key

4.通过终端命令生成SSH key
ssh-keygen -t rsa -C "邮箱"

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxxx/.ssh/id_rsa):
Created directory '/Users/xxx/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/xxxx/.ssh/id_rsa.
Your public key has been saved in /Users/xxxx/.ssh/id_rsa.pub.The key fingerprint is:
略 xxx@xxx.comThe key's randomart image is:
+---[RSA 2048]----+
略
+----[SHA256]-----+

为什么会有 randomart image?
相比超长字符串,人们更容易接受图形。让我们对比两幅图片的差异比对比两个超长字符串也要容易的多。这就是为什么现在大家使用二维码,而不是复制粘贴 URL 的原因。
他通过将 Key 转换成有规律的图片,让人可以更加容易的、快速的对比 Key 的异同。

5.将SSH key添加到GitHub中
随便哪个记事本文件打开id_rsa.pub文件,里面的信息即为SSH key,将这些信息复制到GitHub的Add SSH key页面即可。

Github.com添加SSH Key指引:



当然,这个复制粘贴过程在不同操作系统上也有不同的命令:

mac
pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboardwindows
clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboardlinux
sudo apt-get install xclip
# Downloads and installs xclip. If you don't have `apt-get`, you might need to use another installer (like `yum`)xclip -sel clip < ~/.ssh/id_rsa.pub

6.在本地Terminal链接验证
ssh -T git@github.com

若成功则返回:

Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.

7.提交本地项目到GitHub


剩下的步骤:https://www.jianshu.com/p/7edb6b838a2e

—————————————————————

忘记SSH密码

忘记密码是无法找回的,建议新建一个SSH密钥;

$ cd ~/.ssh
$ ls  // 看得出是存在SSH密钥的
cp      id_rsa.pub  known_hosts
id_rsa      key_backup$ rm

把本地文件夹中的东西删光:
/Users/xxx/.ssh

// 创建新SSH密钥,输入emial
$ ssh-keygen -t rsa -b 4096 -C "chiujingye@163.com"
Generating public/private rsa key pair.Enter file in which to save the key (/Users/chiu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 无密码
Enter same passphrase again:
Your identification has been saved in /Users/chiu/.ssh/id_rsa.
Your public key has been saved in /Users/chiu/.ssh/id_rsa.pub.The key fingerprint is:
SHA256:hAKfoHAtX0S8S0Lt3v44xgyDJzu3MdQRJB7SITWqKlw chiujingye@163.com
The key's randomart image is:
+---[RSA 4096]----+
|. +.oBOoo        |
|.o.+o*== .       |
|.  +=oo.o        |
|   .o.+o .       |
|  .E =.oS        |
|... o.* .        |
|o.   +o*         |
|.   o .o*.       |
|     o.o.o.      |
+----[SHA256]-----+
// 查看密钥$ cat ~/.ssh/id_rsa.pubssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCtLtUlr+z4J89RATHrtlBJ5gg3j+o6DhvrJAYdRSQKyVhd5yAX4kscJ1huW/ZBHcd9aQmA28vKBTRCBl3qx7pj/2Wpo2ctlWoCJa0fm//lkDr5AL6G6P8QxdddDNwoLpU4PB/9ed7ac2pUXCR0oVFDurk0XxROdrKlYKDAfd+OHmEQVsQdhutWbhcuLPXKoy6TqT2xcpWan3BRkIOZxO4v81i+MkkqHEer/fuTwH8kwCDsM0TK8ZzVd9PeVxbfR3+49S9wYw8J3ziJX4XXBuAwMlX3Js+YpRlNIVtsuNCyr2A7DDFfKf9FhkHngxzX+m1W71OtC5RrpvhFyh+mDcz/bqaerPa4joDmeWPhzTdOO3g6WPLumHIA/pau0UpR2cws8jZs6GPOai7JY2953asl51Qd5hB/wr7TQKWZW5CZpvpLOjJTcOaegqRIoUm4R8eb7PlUF0cJM0j36il+6/G6hcatToPBPGDFQLyTKA89uOUfsaJlQ6WpCyapqLFUK7NGKMd1QdEWw3Bn8NdCf3zqGZ3r25ZVc+uV4PzGVDPFMQcJGtspUcvDfPRelFzGJwZBZ1wdGE7+iAPFSk+T7cFqlpxiWwHeof1jDqHGDHWy4n4zeU6/v910J26Jp2KaAC1RkWOcpb4d/b/Qlv8UMtomLAtBLrQZJ1ubkpjTMnaE8Q== chiujingye@163.com

去GITHUB更新新密钥吧;
复制ssh-rsa开头的这一整篇代码(密钥);


—————————————————————

参考博客:
https://blog.igevin.info/posts/generate-ssh-key-for-git/
https://www.jianshu.com/p/7edb6b838a2e

—————————————————————————————————————————

Git介绍

若项目需要更改,比如从Version1改成Version2,可以手动修改;
但如果改动规模过大,比如改成3/4,5/6,容易遗漏细节,或者回溯版本历史时不知道既定版本间具体改了哪些部分,就造成事故;
这时就需要用到Git

Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理;
他是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件

Git的好处:

  • 1.牛逼;
  • 2.完整版本控制功能,解决多人协作问题;
  • 3.提高开发效率;
  • 4.是程序工作的好习惯
  • 注:Git与Github没有关系!!

——————————————————————————————————————————

GIT疑问

1.Clone了Git项目到本地,用IDE(IDEA)打开后无法正常运行?

如图:文件LOGO异常


选择这个项目的本地地址




此时,LOGO都显示正常了。
可能后续还有步骤。。。未完待续
—————————————————————————————————————————

Git安装(on Windows)

一.下载

版本:git-2.12.1-64-bit.exe 官网即可下载(慢)

  • ✓ Use Git from the Windows
  • ✓ Use the OpenSSL library
  • ✓ Checkout Windows - style,comment Unix-style line endings
  • ✓ Use MinTTY(The default terminal of MSYS2)
  • ✓ Enable file system cashing
  • ✓ Launch git bash

二.Git 配置

注:建议从git bash中操作,而不是DOS,因其功能更强大; git bash在桌面右击即可见到。

1.配置用户名

  • git config --global user name "imooc"

一个空格加两杠" --",意思是布置全局,不加代表个性化布置。且两杠与"global"间不得有空格,否则会报错。

  • 注:若此处报错,可能是初始化有误,先输入 git init 回车即可。
    2.配置邮箱
  • git config --global user.email "imooccode@163.com"
    3.其他配置
  • 若安装了KDiff3则需要添加此句,若未安装不需添加。
  • git config --global merge.tool "kdiff3"
  • 让git不要插手Windows/Unix换行符转换的事。
  • git config --global core.autocrlf false
    4.编码配置
  • 配置utf-8字符编码。
  • git config --global gui.encoding utf-8
  • 避免git status显示的中文编码乱码。
  • git config --global core.quotepath off
  • Windows上还需要配置下式,意思是本地仓库git“忽略大小写”是打开还是关闭,true 打开,false 反之,区分大小写。
  • git config --global core.ignorecase false
    5.验证Git安装
  • git --version

三.Git SSH Key Pair 配置

作用:自动化发布脚本,免密码,包括拉取和提交git push时。

1.在Windows/Linux下,git bash输入:

  • ssh-keygen -t rsa -C "imooccode@163.com"
  • 一路回车,不要输入任何文字,直至生成SSH Key Pair。
    2.生成密钥
  • ssh-add ~/.ssh/id_rsa
    3.生成公钥
  • cat ~/.ssh/id_rsa.pub
    [geely@geely~]]$ cat ~/.ssh/id_rsa.pubssh-rsa AAAAB...             ----------------...                          此段为公钥文本imooccode@163.com            ----------------[geely@geely~]]$
  • 注:此步骤易出现报错:Could not open a connection to your authentication agent,解决方法在后文:Git 问题汇总。
  • copy公钥文本,将其粘贴至代理管理网站,本次教程使用 码云 https://gitee.com/

5.常用命令

  • 检查本机公钥:$ cd ~/.ssh
  • 若出现 No such file or directory 表明是第一次使用git。
  • 若需清除本机原有ssh密钥:
  • $ mkdir key_backup$ cp id_rsa* key_backup$ rm id_rsa*
  • 生成一个新密钥:
  • $ ssh-keygen -t rsa -C “邮箱地址”

——————————————————————————————

报错

————————————

  • 生成公钥时报错:Could not open a connection to your authentication agent 解决办法
    先执行 ssh-add ~/.ssh/rsa
    再执行 eval`ssh-agent`(此处" "符应为键盘1左边此键,且为半角,例句里是全角)。 若成功,用ssh-add -l` 查看,是否生成了新的rsa。

————————————

  • /c/Users/Administrator/.ssh/rsa: No such file or directory
    没有RSA这个文件或者目录。

————————————

  • Error loading key “/c/Users/Administrator/.ssh/rsa”: Is a directory
    rsa是一个目录(指ssh-add命令只能制定在文件上,而不是目录上)。

————————————

  • bash: syntax error near unexpected token 'newline’
    原因: 符号「<」和「>」 是重定向字符,是特殊字符有特殊意义。
    解决: 去掉两个尖括号【<】和【>】。

————————————
删除分支时,当前分支不能为带删除的分支(当前分支要调整成其他分支)
例如要删除v1.0,要把当前分支switch成master
$ git checkout master

————————————

  • Switch分支时报错:Your local changes to the following files would be overwritten by checkout
    Please commit your changes or stash them before you switch branches.
    当前分支有未跟踪的文件,checkout 命令会覆盖它们,请缓存( stash )或者提交( commit )。
    ①未跟踪文件的内容改动很重要,保存修改
//第一种方式 存到暂存区
git add.
git stash
//取出的时候使用
git stash pop//第二种方式 发起一个commit 存到提交历史
git add.
git commit -m "commit message"
  • ②未跟踪文件的内容改动不重要,放弃修改
  • 推荐做法:清除未跟踪文件
git clean n  //这个是清除文件预览
git clean -f //强制清除文件
  • 另一种做法:强制切换分支
  • 强制切换分支命令如下,结果同提示说的那样,会直接覆盖未跟踪的文件。这个方式我觉得很是粗暴,我们日常切换的时候,还是不要使用 -f 强制切换,没有覆盖提示,很容易发生文件修改丢失,但是我们自己不知道。
    git checkout -f <branch>


  • GIT分区分析:
  • https://blog.csdn.net/qq_32452623/article/details/75645578

————————————

  • idea新建maven项目没有resource文件夹
    File --> project structure --> Modules --> Sources --> main右键 --> New Folder, 输入resources
    选择resources右键,点击Resources(选择文件资源类型),然后Apply,OK即可

————————————

  • git commit 后:git push origin master时报错
 ! [rejected]        master -> master (fetch first)
Merge branch 'master' of github.com:JYCN/springcloudtest
error: failed to push some refs to 'git@github.com:JYCN/springcloudtest.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.

$ git pull origin master
$ git push origin master
————————————

————————————

————————————

————————————

————————————

————————————

————————————

————————————

————————————

————————————

参考博客:
1.https://blog.csdn.net/qq_33528613/article/details/78678327
2.https://www.cnblogs.com/Tsing-Evan/p/8445020.html
3.https://blog.csdn.net/qq_37869130/article/details/82380529
4.https://blog.csdn.net/qq_32452623/article/details/75645578

【分布式版本控制系统】GIT 托管网站 客户端相关推荐

  1. 分布式版本控制系统Git——使用GitStack+TortoiseGit 图形界面搭建Git环境(服务器端及客户端)(转)...

    近期想改公司内部的源码管控从TFS为git,发现yubinfeng大侠有关git的超详细大作,现将其转载并记录下,以防忘记,其原博客中有更加详细的git及.net开发相关内容.原文地址:http:// ...

  2. 分布式版本控制系统Git与项目托管平台Github相关概念、操作方法、开发流程与常用命令

    tip:有问题或者需要大厂内推的+我脉脉哦:丛培森 ٩( 'ω' )و [本文源址:http://blog.csdn.net/q1056843325/article/details/54667602 ...

  3. [.net 面向对象程序设计进阶] (27) 团队开发利器(六)分布式版本控制系统Git——在Visual Studio 2015中使用Git...

    [.net 面向对象程序设计进阶] (26) 团队开发利器(六)分布式版本控制系统Git--在Visual Studio 2015中使用Git 本篇导读: 接上两篇,继续Git之旅 分布式版本控制系统 ...

  4. 分布式版本控制系统Git学习资源收集汇总

    伴随着知乎上一个问题:GitHub 是怎么火起来的?被顶起200+的回答说到:Github不是突然火起来的,在Ruby社区Github其实从一开始就很流行,我们2009年搞Ruby大会就邀请了Gith ...

  5. 分布式版本控制系统Git的安装与使用(作业2)

    (本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103) 分布式版本控制系统Git的安装与使用 一.安装Git b ...

  6. 分布式版本控制系统Git的安装与使用

    作业要求 1.(本次作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2103 2.  我的Github远程仓库地址: ht ...

  7. 分布式版本控制系统 Git 教程

    简介 Git 是什么? Git 是一个开源的分布式版本控制系统. 什么是版本控制? 版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 什么是分布式版本控制系统? 介绍分布 ...

  8. 分布式版本控制系统Git的下载、安装与使用其复制GitHub项目代码的方法

      本文介绍分布式开源版本控制系统Git的下载.安装,并基于Git实现克隆GitHub中项目代码的方法.   Git是一款开源软件,因此我们直接在Git的官方下载地址(https://git-scm. ...

  9. 分布式版本控制系统Git的安装和使用

    作业要求来自https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 GitHub远程仓库的地址:https://github.com/ ...

最新文章

  1. 使用memcache做web缓存
  2. indexedDB数据库的使用
  3. 解密:面部特征点检测的关键技术
  4. (chap2 简单的Http协议) 请求报文和响应报文
  5. 华为防火墙USG多出口网络场景是如何排除故障的
  6. spring boot项目怎么记录用户操作行为和登录时间_6 个 Github 项目拿下 Spring Boot
  7. Application Wizard生成的项目文件简介
  8. 百度文库下载工具(引言及使用教程)
  9. 将android手机屏幕投影到电脑端
  10. 【推荐】智慧城市顶层设计及其建设规划实施解决方案可行性研究报告合集(共107份,963M)
  11. 【硬刚大数据】从零到大数据专家之Apache Doris篇
  12. nodejs+vue+elementui+python酒店客房预订网站系统java
  13. Mobile-Unet
  14. This scheduler instance is still active but was recovered by another instance in the cluster.
  15. ffmpeg common.mak:159: *** missing separator. Stop.
  16. 数据可视化图表总结(一)
  17. 什么是百度SEO?百度SEO优化怎么做?
  18. JavaScript 隐式类型转换
  19. Python同步学习(一)
  20. Brat文本注释工具的使用文档

热门文章

  1. django mezzanine添加多语言支持
  2. 【剑桥摄影协会】Photoshop中的USM算法(Unsharp Mask)
  3. Oracle 登陆数据库的方式
  4. tkinter 设置不可编辑_jquery 设置页面元素不可点击、不可编辑、只读(备忘)
  5. Java后台生成多个Excel并用Zip打包后(可以将excel文件放置到不同的目录)下载
  6. excel图片技巧:如何为报表配上节日祝福动画
  7. 价值百万的企业大数据分析报告是如何炼成的?
  8. PPC与PC无线连接(Wi-Fi)
  9. HTML5基础实例(三)
  10. freeipa 客户端 Kerberos认证 SSSD关系