[Git] Git 使用技巧整理
1 批量删除指定文件名的所有文件
可以执行如下文件,其中 ‘*.c’ 是要删除文件的通配符,请自行修改:
find . -name '*.c' > filesToRemove.txt for i in `cat filesToRemove.txt`; do git rm $i; done
2、使用多个 SSH Key
1)生成一个公司用的SSH-Key
运行如下命令,使用默认设置:
ssh-keygen -t rsa -C "youremail@yourcompany.com”
在~/.ssh/目录会生成id-rsa和id-rsa.pub私钥和公钥。 我们将id-rsa.pub中的内容粘帖到公司gitlab服务器的SSH-key的配置中。
2)生成一个github用的SSH-Key
运行如下命令,注意这次保存到 ~/.ssh/github
ssh-keygen -t rsa -C "youremail@your.com”
在~/.ssh/目录会生成github-rsa和github-rsa.pub私钥和公钥。 我们将github-rsa.pub中的内容粘帖到github服务器的SSH-key的配置中。
3)添加私钥
运行如下命令:
ssh-add ~/.ssh/id_rsa ssh-add ~/.ssh/github_rsa
如果执行ssh-add时提示”Could not open a connection to your authentication agent”,可以现执行命令:
ssh-agent bash
然后再运行ssh-add命令。
# 可以通过 ssh-add -l 来确私钥列表
ssh-add -l
# 可以通过 ssh-add -D 来清空私钥列表
ssh-add -D
4)修改配置文件
在 ~/.ssh 目录下新建一个config文件
touch config
添加内容:
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github5)目录结构

6)测试
运行如下命令:
ssh -T git@github.com
输出
Hi skylook! You've successfully authenticated, but GitHub does not provide shell access.
就表示成功的连上github了.也可以试试链接公司的gitlab.
参考材料
https://my.oschina.net/stefanzhlg/blog/529403
2 git submodule 常用操作
创建子模块
# Usage $ git submodule add [url] [path] # With path $ git submodule add https://github.com/laozhu/hugo-nuo themes/hugo-nuo # Without path $ cd themes $ git submodule add https://github.com/laozhu/hugo-nuo
子模块引用文件
子模块引用文件位于工程的 .gitmodules 文件中。
查看子模块
git submodule
克隆含有子模块的项目
git clone --recursive https://github.com/laozhu/my-blog
更新子模块
git submodule update
参考材料
https://juejin.im/entry/59a0046b518825243d1f05be
3 git merge 基本操作
假设你有一个分支 hotfix 需要合并到 master,使用如下命令进行手工合并:
git checkout master git merge hotfix
参考材料
https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6
4 git brahch 常用操作
查看所有 branch:
git branch --list
切换 branch:
git checkout branch_name
5 git 更改 remote url
更改 git 的远程仓库地址可以用如下操作。
查看 git remote url:
git remote -v
修改 git remote url:
git remote set-url origin git@github.com:skylook/mobilestereonet.git
再次查看应该已经更改过来了。