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 fetch origin 分支名:分支名 拉取远程分支

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 commit --amend 修改最后一条commit信息

回退远程提交:
git reset <commitId>
git checkout .
git push origin <branch>:<branch> --force

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

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

Tag相关:
git tag <tagName> 创建本地Tag
git push origin <tagName> 推送Tag到远程仓库
git tag -d <tagName> 删除本地Tag
git push origin :refs/tags/<tagName> 删除远程Tag


关于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

常见问题

 ! [已拒绝]          dev        -> dev  (非快进式)
   8581983..c371323  dev        -> origin/dev

git fetch origin dev 获取远程dev分支的修改
git merge origin dev 合并远程dev分支
git pull origin dev 更新本地的代码


Mac本地生成SSH Key的方法

  • 打开终端

  • 键入命令 ~ » ssh-keygen -t rsa -C "youremail@example.com"

  • 一路回车。当问询到是否覆盖时,键入 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值