gitlab命令行使用(基礎(chǔ)篇)

內(nèi)容簡介:git 是分布式代碼管理工具匾二,越來越多的企業(yè)使用它汁雷。所以掌握git的使用至關(guān)重要。它的遠(yuǎn)端管理是基于ssh,所以在使用遠(yuǎn)端git的時候需要進(jìn)行ssh配置府寒。ssh是一種免密登錄,使用的rsa非對稱加密來進(jìn)行服務(wù)器認(rèn)證湿痢;執(zhí)行結(jié)果:

本文轉(zhuǎn)載自:https://segmentfault.com/a/1190000018258258购桑,本站轉(zhuǎn)載出于傳遞更多信息之目的,版權(quán)歸原作者或者來源機(jī)構(gòu)所有

git 是分布式代碼管理工具菠隆,越來越多的企業(yè)使用它兵琳。所以掌握git的使用至關(guān)重要。它的遠(yuǎn)端管理是基于ssh骇径,所以在使用遠(yuǎn)端git的時候需要進(jìn)行ssh配置躯肌。

ssh是一種免密登錄,使用的rsa非對稱加密來進(jìn)行?服務(wù)器?認(rèn)證破衔;

一清女、git 的ssh配置如下

首先你需要擁有g(shù)itlab或是github的一套賬號,然后設(shè)置git的user name 和 user email:

# git 全局設(shè)置賬號git config --global user.name "your name"git config --global user.email "your e-mail"

根據(jù) user name 和 user email 生成自己的ssh密鑰

cd $USER

cd .ssh #如果存在晰筛,需要備份可以備份下嫡丙,如果不需要可以直接覆蓋ssh-keygen -t rsa -C "your e-mail" #生成 ssh

執(zhí)行結(jié)果:

最終得到 id_rsa 和 id_rsa.pub

gitlab上面添加ssh密鑰,使用文件 id_rsa.pub读第。

打開?https://gitlab.company.com/?曙博,使用自己賬號登錄,然后添加ssh密鑰怜瞒;

主頁頭像 -> profile -> SSH Keys -> Add key

將id_rsa.pub文件中的內(nèi)容添加上去父泳,然后點(diǎn)擊Add key;


測試 ssh 鏈接gitlab.company.com

ssh git@gitlab.company.com#會出現(xiàn)如下提示盼砍,說明鏈接成功The authenticity of host 'gitlab.company.com (100.98.137.229)' can't be established.

RSA key fingerprint is SHA256:WiQ1s8RKGjQpO0ICFY3NV2Hs0axwIbrv6j6q0XJsdsc.

RSA key fingerprint is MD5:6c:8d:0f:09:31:c9:08:9b:67:48:09:7c:d9:46:65:3e.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'gitlab.company.com,100.98.137.229' (RSA) to the list of known hosts.

PTY allocation request failed on channel 0

Welcome to GitLab, your name!

Connection to gitlab.company.com closed.

二尘吗、git 的簡單使用

創(chuàng)建一個新的倉庫

git clone git@gitlab.company.com:yaoname/my-test-git-project.git

cd my-test-git-project

touch README.md

git add README.md

git commit -m "add README"git push -u origin master

touch .gitignore

vi .gitignore #編輯git add .gitignore

git commit -m "add .gitignore"git push -u origin master

本地倉庫和遠(yuǎn)端倉庫建立連接

cd existing_folder

git init

git remote add origin git@gitlab.company.com:yourname/my-test-git-project.git

git add .git commit

git push -u origin master

git基本操作講解

The most commonly used git commands are:? add? ? ? ? Add file contents to the index

? bisect? ? Find by binary search the change that introduced a bug

? branch? ? List, create, or delete branches

? checkout? Checkout a branch or paths to the working tree

? clone? ? ? Clone a repository into a new directory

? commit? ? Record changes to the repository

? diff? ? ? Show changes between commits, commit and working tree, etc

? fetch? ? ? Download objects and refs from another repository

? grep? ? ? Print lines matching a pattern

? init? ? ? Create an empty Git repository or reinitialize an existing one

? log? ? ? ? Show commit logs

? merge? ? ? Join two or more development histories together

? mv? ? ? ? Move or rename a file, a directory, or a symlink

? pull? ? ? Fetch from and merge with another repository or a local branch

? push? ? ? Update remote refs along with associated objects

? rebase? ? Forward-port local commits to the updated upstream head

? reset? ? ? Reset current HEAD to the specified state

? rm? ? ? ? Remove files from the working tree and from the index

? show? ? ? Show various types of objects

? status? ? Show the working tree status

? tag? ? ? ? Create, list, delete or verify a tag object signed with GPG

git 倉庫初始化

git init

git 生成快照并且存入項目索引

git add .? #或 git add * 提交所有文件git add README.md #提交指定文件git add folder/ #提交某個文件夾下面的所有文件

git 將項目多索引提交到本地倉庫的當(dāng)前分支下

git commit -m '注解'

git 將遠(yuǎn)端倉庫信息更新到本地,并且merge到本地分支

git pull origin master

git 推送當(dāng)前分支到遠(yuǎn)端分支

git push origin master

git 將遠(yuǎn)端倉庫信息更新到本地浇坐,不合并到本地分支

git fetch

git 創(chuàng)建分支操作

git branch -a #查看所有分支git branch --list #查看本地分支git branch feature/20181017.business.chao #創(chuàng)建分支git checkout feature/20181017.business.chao #切換分支#或直接創(chuàng)建并且切換git checkout -b feature/20181017.business01.chaogit push origin feature/20181017.business01.chao #推送到遠(yuǎn)端git pull origin feature/20181017.business01.chao #從遠(yuǎn)端拉取#刪除git branch -d/-D feature/20181017.business01.chao #刪除本地git push origin --delete feature/20181017.business01.chao #刪除遠(yuǎn)端分

git 查看當(dāng)前分支的狀態(tài)

git status

git merge合并分支

#當(dāng)前分支 git checkout feature/20181017.business01.chaotouch 1.txtvi 1.txt # 編輯內(nèi)容然后提交到遠(yuǎn)端git checkout git checkout feature/20181017.business.chao

git merge feature/20181017.business01.chao #合并本地分支

git grep 查找當(dāng)前分支文件中的內(nèi)容

git grep --text "test" #使用git grep -h 查看更多用法

git diff 比較分支

git diff master #比較兩個分支的不同

git log 查看commit記錄

git log -n 10 #查看最近10條提交記錄

git rm 刪除文件以及索引

touch 2.txtgit add 2.txtgit commit -m 'add 2.txt'git rm 2.txt #刪除已經(jīng)提交到本地倉庫的文件

git tag 一般用來管理【里程碑】

git checkout master

git tag --list #查看所有標(biāo)簽git tag v1.0 #添加taggit tag --list 'v1.*' # 列出version為1的所有子版本

git show 獲取項目的詳細(xì)信息

git show #展示項目的各種類型commit eaa7f945204bed8f2b01d284d99fcf0b3ac3029eAuthor: chao <chao@yourcompany.com>Date:? Wed Oct 17 06:16:26 2018 +0000? ? add README

diff --git a/README.md b/README.mdnew file mode 100644index 0000000..7088fed--- /dev/null+++ b/README.md@@ -0,0 +1,5 @@++# this is repositry

git rebase 重置當(dāng)前的操作options,寫的不錯?rebase詳解

git rebase branchName #消除本分支下面的commit

git rebase --continue #在rebase的過程中睬捶,也許會出現(xiàn)沖突(conflict). 在這種情況,Git會停止rebase并會讓你去解決 沖突近刘;在解決完沖突后擒贸,用"git-add"命令去更新這些內(nèi)容的索引(index), 然后,你無需執(zhí)行 git-commit,只要執(zhí)行

git rebase --abort #在任何時候觉渴,你可以用--abort參數(shù)來終止rebase的行動介劫,并且"mywork" 分支會回到rebase開始前的狀態(tài)

git stash 儲藏(臨時保存到git棧中,獨(dú)立于git分支管理之外案淋。那個分支使用恢復(fù)到哪個分支就行了)

#儲藏使用名稱和不使用名稱保存未提交到倉庫的(暫存和非暫存)git stash

git stash save "stash-name-1"#變更統(tǒng)計git stash show#每次儲藏git stash list#恢復(fù)到git版本管理中g(shù)it stash pop #彈出棧#通過名字使用儲藏git stash apply #刪除git stash drop#清空git stash clear#直接使用stash創(chuàng)建分支git stash branch testbranch

git revert 向前滾動的方式回滾座韵,不會丟失源代碼

git revert commit_id

git reset 使用HEAD的狀態(tài)的方式回滾,會丟失源代碼

git reset --hard HEAD^? ? ? ? #回退到上個版本git reset --hard HEAD~3? ? ? ? #回退到前3次提交之前,以此類推誉碴,回退到n次提交之前git reset --hard commit_id? ? #退到/進(jìn)到 指定commit的sha碼git push origin HEAD --force? #強(qiáng)推到遠(yuǎn)端,使用git push 推送推送失敗

git bisect 通過二進(jìn)制搜索找到引入錯誤的更改

git bisect start #開啟查找git bisect good v2.6.18

git bisect bad master #查找報錯的版本

刪除并忽略已經(jīng)加入到git倉庫的文件

#查找到對應(yīng)的文件find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch#加入git忽略echo .DS_Store >> ~/.gitignore#addgit add --all#提交忽略git commit -m '.DS_Store banished!'

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末宦棺,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子黔帕,更是在濱河造成了極大的恐慌代咸,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件成黄,死亡現(xiàn)場離奇詭異呐芥,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)奋岁,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門思瘟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人厦取,你說我怎么就攤上這事潮太」芴拢” “怎么了虾攻?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長更鲁。 經(jīng)常有香客問我霎箍,道長,這世上最難降的妖魔是什么澡为? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任漂坏,我火速辦了婚禮,結(jié)果婚禮上媒至,老公的妹妹穿的比我還像新娘顶别。我一直安慰自己,他們只是感情好拒啰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布驯绎。 她就那樣靜靜地躺著,像睡著了一般谋旦。 火紅的嫁衣襯著肌膚如雪剩失。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天册着,我揣著相機(jī)與錄音拴孤,去河邊找鬼。 笑死甲捏,一個胖子當(dāng)著我的面吹牛演熟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播司顿,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼芒粹,長吁一口氣:“原來是場噩夢啊……” “哼蚕冬!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起是辕,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤囤热,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后获三,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旁蔼,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年疙教,在試婚紗的時候發(fā)現(xiàn)自己被綠了棺聊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡贞谓,死狀恐怖限佩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情裸弦,我是刑警寧澤祟同,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站理疙,受9級特大地震影響晕城,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜窖贤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一砖顷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赃梧,春花似錦滤蝠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至粤攒,卻和暖如春所森,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背夯接。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工焕济, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盔几。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓晴弃,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子上鞠,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容