Git

全局配置

配置全局参数的指令如下:

git config --global <global-name> <global-value>
git config -l  # 查看git的全局配置信息

常用的全局变量有user.name,user.email,core.editor

git config --global user.name litreily
git config --global user.email litreily@outlook.com
git config --global core.editor vim
git config --global http.proxy http://127.0.0.1:3128
git config --global https.proxy http://127.0.0.1:3128

某些特殊情况下,电脑如何通过ssh访问远程仓库,此时需要使用https,但是默认每次都要重新输入用户名和密码,此时可以配置credential.helperstore,这样只需要需要一次,本地就会保存相应的证书信息,下次就无需输入了。

git config --global credential.helper store

生成秘钥

为了在远程仓库托管本地代码,首先需要通过keygen生成一组秘钥,并添加至远程代码托管平台。

cd
ssh-keygen
cat .ssh/id_rsa.pub

添加秘钥至远程仓库,如githubcoding。可以打开远程仓库所在官网,登录个人账户,在设置中添加ssh-key

对于没有GUI界面的远程服务器,添加ssh秘钥时可以使用以下指令:

添加完成后,为检验与远程服务器的连接情况,可使用以下指令:

github为例,以上显示结果表明与github能够正常连接。

远程操作

git remote

git clone

常用指令

git add

git rm

说明:如果先通过addcommit对某些文件或文件夹进行了追踪,当我们在.gitignore文件中添加这些文件或文件夹时将不生效,因为忽略文件仅对未跟踪的文件或文件夹生效,为解决这个问题,可以先用git rm -r --cached指令移除这些文件或文件夹,以解开对它们的追踪,然后再修改.gitignore.

git mv

git commit

git push/pull/fetch

git log

git reflog

git status

git blame

git tag

合并

git merge

Join two or more development histories together

git rebase

Reapply commits on top of another base tip

git cherry-pick

Apply the changes introduced by some existing commits

暂存

git stash

添加子模块

git submodule add

首次添加子项目会多出一个.gitmodules文件,用于存储子项目的基本信息. 默认情况下新添加的子项目及.gitmodules都会存入暂存区, 此时通常需要commit一次.

撤销操作

git reset

git revert

补丁操作

git format-patch

将修改的代码写入补丁文件中

git am

将补丁文件应用到某仓库中

git send-email

将补丁以邮件形式发送出去

如果需要取消CC patch中signed-off-by的相关人员,可以将sendemail.suppresscc配置为all. 与send-email相关的常用配置如下:

其它指令

gitk

打开git的图形化界面

tig

Linux服务器端的利器,git的文本模式接口

Last updated

Was this helpful?