Git相關:git 命令&SSH Key

git 命令

克隆的3種方法遠程倉庫:

git clone git倉庫地址
git clone git倉庫地址 文件夾名
git clone -b 分支名 git倉庫地址 文件夾名


查看信息相關

git config --list 查看配置
git config --list --global 查看全局配置
git config --list --local 查看本地配置(本地配置優先於全局配置)
git branch 查看本地分支
git branch -a 查看遠程分支
git log 查看當前分支提交歷史
git log --oneline 查看單行的簡潔歷史
git log --oneline -n4 查看最近的四條簡潔歷史
git log --all 查看所有分支的提交歷史
git log --all --graph 查看圖形化的歷史
git log --oneline --all -n4 --graph 查看所有分支最近 4 條單行的圖形化歷史
git shortlog 查看簡潔日誌(按 提交者 分組)
git diff 查看當前所以文件和暫存區文件差異
git diff 文件名 查看目標文件和暫存區的差異
git status 查看當前本地分支狀態


操作相關

git branch 分支名 新建分支
git checkout 分支名 切換分支
git checkout -b 分支名 創建並切換分支

git add 文件名 添加目標文件
git add . 添加當前所有改動的文件
git add ."後綴名" 添加所有該後綴的文件
git add 文件路徑 添加所有該路徑下的文件

git checkout . 放棄所有當前修改

git commit -m"備註" 提交更改,添加備註
git commit --amend 修改上次提交的commit信息(進入編輯模式)
git commit --amend -m "修改的commit信息" 直接修改上次提交的commit信息
git reset commit號 回退到當前commit

git pull origin 遠程分支名:本地分支名 拉取遠程分支到本地
git push origin 本地分支名:遠程分支名 推送本地分支到遠程
git checkout -b 分支名 origin/分支名 在本地有分支的情況下拉取遠程的新分支

git branch -d 分支名 刪除本地分支
git branch -D 分支名 強制刪除本地分支

刪除遠程分支:
git branch -r -d origin/分支名
git push origin [空格]:分支名

修改遠程git地址:
git remote rm origin
git remote add origin 地址


關於stash:

可用來暫存當前正在進行的工作, 比如:

  1. pull 最新代碼, 又不想加新commit
  2. 想緊急切到另一個分支做一寫修改
  3. 爲了fix 一個緊急的bug, 先stash, 使返回到自己上一個commit, 改完bug之後再stash pop, 繼續原來的工作
git stash
git stash pop

關於Merge:

一般會有兩種場景:

  • master切出分支做修改,然後合入master中

                             master
                               /
      C0 ---- C1 ---- C2 ---- C4
                               \
                               C3 ---- C5
                                        \
                                      debugdev
    
  • master切出分支做修改,master同時也在修改,然後合入master中

                                     master
                                       /
      C0 ---- C1 ---- C2 ---- C4 ---- C6
                               \      /
                               C3 ----C5
                                       \
                                    debugdev
    

使用方法:

  • 在debugdev上完成開發後,切到要merge到的分支上:git checkout master
  • 將debugdev分支merge過來:git merge debugdev

Mac本地生成SSH Key的方法

  • 打開終端

  • 鍵入命令 ~ » ssh-keygen -t rsa -C "[email protected]"

  • 一路回車。當問詢到是否覆蓋時,鍵入 y 回車

  • 祕鑰生成位置在 ~/.ssh/id_rsa.pub

  • 可以用 cat 命令方便複製出來 ~/.ssh » cat ./id_rsa.pub

  • 將該 SSH Key 加入到配置到你的服務器中:github 或 gitlib

    步驟中 » 前是執行命令的位置,不是命令內容;id_rsa爲私鑰,請妥善保管。


其它問題

遇到下面問題,可能需要重新配置 SSH Key

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

遇到下面問題,可能可以通過刪除 known_hosts 解決

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The RSA host key for gitlab.xx.com has changed,
and the key for the corresponding IP address xx.xx.xx.xx
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Please contact your system administrator.
Add correct host key in /xx/xx/.ssh/known_hosts to get rid of this message.
Offending RSA key in /xx/xx/.ssh/known_hosts:9
RSA host key for gitlab.xx.com has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

known_hosts 文件位置:~/.ssh/known_hosts

遇到下面的問題,可以通過刪除該 host 的方法解決

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:YGXpYu6n0YD/0fLpD/3rJ+hiLqTzyr3G3nz7cSxsjeY.
Please contact your system administrator.
Add correct host key in /Users/zhaoyidong/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/zhaoyidong/.ssh/known_hosts:8
ECDSA host key for cn.sf.rtc.openfin.com has changed and you have requested strict checking.
Host key verification failed.
執行 ssh-keygen -R + host
發佈了32 篇原創文章 · 獲贊 8 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章