git 命令行使用(基礎篇)

  git 是分佈式代碼管理工具,愈來愈多的企業使用它。因此掌握git的使用相當重要。它的遠端管理是基於ssh,因此在使用遠端git的時候須要進行ssh配置。
  ssh是一種免密登陸,使用的rsa非對稱加密來進行服務器認證;html

1、git 的ssh配置以下

首先你須要擁有gitlab或是github的一套帳號,而後設置git的user name 和 user email:

一、全局配置帳號

# git 全局設置帳號
git config --global user.name "your name"
git config --global user.email "your e-mail"

二、根據 user name 和 user email 生成本身的ssh密鑰

cd $USER 
cd .ssh #若是存在,須要備份能夠備份下,若是不須要能夠直接覆蓋

ssh-keygen -t rsa -C "your e-mail" #生成 ssh

執行結果:
圖片描述git

最終獲得 id_rsa 和 id_rsa.pubgithub

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

打開https://gitlab.company.com/ ,使用本身帳號登陸,而後添加ssh密鑰;
主頁頭像 -> profile -> SSH Keys -> Add keyshell

clipboard.png
將id_rsa.pub文件中的內容添加上去,而後點擊Add key;
圖片描述服務器

四、測試 ssh 連接gitlab.company.com

ssh git@gitlab.company.com

#會出現以下提示,說明連接成功
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.

2、git 的簡單使用

建立一個新的倉庫
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

本地倉庫和遠端倉庫創建鏈接app

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 將項目多索引提交到本地倉庫的當前分支下

git commit -m '註解'

四、git 將遠端倉庫信息更新到本地,而且merge到本地分支

git pull origin master

五、git 推送當前分支到遠端分支

git push origin master

六、git 將遠端倉庫信息更新到本地,不合併到本地分支

git fetch

七、git 建立分支操做

git branch -a #查看全部分支
git branch --list #查看本地分支
git branch feature/20181017.business.chao #建立分支
git checkout feature/20181017.business.chao #切換分支
#或直接建立而且切換
git checkout -b feature/20181017.business01.chao
git push origin feature/20181017.business01.chao #推送到遠端
git pull origin feature/20181017.business01.chao #從遠端拉取

#刪除
git branch -d/-D feature/20181017.business01.chao #刪除本地
git push origin --delete feature/20181017.business01.chao #刪除遠端分

八、git 查看當前分支的狀態

git status

九、git merge合併分支

#當前分支 git checkout feature/20181017.business01.chao
touch 1.txt
vi 1.txt # 編輯內容而後提交到遠端
git checkout git checkout feature/20181017.business.chao
git merge feature/20181017.business01.chao #合併本地分支

十、git grep 查找當前分支文件中的內容

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

十一、git diff 比較分支

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

十二、git log 查看commit記錄

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

1三、git rm 刪除文件以及索引

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

1四、git tag 通常用來管理【里程碑】

git checkout master
git tag --list #查看全部標籤
git tag v1.0 #添加tag
git tag --list 'v1.*' # 列出version爲1的全部子版本

1五、git show 獲取項目的詳細信息

git show #展現項目的各類類型


commit eaa7f945204bed8f2b01d284d99fcf0b3ac3029e
Author: chao <chao@yourcompany.com>
Date:   Wed Oct 17 06:16:26 2018 +0000

    add README

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

1六、git rebase 重置當前的操做options,寫的不錯rebase詳解

git rebase branchName #消除本分支下面的commit
git rebase --continue #在rebase的過程當中,也許會出現衝突(conflict). 在這種狀況,Git會中止rebase並會讓你去解決 衝突;在解決完衝突後,用"git-add"命令去更新這些內容的索引(index), 而後,你無需執行 git-commit,只要執行
git rebase --abort #在任什麼時候候,你能夠用--abort參數來終止rebase的行動,而且"mywork" 分支會回到rebase開始前的狀態

1七、git stash 儲藏(臨時保存到git棧中,獨立於git分支管理以外。那個分支使用恢復到哪一個分支就好了)

#儲藏使用名稱和不使用名稱保存未提交到倉庫的(暫存和非暫存)
git stash 
git stash save "stash-name-1"
#變動統計
git stash show
#每次儲藏
git stash list
#恢復到git版本管理中
git stash pop #彈出棧
#經過名字使用儲藏
git stash apply 
#刪除
git stash drop
#清空
git stash clear
#直接使用stash建立分支
git stash branch testbranch

1八、git revert 向前滾動的方式回滾,不會丟失源代碼

git revert commit_id

1九、git reset 使用HEAD的狀態的方式回滾,會丟失源代碼

git reset --hard HEAD^         #回退到上個版本
git reset --hard HEAD~3        #回退到前3次提交以前,以此類推,回退到n次提交以前
git reset --hard commit_id     #退到/進到 指定commit的sha碼
git push origin HEAD --force   #強推到遠端,使用git push 推送推送失敗

20、git bisect 經過二進制搜索找到引入錯誤的更改

git bisect start #開啓查找
git bisect good v2.6.18 
git bisect bad master #查找報錯的版本

2一、刪除並忽略已經加入到git倉庫的文件

#查找到對應的文件
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
#加入git忽略
echo .DS_Store >> ~/.gitignore
#add
git add --all
#提交忽略
git commit -m '.DS_Store banished!'
相關文章
相關標籤/搜索