免費(fèi)空間網(wǎng)站推薦百度地圖網(wǎng)頁版進(jìn)入
前言
那么這里博主先安利一些干貨滿滿的專欄了!
首先是博主的高質(zhì)量博客的匯總,這個(gè)專欄里面的博客,都是博主最最用心寫的一部分,干貨滿滿,希望對大家有幫助。
- 高質(zhì)量博客匯總
然后就是博主最近最花時(shí)間的一個(gè)專欄《Git企業(yè)開發(fā)控制理論和實(shí)操》希望大家多多關(guān)注!
- Git企業(yè)開發(fā)控制理論和實(shí)操
Git的遠(yuǎn)程操作
博主使用碼云(Gitee)進(jìn)行講解,操作方法和Github完全一樣。
理解分布式版本控制系統(tǒng)
我們目前所說的所有內(nèi)容(工作區(qū),暫存區(qū),版本庫等等),都是在本地!也就是在你的筆記本或者計(jì)算機(jī)上。而我們的 Git 其實(shí)是分布式版本控制系統(tǒng)!什么意思呢?
可以簡單理解為,我們每個(gè)人的電腦上都是一個(gè)完整的版本庫,這樣你工作的時(shí)候,就不需要聯(lián)網(wǎng)了,因?yàn)榘姹編炀驮谀阕约旱碾娔X上。既然每個(gè)人電腦上都有一個(gè)完整的版本庫,那多個(gè)人如何協(xié)作呢?比方說你在自己電腦上改了文件A,你的同事也在他的電腦上改了文件A,這時(shí),你們倆之間只需把各自的修改推送給對方,就可以互相看到對方的修改了。
分布式版本控制系統(tǒng)通常也有一臺充當(dāng)“中央服務(wù)器”的電腦,但這個(gè)服務(wù)器的作用僅僅是用來方便“交換”大家的修改,沒有它大家也一樣干活,只是交換修改不方便而已。有了這個(gè)“中央服務(wù)器”的電腦,這樣就不怕本地出現(xiàn)問題導(dǎo)致文件丟失了。
中央倉庫我們叫“遠(yuǎn)程倉庫”,有個(gè)很牛的網(wǎng)站叫做Github
就是中央倉庫的一個(gè)作用!
當(dāng)然我這篇文章的讀者可能有部分伙伴訪問國外網(wǎng)站比較慢,這里博主會用國內(nèi)的一個(gè)替代網(wǎng)站,叫做碼云來進(jìn)行演示。
創(chuàng)建遠(yuǎn)程倉庫
在Gitee上新建倉庫
簡單理解issues
和pull request
Gitee這個(gè)平臺,為我們提供了四種管理者的身份。
遠(yuǎn)程倉庫是由自己的成員的。
這個(gè)issues
就是一些有問題的人和倉庫成員交流的一個(gè)地方。
pull request
是什么呢?
首先對于開發(fā)者來說,肯定不是在master
上進(jìn)行開發(fā)的,我們可能是在dev
分支上開發(fā),然后我們就要進(jìn)行merge
操作。
當(dāng)然,我們知道merge
操作是有風(fēng)險(xiǎn)的。因此我們不能隨隨便便讓人來merge
。
因此,想要進(jìn)行merge
的開發(fā)人,就要寫一個(gè)PR(pull request),可以理解成“合并申請單”,讓倉庫管理人看。
管理員同意后,我們才能進(jìn)行merge
操作。
克隆遠(yuǎn)程倉庫
這些都是Gitee給我們提供的數(shù)據(jù)傳輸協(xié)議,常用的就是https和ssh。
HTTPS協(xié)議clone
使用這里給的網(wǎng)址,復(fù)制,然后使用 git clone
命令。
git clone https://gitee.com/Yufch/remote-gitcode.git
注意:不能在任何本地倉庫下執(zhí)行這個(gè)命令。
這個(gè)就是遠(yuǎn)程倉庫下有的東西了。用法和之前講的完全一樣。
git remote -v # 查看遠(yuǎn)程倉庫的一些信息
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git remote -v
origin https://gitee.com/Yufch/remote-gitcode.git (fetch)
origin https://gitee.com/Yufch/remote-gitcode.git (push)
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$
表示我們是有遠(yuǎn)程倉庫的fetch
權(quán)限(拉權(quán)限),和push
權(quán)限(推權(quán)限)。
SSH協(xié)議clone
ssh協(xié)議是采用一個(gè)公鑰加密和公鑰登陸的一個(gè)機(jī)制。所以我們必須要把自己本地服務(wù)器的一個(gè)公鑰放到git服務(wù)器上進(jìn)行管理。
我們現(xiàn)在還沒有配置公鑰。
**第一步:創(chuàng)建SSH Key。**在用戶主目錄下,看看有沒有.ssh
目錄,如果有,再看看這個(gè)目錄下有沒有id_rsa
和 id_rsa.pub
這兩個(gè)文件,如果已經(jīng)有了,可直接跳到下一步。如果沒有,需要?jiǎng)?chuàng)建SSH Key。
ls -al ~ | grep .ssh # 可以搜索一下~下有沒有.ssh文件夾,如果沒有就要?jiǎng)?chuàng)建SSH Key
這個(gè)命令就是系統(tǒng)的知識了,這里不過多講解。
設(shè)置ssh公鑰需要配置郵箱,然后這個(gè)郵箱要和碼云上的一致。這里可以看。
然后配置SSH公鑰命令如下。
ssh-keygen -t rsa -C "xxx.com"
(base) [yufc@ALiCentos7:~]$ ssh-keygen -t rsa -C "yufc779@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yufc/.ssh/id_rsa):
Created directory '/home/yufc/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yufc/.ssh/id_rsa.
Your public key has been saved in /home/yufc/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:1PWuf6UgtkssYGh2x5QasdfaSrD4YmS7EL0Yf7VamusiQ yufc779@gmail.com
The key's randomart image is:
+---[RSA 2048]----+
| .+.. . |
| . + . o + . . |
|..o o o * . . |
| .o+ o * . |
|oo+ B * S . |
|o+.= B o .o .. .|
|E.. . o ..oo.. ..|
| o. . o. .. .|
| .o. .. .. |
+----[SHA256]-----+
(base) [yufc@ALiCentos7:~]$
后面的東西全部默認(rèn)回車即可。
(base) [yufc@ALiCentos7:~/.ssh]$ cat id_rsa.pub
ssh-rsa AAAAB3NzaasdasdfadSMIpxADoRltTaMiJadsfasdflBld/qF7EHq+KwJY4CMhb65A6tsvGPxAPAeYNgufjc4LC5r0v2hdgs0Pk86XIX7vUK6DUrpe6LPTBOdAGTsWmSbcynpJhX97mdM0P2p/3HK0KYmlymyAIzPpEyNF5YRF9goDzI6OBw7Y9WZlp0QWPyWRbnSMOe4olXKY7EFcon5uCrlD3vJNac/3ZIZpKy2TrtvPvcm1fk9sJW5JlcC7P6DByL0CG8ohxGwz2/4l yufc779@gmail.com
(base) [yufc@ALiCentos7:~/.ssh]$
這個(gè)東西就是你的公鑰。弄到碼云上即可。
此時(shí)就可以clone了。
git clone git@gitee.com:Yufch/remote-gitcode.git
向遠(yuǎn)程倉庫推送
準(zhǔn)備操作
本地倉庫要進(jìn)行一次push
操作,將本地某個(gè)分支的內(nèi)容推送到遠(yuǎn)程倉庫中某個(gè)分支上。
然后別忘了,進(jìn)去remote-gitcode
倉庫之后,要配置一下name和email。這個(gè)要和碼云上的一致。
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config -l
sendpack.sideban=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitee.com:Yufch/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config user.name "Yufch"
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config user.email "yufc779@gmail.com"
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git -l # 這里打錯(cuò)了 ...
Unknown option: -l
usage: git [--version] [--help] [-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>]
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git config -l
sendpack.sideban=false
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@gitee.com:Yufch/remote-gitcode.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
user.name=Yufch
user.email=yufc779@gmail.com
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ # 這樣就配置好了
那么現(xiàn)在我們先創(chuàng)建一個(gè)文件。然后把他添加到版本庫中先(上一章的內(nèi)容)
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ ls
README.en.md README.md
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ vim file.txt
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ cat file.txt
hello gitee
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git add .
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git commit -m "create file.txt"
[master 7352d02] create file.txt1 file changed, 1 insertion(+)create mode 100644 file.txt
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$
git push
命令
git push origin master:master
# origin: 遠(yuǎn)程倉庫的名字:一般都叫origin, 使用 `git remote` 命令可以查看
# 第一個(gè)master: 要推送的本地分支
# 第二個(gè)master: 要推送到的遠(yuǎn)程倉庫的分支
# 如果冒號左右兩邊一樣,可以省略一個(gè)
git push origin master
此時(shí)就可以在遠(yuǎn)程倉庫中看到我們修改的內(nèi)容了。
拉取遠(yuǎn)程倉庫
如果現(xiàn)在的情況是,遠(yuǎn)程倉庫中的代碼是領(lǐng)先于本地倉庫的(可能別人開發(fā),然后push上去了),我們想在本地看到遠(yuǎn)程倉庫中的內(nèi)容,我們就要進(jìn)行拉取操作。
假設(shè)有人加了一行hello world
,然后push上去了,我們在本地也想看到這一行。
注意:在工作中,千萬不要直接改碼云或者github上的代碼,要改也是拉到本地然后改完去PR。
git pull origin master:master
# origin: 遠(yuǎn)程倉庫的名字:一般都叫origin, 使用 `git remote` 命令可以查看
# 第一個(gè)master: 要拉取的遠(yuǎn)程倉庫的分支
# 第二個(gè)master: 要拉取到的本地倉庫的分支
# 如果冒號左右兩邊一樣,可以省略一個(gè)
git pull origin master
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git pull origin master:master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From gitee.com:Yufch/remote-gitcode7352d02..8513266 master -> master
Warning: fetch updated the current branch head.
Warning: fast-forwarding your working tree from
Warning: commit 7352d02962528eae4ef12d0669a40f4f7cdf35ec.
Already up-to-date.
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$
注意:拉下來的分支之后,會做一個(gè)合并的操作,所以pull = 拉取 + merge
忽略特殊文件
在日常開發(fā)中,我們有些文件不想或者不應(yīng)該提交到遠(yuǎn)端,比如保存了數(shù)據(jù)庫密碼的配置文件,那怎么讓 Git知道呢? 在 Git 工作區(qū)的根目錄下創(chuàng)建一個(gè)特殊的.gitignore
文件,然后把要忽略的文件名填進(jìn)去,Git 就會自動(dòng)忽略這些文件了。不需要從頭寫.gitignore
文件,gitee 在創(chuàng)建倉庫時(shí)就可以為我們生成,不過需要我們主動(dòng)勾選下。
當(dāng)然我們剛才創(chuàng)建的時(shí)候沒選,我們就自己創(chuàng)建一個(gè)就行。
touch .gitignore
之類的意思就是讓git忽略.so
結(jié)尾的和.ini
結(jié)尾的文件。
如下所示:按道理來說,我們創(chuàng)建了.gitignore
,key.so
,token.ini
文件之后,git status
會識別到工作區(qū)中有三個(gè)文件被創(chuàng)建,但是如下所示,git status
只看到.gitignore
被創(chuàng)建了。這就是因?yàn)槲覀冊O(shè)置了忽略。
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ touch key.so
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ touch token.ini
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# .gitignore
nothing added to commit but untracked files present (use "git add" to track)
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]
此時(shí)如果git add .
,剛才那兩個(gè)被設(shè)置過忽略的文件是不會被提交到暫存區(qū)的。
但是在這條件下我還是想提交一個(gè)key2.so
怎么辦呢?可以使用以下命令。
git add -f key2.so # 強(qiáng)制add
但是我們一般不想去破壞 .gitignore
文件的規(guī)則的,所以git還提供給我們一種語法。
這樣就可以排出忽略。
然后如果有一天,你的.gitignore
文件寫了很長很長,然后你創(chuàng)建一個(gè)key3.so
,你自己都忘記為什么這個(gè)被忽略掉了,可以通過一個(gè)命令查看為什么被忽略。
git check-ignore -v key.so # 查看一個(gè)文件為什么被忽略
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git check-ignore -v key.so
.gitignore:4:*.so key.so
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$
然后我們就能看到,是第四行讓它被忽略了。
最后我們推送一下,遠(yuǎn)端就能看到這些文件了。
配置命令別名
如果我覺得我經(jīng)常要使用git status
命令,但是它太難打了,想起個(gè)別名:git st
行嗎?可以的。不過我們要進(jìn)行一些配置。
git config --global alias.st status
**這個(gè)命令的意思就是,給status
起別名為st
。**是否要--global
可以自選。
現(xiàn)在git st
就能用了。
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git st
# On branch master
nothing to commit, working directory clean
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$
再配一個(gè)吧。
我們以前學(xué)過這個(gè)。
git log --pretty=oneline --abbrev-commit
起個(gè)別名。
git config --global alias.lpa 'log --pretty=oneline --abbrev-commit'
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$ git lpa
7393ea0 add .gitignore
8513266 update file.txt.
7352d02 create file.txt
a9af182 Initial commit
(base) [yufc@ALiCentos7:~/Src/Bit-Courses/GitDevelopment/remote-gitcode]$
不建議初學(xué)的時(shí)候配置,建議大家熟悉之后,在工作中的時(shí)候再去簡化。