git常用问题
sunyongjian opened this issue · 3 comments
git的公钥
https 和 SSH 的区别:
-
前者可以随意克隆github上的项目,而不管是谁的;而后者则是你必须是你要克隆的项目的拥有者或管理员,且需要先添加 SSH key ,否则无法克隆。
-
https url 在push的时候是需要验证用户名和密码的;而 SSH 在push的时候,是不需要输入用户名的,如果配置SSH key的时候设置了密码,则需要输入密码的,否则直接是不需要输入密码的。
创建本地公有秘钥
$ cd ~/.ssh && ls
进入该文件夹下检查有没有id_rsa.pub 或 id_dsa.pub 文件,如果存在 则已经创建,没有需要创建
//配置git用户名和邮箱:
$ git config user.name "用户名"
$ git config user.email "邮箱"
$ ssh-keygen -t rsa -C "邮箱"
//多个密钥的情况下,可生成ssh key同时指定保存的文件名
$ ssh-keygen -t rsa -f ~/.ssh/ellacf -C "邮箱"
代码参数含义:
- -t 指定密钥类型,默认是 rsa ,可以省略。
- -C 设置注释文字,比如邮箱。
- -f 指定密钥文件存储文件名。
执行后,会填写保存两种密钥的文件夹,和passphrase,全部可以按enter。然后执行ls来查看生成后的文件。
- id_rsa和id_rsa.pub分别是私有密钥和公有密钥。
- 我们指定的文件名就是id_rsa.github,这时~/.ssh目录下会多出id_rsa.github和id_rsa.github.pub两个文件,id_rsa.github里保存的就是我们要使用的key。
多个密钥
- 新增config文件
touch ~/.ssh/config
在config里面添加
Host *.github.com
IdentityFile ~/.ssh/id_rsa.github
User '用户名'
公钥添加到github
- 查看并copy
- 查看copy
cat ~/.ssh/id_rsa.pub
或者直接copy
pbcopy < ~/.ssh/id_rsa.pub
2.登录github ,个人中心 ,ssh key ,添加 Add SSH key
链接测试
$ssh -T git@github.com
修改本地 .git config文件
就是把https的链接方式
修改远程仓库地址
有时候push不上去,提示 the project you were looking for could not be found. 可能是远程仓库地址换了。
- 删除 git remote rm origin
- 添加 git remote add origin git@github.com:
- 修改 git remte origin set-url URL
删除的文件恢复
批量
git ls-fies -d | xargs git checkout --
git 默认不区分文件名大小写
- 配置git 使其对文件名大小写敏感
git config core.ignorecase false
多个github账号的问题
- 最简单的就是在当前的项目里更改username,当前项目的username就是 单独的
git config user.name 'sunyongjian'
git config user.email 'sunyongjian@1111.com'
- 配置秘钥(key)...
查看某个commit 具体的代码更改
- git show 哈希值
git diff
git diff //查看尚未暂存的文件更新了哪些部分
git diff filename //查看尚未暂存的某个文件更新了哪些
git diff –cached //查看已经暂存起来的文件和上次提交的版本之间的差异
git diff –cached filename //查看已经暂存起来的某个文件和上次提交的版本之间的差异
git diff hash1 hash2 //查看某两个版本之间的差异
git diff hash1:filename hash2:filename //查看某两个版本的某个文件之间的差异
分支问题
- feature分支
//首先从develop切除feature分支
$ git checkout -b feature-xxx develop
//开发完切回develop
$ git checkout develop
//然后进行合并,-no-ff 参数,以保持分支的合并历史
$ git merge --no-ff feature/xxx
//没问题就可以删除feature分支了
$ git branch -d feature/xxx
- hot fix
从远程 master 切出分支,不要将本地分支 feature 功能带上
git checkout -b hot-fix origin/master
查看log
-
查看某个文件历史修改记录
git log src/index.js -
一行展示
--oneline -
作者
--author="sunyongjian" -
过滤合并
--no-merges / merges -
根据提交信息
--grep="" -
查看跟关联的分支和tag,比如从这个commit提交到origin/develop
--decorate -
结合展示分支结构,merge的去向
--graph -
按作者分,所有的commit
git shortlog -
diff改动
--stat哪些文件有改动,改动的行
-p具体的代码 -
按数量
-10 -
自定义格式
%cn、%h 和 %cd 这三种占位符会被分别替换为作者名字、缩略标识和提交日期。
git log --pretty=format:"%cn committed %h on %cd" -
查看某个commit的改动
git show hash -
查看历史操作
git reflog可以撤销一些不可逆的操作。比如reset之后,找到reset之前的commit hash,再reset
git show HEAD === git log -1 -p
如何补交自己的 github commit
利用 git commit --date 的操作,可以指定 commit 的时间。这个是属于补交的操作。如果有修改 commit 时间的操作,就需要用到 git commit --ament,进入交互模式修改。
首先,获取到你想补交的时间。mac 下的获取时间的命令是 date,另外 commit 的时间格式是英文的标准时间,这个可以通过 date -R 获取。但是如何获取指定的标准时间呢。有两种情景:
-
一个是你想补交昨天,或者前天的 commit,你也不知道具体日期。那就利用
-v参数,date -v -1d表示获取昨天的时间,即 -2d 就是前天,以此类推。date -v +1d表示明天,虽然这个没啥用。-1y就表示去年了。最终就是date -R -v -1d... -
另一种情景是,你知道了具体的日期,快速格式化。
date -R -j -f %Y-%m-%d 2018-01-01
得到时间后,就可以补交 commit 啦。
Commit message 规范
- feat: A new feature
- fix: A bug fix
- docs: Documentation only changes
- style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- refactor: A code change that neither fixes a bug nor adds a feature
- perf: A code change that improves performance
- test: Adding missing tests or correcting existing tests
- build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
- ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle,BrowserStack, SauceLabs)
- chore: Other changes that don't modify src or test files
- revert: Reverts a previous commit
// 打标签
git tag -a v0.0.x
git push origin 0.0.x