前端 2018-06-09 10:06:26

常见问题:

1. git clone 出现 Permission denied, please try again.

未添加sshkey 见2.1

2.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The ECDSA host key for gitlab.daiyou-inc.com has changed,
and the key for the corresponding IP address 47.110.232.207
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 ECDSA key sent by the remote host is
SHA256:OmEmGshUsIDomPjjsrVx7E9PZH3NkAZxL9k4pWBHfxo.
Please contact your system administrator.
Add correct host key in /Users/alfred/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/alfred/.ssh/known_hosts:21
ECDSA host key for gitlab.daiyou-inc.com has changed and you have requested strict checking.
Host key verification failed.

删除 /Users/alfred/.ssh/known_hosts:21内容

3 git push error:remote unpack failed: index-pack abnormal exit

git远程分支消失,导致git push出现

remote: error: object file /var/opt/gitlab/git-data/repositories/daiyou-frontend/daiyou-manager-fed.git/objects/b0/e02a2bbc880dcf045e0d921bfd31a896f31f0e is empty
remote: fatal: cannot read existing object info b0e02a2bbc880dcf045e0d921bfd31a896f31f0e
error: remote unpack failed: index-pack abnormal exit
To http://xxx.git
 ! [remote rejected] feature/version-1.2.0 -> feature/version-1.2.0 (unpacker error)
error: failed to push some refs to 'http://xxx.git'

用sourcetree降分支重置到出问题的提交前一个提交,然后创建新分支

4 git 提交413

error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large fatal: The remote end hung up unexpectedly Writing objects: 100% (138/138), 14.27 MiB | 16.53 MiB/s, done. Total 138 (delta 33), reused 0 (delta 0) fatal: The remote end hung up unexpectedly

提交数据量太大
解决:从http改成ssh方式 git remote set-url origin ssh://xxx.git

git操作

1.用户信息

git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git config --global core.editor  // 设定你喜欢的编辑软件

2.配置信息

git config --list

2.1 git ssh key生成

  1. 生成 ssh-keygen -o -t rsa -b 4096 -C <gitlab账号>
  2. 拷贝公钥 pbcopy < /Users/abley/.ssh/id_rsa.pub
  3. 添加 gitlab ->setting -> sshkeys
  4. 验证 ssh -T <gitlab地址>

3.获取帮助

git help

4.初始化仓库

git init
git add .
git commit -m 'initial project version'

5.克隆现有的仓库

git clone https://github.com/libgit2/libgit2

自定义本地仓库的名字

git clone https://github.com/libgit2/libgit2 mylibgit

检查当前文件状态

git status

忽略文件

文件 .gitignore 的格式规范如下: 所有空行或者以 # 开头的行都会被 Git 忽略。 可以使用标准的 glob 模式匹配。 匹配模式可以以(/)开头防止递归。 匹配模式可以以(/)结尾指定目录。 要忽略指定模式以外的文件或目录,可以在模式前加上惊叹号(!)取反。

文件对比

git diff  // 暂存前后的变化

git diff --staged // 已经暂存起来的变化

文件提交

git commit 
git commit -v 

这会将你所做的改变的 diff 输出放到编辑器中从而使你知道本次提交具体做了哪些修改。 git commit -m "Story 182: Fix benchmarks for speed" 将提交信息与命令放在同一行 git commit -a Git 就会自动把所有已经跟踪过的文件暂存起来一并提交

文件删除

git rm // 从暂存区域移除 并连带从工作目录中删除指定的文件

--cached 不删除物理文件,仅将该文件从缓存中删除;

提交回滚

git revert  commit id

操作提交历史记录

git rebase -i "commit id"^

查看提交历史

git log
git log -p,用来显示每次提交的内容差异
git log -2,显示最近两次提交
git log --stat,看到每次提交的简略的统计信息
git log --pretty=format:"%h - %an, %ar : %s"  可以定制要显示的记录格式。 这样的输出对后期提取分析格外有用

当 oneline 或 format 与另一个 log 选项 --graph 结合使用时尤其有用。 这个选项添加了一些ASCII字符串来形象地展示你的分支、合并历史

选项 说明
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 --date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明

git log 限制

选项说明

-(n)

仅显示最近的 n 条提交

--since, --after

仅显示指定时间之后的提交。

--until, --before

仅显示指定时间之前的提交。

--author

仅显示指定作者相关的提交。

--committer

仅显示指定提交者相关的提交。

--grep

仅显示含指定关键字的提交

-S

仅显示添加或移除了某个关键字的提交

git show 显示提交详情

  1. 查看最新的commit
git show
  1. 查看指定commit hashID的所有修改:
git show commitId
  1. 查看某次commit中具体某个文件的修改:
git show commitId fileName

撤销操作

git commit --amend

有时候我们提交完了才发现漏掉了几个文件没有添加,或者提交信息写错了, 尝试重新提交:

git reset HEAD <file> 取消文件暂存
git checkout -- <file> 撤消对文件的修改

文件移动

git mv file_from file_to

远程仓库

git branch -a 查看远程分支 (git branch 本地)
git remote 查看你已经配置的远程仓库服务器
git remote -v 会显示需要读写远程仓库使用的 Git 保存的简写与其对应的 URL。
git remote add <shortname> <url>   添加远程仓库
git fetch [remote-name] 从远程仓库中抓取与拉取
git push origin master 推送到远程仓库
git remote show [remote-name]  查看某一个远程仓库的更多信息
git remote rename pb paul 远程仓库重命名
git remote rm paul 远程仓库移除
git remote set-url origin http://gitlab2.dui88.com/frontend/tuia-ssp-manager-node.git 更换仓库地址

标签

git tag 列出标签
git tag -a v1.4 -m 'my version 1.4' 附注标签
git show v1.4 看到标签信息与对应的提交信息
git tag v1.4-lw 轻量标签
git tag -a v1.2 9fceb02(校验和(或部分校验和))  后期打标签
git push origin v1.5 显式地推送标签到共享服务器上
git push origin --tags 把所有不在远程仓库服务器上的标签全部传送到那里
git checkout -b [branchname] [tagname]  检出标签  在特定的标签上创建一个新分支

别名

git config --global alias.co checkout
git config --global alias.ci commit  

如果不想每次都输入完整的 Git 命令,可以通过 git config 文件来轻松地为每一个命令设置一个别名 示例: git config --global alias.unstage 'reset HEAD --'

这会使下面的两个命令等价: git unstage fileA git reset HEAD -- fileA 取消暂存文件

分支

git merge hotfix合并
git branch iss53 创建分支
git checkout iss53 切换分支
git checkout -b iss53 新建一个分支并同时切换到那个分支上
git branch -d iss53 删除分支

删除暂存区内容

git rm --cached "文件路径",不删除物理文件,仅将该文件从缓存中删除;
git rm --f  "文件路径",不仅将该文件从缓存中删除,还会将物理文件删除(不会回收到垃圾
git rm -r "文件路径"

--cached

不删除物理文件,仅将该文件从缓存中删除;

--f

不仅将该文件从缓存中删除,还会将物理文件删除(不会回收到垃圾桶)

关于Git的忽略文件的语法规则

忽略文件中的空行或以井号(#)开始的行将会被忽略。 可以使用Linux通配符。例如:星号(*)代表任意多个字符,问号(?)代表一个字符,方括号([abc])代表可选字符范围,大括号({string1,string2,...})代表可选的字符串等。 如果名称的最前面有一个感叹号(!),表示例外规则,将不被忽略。 如果名称的最前面是一个路径分隔符(/),表示要忽略的文件在此目录下,而子目录中的文件不忽略。 如果名称的最后面是一个路径分隔符(/),表示要忽略的是此目录下该名称的子目录,而非文件(默认文件或目录都忽略)。

冲突解决

  • Step 1.

    Fetch and check out the branch for this merge request git fetch origin git checkout -b feature/test origin/feature/test

  • Step 2. Review the changes locally

  • Step 3. Merge the branch and fix any conflicts that come up git checkout develop git merge --no-ff feature/test

  • Step 4. Push the result of the merge to GitLab git push origin develop

当前仓库合并另一个仓库的代码

现在有个仓库A,需要合并仓库B的代码

在仓库A新建一个基于仓库B的分支

git checkout -b feature/test_merge2 vue-template/master

合并两个分支

git merge feature/test_merge2 --allow-unrelated-histories

参考资料:

https://git-scm.com/book/zh/v2