日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

git 命令行使用(基础篇)

發布時間:2025/3/19 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 git 命令行使用(基础篇) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

??git 是分布式代碼管理工具,越來越多的企業使用它。所以掌握git的使用至關重要。它的遠端管理是基于ssh,所以在使用遠端git的時候需要進行ssh配置。
??ssh是一種免密登錄,使用的rsa非對稱加密來進行服務器認證;

一、git 的ssh配置如下

首先你需要擁有gitlab或是github的一套賬號,然后設置git的user name 和 user email:

1、全局配置賬號

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

2、根據 user name 和 user email 生成自己的ssh密鑰

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

執行結果:

最終得到 id_rsa 和 id_rsa.pub

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

打開https://gitlab.company.com/ ,使用自己賬號登錄,然后添加ssh密鑰;
主頁頭像 -> profile -> SSH Keys -> Add key


將id_rsa.pub文件中的內容添加上去,然后點擊Add key;

4、測試 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.

二、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 mastertouch .gitignore vi .gitignore #編輯 git add .gitignore git commit -m "add .gitignore" git push -u origin master

本地倉庫和遠端倉庫建立連接

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 indexbisect Find by binary search the change that introduced a bugbranch List, create, or delete branchescheckout Checkout a branch or paths to the working treeclone Clone a repository into a new directorycommit Record changes to the repositorydiff Show changes between commits, commit and working tree, etcfetch Download objects and refs from another repositorygrep Print lines matching a patterninit Create an empty Git repository or reinitialize an existing onelog Show commit logsmerge Join two or more development histories togethermv Move or rename a file, a directory, or a symlinkpull Fetch from and merge with another repository or a local branchpush Update remote refs along with associated objectsrebase Forward-port local commits to the updated upstream headreset Reset current HEAD to the specified staterm Remove files from the working tree and from the indexshow Show various types of objectsstatus Show the working tree statustag Create, list, delete or verify a tag object signed with GPG

1、git 倉庫初始化

git init

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

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

3、git 將項目多索引提交到本地倉庫的當前分支下

git commit -m '注解'

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

git pull origin master

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

git push origin master

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

git fetch

7、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 #刪除遠端分

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

git status

9、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 #合并本地分支

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

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

11、git diff 比較分支

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

12、git log 查看commit記錄

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

13、git rm 刪除文件以及索引

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

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

git checkout master git tag --list #查看所有標簽 git tag v1.0 #添加tag git tag --list 'v1.*' # 列出version為1的所有子版本

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

git show #展示項目的各種類型commit eaa7f945204bed8f2b01d284d99fcf0b3ac3029e Author: chao <chao@yourcompany.com> Date: Wed Oct 17 06:16:26 2018 +0000add READMEdiff --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

16、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開始前的狀態

17、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

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

git revert commit_id

19、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 #查找報錯的版本

21、刪除并忽略已經加入到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!'

總結

以上是生活随笔為你收集整理的git 命令行使用(基础篇)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。