web

Git

Version Control

Posted by Lorry on August 25, 2018

文章字数:2364, 阅读全文大约需要:6 分钟

概述

之前零零散散的写过一些 git 的东西, 现在统一放个地方吧, 肯定不全, 但确实是我用到的, 那些不用的指令就请移步官方咯.

全局设置 git 用户名(设置本地,具体某个项目的话将 global 改成 local 即可)

git config –global user.name

git config –global user.email

生成公私钥 先~/.ssh查看是否有,如果有则备份删除,之后使用git生成的ssh密钥会放在这个文件夹

git-keygen -t rsa -C “email@email.com”

将ssh的公钥添加至github或其他线上仓库的设置中.

自有 github 账号与公司 phab 账号的的多账户设置(仅适用于 mac 系统, 需要使用 mac 的 keychain)

在ssh根目录添加config文件, 内容如下:

# phab
Host phab.srv.codemao.cn
    AddKeysToAgent yes
    UseKeychain yes
    HostName phab.srv.codemao.cn
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
# github
Host github.com
    AddKeysToAgent yes
    UseKeychain yes
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github-rsa

git clone ssh://

如果是第一次克隆会提示是否信任该地址,需输入yes才能正确克隆

当我们有新需求要开发的时候, 尽量另开分支. 下面就举例 test.

git branch test git checkout test

也可以使用简略语

git checkout -b test

功能开发完毕后将 test 分支合并到 master 上

git merge test

新功能开发完成之后需要删除

git branch -d test

将已开发功能上传到远端

git push origin test

如果远端没有branchname的话会在远端重新新建一个

删除远端分支

git push origin :test

切换远端的 master

git push origin test:master

注意此时会将原来的 master 删除

切换到某个版本

查看版本

git log

如果安装了 oh-my-zsh, 可以尝试 glola 图形化表示, 更多的 alias 可参考, 记录复制你要回滚的git 版本号, 假设是 abcdef,

git reset –hard abcdef

如果这时再做的任何修改都会在 master 下, 提交代码会提示不是最新, 此时可以使用下列命令强制推送到远端, 慎用, 会覆盖之前的修改.

git push -f origin master

合并提交

如果本地已经有了多个 commit , 但实际上某些 commit 事可以合并在一起进行提交的.(便于团队协作, 更高效的辨明文件的更改) 可以使用 rebase -i 交互工具, 顾名思义, rebase 是重新 基于, 假设现在有3个 commit, 需要把这三个 commit 放在一起进行提交, 可以使用

git rebase -i HEAD~3

从远端合并代码, 有很多方式, git pull 和 git merge, merge 的话可以使用 squash 将别人的 commit 给忽略掉, 在 arc 的时候会方便一点

git merge –squash origin/dev

解决冲突了之后再commit.

arc diff即可

git submodule 的使用

这里只记录常用命令, 更多请参考官方文档.

  • git submodule add xxx.git <dest path> 添加一个子模块到指定路径.该命令会生成.gitsubmodule的文件, 需要将其上传到远程仓库中.
  •  从远程拉取的项目中如果包含 submodule 需要使用 git submodule init 初始化,
  • 执行git submodule update 拉取最新代码
  • 如果子模块以及存在且子模块又进行了更新, 需要使用 git submodule update --recursive --remote 对远程进行更新,
  • 如果还是有问题使用 git submodule update --force --recursive --init --remote 最粗暴的解决类似错误
error: Server does not allow request for unadvertised object 247d7b9ceda3acb52fa9805bb78f206c927ffc2d
Fetched in submodule path 'xxx', but it did not contain 247d7b9ceda3acb52fa9805bb78f206c927ffc2d. Direct fetching of that commit failed.

.gitignore 的坑

如果有文件已经上传上去了, 之后再进行对.gitignore 的添加的话会导致无法 ignore 的情况, 这是因为 git 有缓存, 解决办法为:

git rm -r --cached .
git add -A
git commit -m "update .gitignore"