Git 分布式版本控制

Git 分布式版本控制

一.分布式版本控制

1.基本理念

1.实际上,在我们实际开发中,版本控制并不是在单一机器上进行的,因为很容易想到一个问题:如果本地的机器出现问题或者系统宕机,当下管理的所有版本都有可能丢失。为了避免这种危险的情况,Git被设计为一种分布式版本控制系统

2.可以简单理解为,我们每个⼈的电脑上都是⼀个完整的版本库,这样你⼯作的时候,就不需要联⽹了,因为版本库就在你⾃⼰的电脑上。既然每个⼈电脑上都有⼀个完整的版本库,那多个⼈如何协作呢?⽐⽅说你在⾃⼰电脑上改了⽂件A,你的同事也在他的电脑上改了⽂件A,这时,你们俩之间只需把各⾃的修改推送给对⽅,就可以互相看到对⽅的修改了。
 

3.分布式版本控制系统显然要比单一的本地管理更安全,因为很明显它具有更高的抗风险能力。实际上使用分布式版本控制系统时,虽然这种两台主机之间的推送也在使用,但还是比较少见——因为我们无法保证另一台主机的状态,不管是否在同一局域网,还是是否开机。因此我们实际使用,使用上图的模式更为常见。

4.上图的中央服务器充当了那个“可靠的主机”角色。在这里我们可以把这个中央服务器称为远程仓库,而我们自己的主机上的叫做本地仓库。我们可以像上图一样跟远程仓库进行交互,可以进行仓库本地克隆,推送本地内容到远程仓库,拉取远程仓库到本地仓库等。而有一些神奇的网站:他们专门为Git仓库提供托管服务,就比如Github和Gitee。我们只要在这些网站上注册自己的账号,就可以免费获得Git远程仓库了。

2.远程仓库实操

远程仓库的创建这里不作掩饰,我们直接从克隆远程仓库开始。

1.克隆远程仓库

顾名思义,克隆远程仓库就是把当前远程仓库的内容拷贝一份在我们的本地仓库。但这里需要注意一点

克隆的远程仓库和自己的本地仓库不能处于同一目录

这里掩饰通过HTTPS协议克隆。

git clone ...

wujiahao@VM-12-14-ubuntu:~$ git clone https://gitee.***/wjhwujiahao/linux-fundamentals-learning.git 
Cloning into 'linux-fundamentals-learning'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: ***pressing objects: 100% (7/7), done.
Receiving objects: 100% (7/7), 6.73 KiB | 6.73 MiB/s, done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)

可以看到,远程仓库已经成功克隆

wujiahao@VM-12-14-ubuntu:~$ ls
gitcode  hash.cpp  linux-fundamentals-learning

再来查看这个克隆仓库的文件,和我们在远程仓库中查到的文件是一样的。

wujiahao@VM-12-14-ubuntu:~$ cd ./linux-fundamentals-learning
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ la
.git  LICENSE  README.en.md  README.md
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git remote
origin

由该指令可以看到远程仓库的原名

git remote

这里看到远程仓库的原名叫origin。

2.pull与push

本地和远程仓库的协作方式也比较好理解。当本地仓库修改文件完成之后,将修改结果依次add,***mit最后push到远程仓库。而当远程仓库的内容更加领先(常见于多人协作,例如你的同事将ta的修改结果推送至远程仓库),就需要将远程仓库的内容拉取到本地仓库,具体如下图

接下来我们先实操push操作。

tips:push和pull都是对分支和分支之间的操作。

这里我们在本地仓库创建一个文件test.txt,并写入一些内容。

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git branch
* master
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ touch test.txt
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ vim test.txt

接着将修改的内容add,***mit。

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git add .
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git ***mit -m "create file.txt"
[master ***c5d75] create file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ ls
LICENSE  README.en.md  README.md  test.txt
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 ***mit.
  (use "git push" to publish your local ***mits)

nothing to ***mit, working tree clean

注意我接下来的操作,我们这里的push是分支与分支之间的操作:将本地master的修改push到远程仓库的master分支。若我们创建了更多分支,也可以对应其他分支到其他分支的操作。

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push origin master:master
Username for 'https://gitee.***': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.***': 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta ***pression using up to 2 threads
***pressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 279 bytes | 279.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.*** [1.1.5]
remote: Set trace flag 7b4123a9
To https://gitee.***/wjhwujiahao/linux-fundamentals-learning.git
   f4ba2e0..***c5d75  master -> master

这样我们就完成了push操作。此时查看Gitee上的远程仓库:


接着我们对拉取远程仓库进行实操。

需要拉取远程仓库的情况往往出现在多人协作中。例如你的同事本地仓库2push了一些新内容到远程仓库,而你的本地仓库1当然不会有任何改变。若你想获得更新的远程仓库内容,就需要远程仓库拉取操作。

这里为了方便模拟,我们直接在远程仓库中修改test.txt(注意实际操作中千万不能这样干!!!!)。

我们进行拉取操作。

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git pull origin master:master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: ***pressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 986 bytes | 986.00 KiB/s, done.
From https://gitee.***/wjhwujiahao/linux-fundamentals-learning
   ***c5d75..a05ac3a  master     -> master
   ***c5d75..a05ac3a  master     -> origin/master
warning: fetch updated the current branch head.
fast-forwarding your working tree from
***mit ***c5d75057bd872e8c72aa91ee81c660c50244ff.
Already up to date.

效果如下,pull做的工作就是拉取+合并

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ cat test.txt
hello git!!
I made some change .....

3.忽略特殊文件.gitignore

在实际开发中,我们往往一次性会修改很多文件,然后直接使用

git add .

将当前工作区所有文件都进行push。这样很方便,但同时也隐藏着一个问题:

如果我有不想被push到远程仓库的文件怎么办(比如数据库配置文件)?

这就涉及到一个重要的文件:.gitignore。

.gitignore的主要作用就是将一些文件忽略,避免将它们推送至远程仓库。我们可以在当前本地仓库目录下创建该文件。

我们不仅可以直接写文件名,还可以写某些类型的文件。

此时我们创建一个a.so,再查看git的status

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ touch a.so
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ ls
a.so  LICENSE  README.en.md  README.md  test.txt
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be ***mitted)
	.gitignore

我们发现一个有趣的现象:工作区确实发生了修改,但它记录的是.gitignore文件的创建,并没有记录a.so:它确实被忽略了。

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git add .
wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be ***mitted:
  (use "git restore --staged <file>..." to unstage)
	new file:   .gitignore

甚至:我们执行了add操作,暂存区中也没有a.so。

那么对于一些被忽略的文件,我们想正常add怎么办?

只要add -f filename即可

但为了保护.gitignore的规则,我们建议在该文件中添加!filename。

我们也可以查看某个文件为什么被忽略

git check-ignore -v a.so

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git check-ignore -v a.so
.gitignore:3:*.so	a.so

进行推送

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git push origin master:master
Username for 'https://gitee.***': wjhwujiahao
Password for 'https://wjhwujiahao@gitee.***': 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta ***pression using up to 2 threads
***pressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 332 bytes | 332.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.*** [1.1.5]
remote: Set trace flag 75467094
To https://gitee.***/wjhwujiahao/linux-fundamentals-learning.git
   a05ac3a..c68d4e6  master -> master

成功推送

4.配置命令别名

这里为status选项起名叫做st。当然即使起了别名,原来的名字也能够正常使用。

wujiahao@VM-12-14-ubuntu:~/linux-fundamentals-learning$ git config alias.st status

转载请说明出处内容投诉
CSS教程网 » Git 分布式版本控制

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买