git-相关操作

在一个仓库中引用另一个仓库

参考资料:http://www.jianshu.com/p/491609b1c426


初始化配置邮件及用户

1
2
$ git config --global user.email "364105996@qq.com"
$ git config --global user.name "wilker"

检测仓库是否可以 ssh 连接

1
2
3
4
5
6
7
8
# 测试连接 成功 地址: 192.168.1.159:22
$ ssh -T -p 22 git@192.168.1.159
Hi there, You've successfully authenticated, but Gogs does not provide shell access. # 连接 成功
If this is unexpected, please log in with password and setup Gogs under another user.

# 测试连接 失败 地址: 192.168.1.177:20022
$ ssh -T -p 20022 git@192.168.1.177
ssh: connect to host 192.168.1.177 port 20022: Connection refused # 连接 失败

设置代理

参考总结: other-TortoiseGit设置ssr代理.md


设置提交模板

一般不同项目的模板不同, 所以使用 局部模板

  1. 项目内新建一个模板文件, 如 %PROJECT%\rummy_template.txt, 内容如下

    1
    2
    3
    4
    5
    6
    是否修改引擎: 否

    问题:

    描述:

  2. cd 到 项目根目录 下设置

    1
    2
    3
    > cd I:\workspace\ShadowsocksR
    I:\workspace\ShadowsocksR (master -> origin)
    > git config commit.template ./rummy_template.txt # 设置模板

    其实就是修改 %PROJECT%\.git\config 文件, 增加模板字段

    1
    2
    [commit]
    template = "./rummy_template.txt"
    • TortoiseGit 设置

  3. 测试提交

  4. 取消模板. 把模板文件置空设置即可.


ssh config 配置

SSH 程序可以从以下途径获取配置参数:

用户配置文件 (~/.ssh/config)
系统配置文件 (/etc/ssh/ssh_config)

配置文件可分为多个配置区段,每个配置区段使用”Host”来区分。我们可以在命令行中输入不同的Host来加载不同的配置段。


ssh 生成公钥密钥

  • 命令

    1
    $ ssh-keygen -t rsa -C "aaa@bbb.com"
    • 一直回车即可

ssh 指定私钥登录

  • a

    1
    ssh -i /root/.ssh/ido_sch_pro root@192.168.1.111 -p 22

ssh 校验

  • 使用命令: ssh -T git@ip, git 是 git 服务的默认用户, 直接写 git 就行

    1
    2
    3
    4
    5
    6
    7
    8
    // 测试 ok
    $ ssh -T -p 22 git@github.aaa.com
    Hi there, yangxuan! You've successfully authenticated with the key named aaa@qq.com, but Gitea does not provide shell access.
    If this is unexpected, please log in with password and setup Gitea under another user.

    // 测试 失败
    $ ssh -T -p 22 aaa@github.aaa.com
    aaa@github.rmgstation.com: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

文件大小写敏感

1
$ git config --global core.ignorecase false

修改已有仓库 文件夹 及 文件 大小写问题

参考: https://www.jianshu.com/p/420d38913578

  1. git执行下列命令:
1
$ git config core.ignorecase false

解释:设置本地git环境识别大小写

  1. 修改文件夹名称,全部改为小写(F2重命名修改即可),然后push到远程仓库。
    这时如我前面的图片所示,仓库上就会有重名的文件(文件夹)了。
  2. 删除多余的文件,我这里就是把Footer,Header,Menu等给删掉。
    a). 执行命令,删除远程文件(删除文件夹里面的文件,文件夹也会消失)
1
2
3
# 删除Header文件夹下的所有文件
$ git rm --cached src/components/Header -r
$ git rm --cached src/components/Footer -r

如果显示如下,说明操作成功:

1
2
3
rm 'src/components/Menu/Header.js'
rm 'src/components/Menu/Header.less'
...

b). 同步,提交到远程仓库

1
2
3
$ git add .
$ git commit -m'rm files'
$ git push origin master

这时,如果上面的步骤都顺利,那在远程仓库刷新一下,惊喜就出来了:


git 日志 命令

1
2
3
4
5
6
7
8
9
> git log --since ==2019-08-13 --until=2019-08-15 --pretty=format:"myflag:%h @ %an @ %ai @ %s" --author=yangxuan --no-merges --name-status
myflag:6415697 @ yangxuan @ 2019-08-15 20:54:48 +0800 @ 描述: 商城内测支持
M Assets/Code/lua/logic/patch/patch.lua
A Assets/Code/lua/logic/pay/pay_test.lua.meta
M Assets/Code/lua/logic/platform/platform_android.lua
M Assets/Code/lua/logic/ui/pnl_shop/chips_list_item/chips_list_item_logic.lua

myflag:c783dc0 @ yangxuan @ 2019-08-15 19:41:02 +0800 @ 描述: 打包工具 父目录
M Assets/Code/Editor/Package/PackAppPatch.cs

可以用于写工具判读某个时间内 某人 是否有提交过某些文件.

比如: 使用 python 执行这个命令后把输出导出到一个文件中, 然后读取文件解析后封装到数据结构中, 再进行逻辑分析.

–date-order 日期降序排序

–reverse 翻转排序


设置不同平台下的换行符

以 windows 为准,不然 不同平台下的使用 windows 上的仓库会出现 所有文件都有差异

1
$ git config --global core.autocrlf true

正确的姿势:

在当前仓库,邮件 TortoiseGit -> Submodule Add

path 中的目的 文件夹必须是不存在的,也就是 hexo-theme-nex 文件夹是不存在的,不然汇报 :xxx alread exists 的错

这里写图片描述


初始化带有 submodule 的工程

  1. 先 clone 出工程

    1
    root@df0ca32e848a$~# git clone git@git.oschina.net:yangxuan0261/my_hexo_blog.git
  2. cd 进工程后再初始化 submodule

    1
    2
    root@df0ca32e848a:~# cd my_hexo_blog 
    root@df0ca32e848a:~/my_hexo_blog$ git submodule update --init --recursive
  3. 在分别进入所有的子模块 checkout 到 master 分支

    1
    2
    root@df0ca32e848a:~# cd themes/xxxx
    root@df0ca32e848a:~/my_hexo_blog/themes/xxxx$ git checkout master
  4. 然后就在工作中update所有的子模块

    1
    2
    root@df0ca32e848a:~/my_hexo_blog/themes/xxxx# cd ../..
    root@df0ca32e848a:~/my_hexo_blog$ git submodule update --remote
  • 正确姿势,递归 clone 所有 submodule

    1
    $ git clone --recursive -b master git@github.com:yangxuan0261/Testmmo-server.git a_mmo_skynet

取消修改,还原到上一次提交

  • 命令

    1
    $ git checkout -- <file>

工作流


Github 更新 fork 仓库

有两种方式, 一种 在 web 界面操作, 一种是 命令行 操作

web 同步

  1. 创建一个 pull request

  2. 这一步很重要, 选择同步方向, 默认是 fork 仓库 -> 源 仓库, 也就是是向别人提交一个合并请求, 并不是我们想要的.

    所以要点击 switching the base, 将同步方向修改为 源 仓库 -> fork 仓库

  3. 合并请求 merge request

  4. done. 可以看到提交总记录 比 源库 多了一个 (也就是 merge).

    • 提交记录比较

命令行 同步

官方文档 - https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/syncing-a-fork

源 仓库 同步到 在本地仓库, 然后再 推到 fork 下来的仓库.

  1. 创建上游仓库地址

    1
    $ git remote add upstream https://github.com/MisterBooo/LeetCodeAnimation
  2. fetch 更新下来

    1
    2
    3
    $ git fetch upstream
    > remote: Counting objects: 75, done.
    > remote: Compressing objects: 100% (53/53), done.
  3. 切到同步的分支

    1
    2
    $ git checkout master
    > Switched to branch 'master'
  4. 并上游提交

    1
    2
    3
    4
    5
    $ git merge upstream/master
    > Updating a422352..5fdff0f
    > Fast-forward
    > README | 9 -------
    > README.md | 7 ++++++
  5. 推送到 fork 仓库

    1
    $ git push origin master

clone 具体分支

  • 加个 -b 参数

    1
    $ git clone -b OpenSSL_1_0_2-stable git@github.com:openssl/openssl.git openssl_1.0.2

解决git无法clone提示443问题

大概就可以猜测是被墙了…翻墙的话开全局代理也没有起作用

报错: Failed to connect to chromium.googlesource.com port 443: Timed out

  1. 需要配置下git代理(前提是你有shadowsocks), 1080 是ss的客户端端口
1
$ git config --global http.proxy "localhost:1080"
  1. 再 clone 就 ok
1
2
3
4
5
6
7
8
9
git.exe clone --progress -v "https://chromium.googlesource.com/external/googletest" "D:\a_tmp_test_dir\googletest"

Cloning into 'D:\a_tmp_test_dir\googletest'...
POST git-upload-pack (151 bytes)
remote: Total 4343 (delta 3260), reused 4343 (delta 3260)
Receiving objects: 100% (4343/4343), 1.41 MiB | 49.00 KiB/s, done.
Resolving deltas: 100% (3260/3260), done.

Success (33681 ms @ 2017/7/24 10:31:42)

参考资料


git warning: refname ‘HEAD’ is ambiguous

https://segmentfault.com/q/1010000006978681

删除 HEAD 分支即可

1
git branch -D HEAD

命令行操作

url_name : 远端地址别名

url : 远端地址

remote_branch : 远端分支名

local_branch : 本地分支名

local_repo_name : 本地仓库名


全局配置

1
2
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com

初始化仓库及推送流程

1
2
3
4
5
6
$ git init
$ git checkout -b [local_branch]
$ git add -A
$ git commit -am "init workplace"
$ git remote add [url_name] [url]
$ git push [url_name] [local_branch]:[remote_branch]

暂存修改到缓冲区

使用场景: 在分支 a 上修改着东西, 突然要切到分支 b 上操作, 此时有不想提交分支a 修改的东西, 就可以把修改的东西暂时存放到缓冲区, 然后再切到分支 b 上去修改东西.

  1. 在分支 a 上暂存数据

    1
    2
    $ git stash
    Saved working directory and index state WIP on a: fd52475 - fixed chat bug.

    查看缓冲区暂存数据

    1
    2
    3
    $ git stash show
    config.gradle | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)
  2. 切到分支 b 改改改, 然后在切回分支 a 取出暂存数据

    1
    $ git stash pop

拉取更新推送

克隆仓库及子模块仓库
1
$ git clone -b [remote_branch] --recursive [url] [local_repo_name]
拉取仓库
1
2
$ git pull [url_name] [remote_branch] --progress -v --no-rebase #远端remote_branch与本地当前分支合并
$ git pull [url_name] [remote_branch]:[local_branch] --progress -v --no-rebase #远端remote_branch与本地local_branch合并
推送仓库
1
2
$ git push [url_name] [remote_branch] #推送本地当前分支到远端remote_branch
$ git push [url_name] [local_branch]:[remote_branch] #推送本地local_branch到远端remote_branch
  • 初始推送可以带上 -u 参数指定upstream
查看修改状态
1
2
3
4
5
6
7
8
9
10
11
12
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md # 修改
deleted: nohup.out # 删除
Untracked files:
(use "git add <file>..." to include in what will be committed)
aaa.txt # 增加
no changes added to commit (use "git add" and/or "git commit -a")
revert代码
1
$ git checkout . # 有点像切分支
单独提交某个文件
1
$ git commit -m 'aaa' refresh_blog_gen.sh

地址操作

添加远端地址
1
$ git remote add [url_name] [url]
查看远端地址
1
2
3
$ git remote -v
origin git@gitlab.rd.175game.com:yx0588/q6_mywiki.git (fetch)
origin git@gitlab.rd.175game.com:yx0588/q6_mywiki.git (push)
删除远端地址
1
$ git remote rm [url_name]
修改远端地址
  1. 直接修改

    1
    $ git remote set-url origin [url]
  2. 先删后加

    1
    2
    $ git remote rm origin
    $ git remote add origin [url]

分支操作

创建本地分支
1
$ git checkout -b [new_branch] # 创建并切换到 new_branch
创建远端分支
1
$ git push [url_name] [local_branch]:[remote_branch] #与推送一样,远端如果没有remote_branch分支会自动创建
切换分支
1
$ git checkout [local_branch]
更新本地的远端分支列表
1
$ git remote update origin --prune
查看本地分支
  1. 查看 所有的本地分支, * 在前面的表示是 当前所在分支

    1
    2
    3
    git branch
    aaa
    * master
  2. 查看 当前所在的分支

    1
    2
    git rev-parse --abbrev-ref HEAD
    master
查看远端分支
1
git branch -r                
同时查看本地与远端分支
1
git branch -a
指定分支upstream
1
2
3
git branch [local_branch] --set-upstream-to [url_name]/[remote_branch]
// 例如
git branch master --set-upstream-to origin/master
删除本地分支
1
git branch -D [local_branch]
删除本地缓存的远端无用分支
1
2
git remote prune [url_name] # 本地缓存的 指定远端无用的分支 都清除
git fetch -p # 本地缓存的 所有远端无用的分支 都清除
删除远端分支

在Git v1.7.0 之后,可以使用这种语法删除远程分支:

1
git push origin -d [remote_branch]

gui 中删除: browse references

分支对比

对比 分支 a 比 分支 b 多了那些提交

1
git log b..a
拉取远端所有信息
1
git fetch origin -v --prune

tag 操作

tag 可作为 时间线 中的一个 标记, 方便看出那个时间点 前后发生了什么事情.

比如发了一个版本, 打个 tag, 看日志的时候就一目了然.

创建 tag

直接可视化记录

  1. 本地创建一个新 tag : rls-ccc

  2. 查看日志, 在 tag 上右键 -> push “rls-ccc”. 远端就有这个 tag 了. 相当于这个命令

    1
    git push --progress "origin" refs/tags/rls-ccc
删除本地 tag
1
git tag -d [tag_name]
删除远端 tag
1
git push origin -d tag [tag_name]
查看所有 tag

列出 tag 及 一行信息, tag 信息优先

1
2
3
$ git tag -ln
rls-app-v1-0.9.0.1 描述: 打包工具
rls-patch-v1-0.9.1.1 platId: -1
查找符合模式的 tag
1
2
$ git tag -l rls-ap*
rls-app-v1-0.9.0.1
查看某个 tag 详情
1
2
3
4
5
6
7
8
9
10
11
$ git show rls-patch-v1-0.9.1.1
tag rls-patch-v1-0.9.1.1
Tagger: www <321321@qq.com>
Date: Fri Mar 27 11:00:00 2020 +0800

platId: -1 # tag message
所有渠道

commit e232d5fdd4a6710ced07887140dfed27f7153e31 (tag: rls-patch-v1-0.9.1.1)
Author: aaa <123123@qq.com>
Date: Tue Mar 24 21:09:35 2020 +0800

模块操作

递归 初始化 所有子模块
1
git submodule update --init --recursive
更新指定子模块
1
git submodule update --init -- "Assets/Code/lua"
递归 更新 所有子模块
1
git submodule update --recursive
查看子模块,仓库根目录查看记录文件
1
2
3
4
5
~/test_repo/z_mywiki_hello # cat .gitmodules           
[submodule "a_blog"]
path = a_blog
url = git@xxx.com:www/ccc.git
branch = master
添加子模块
1
$ git submodule add [url] [dir_path]
删除子模块
  • 操作顺序

    1
    2
    3
    4
    5
    $ git submodule deinit -f [submodulename]
    $ git rm -f [submodulename]
    $ git rm -f --cached [submodulename]
    $ rm -rf ./git/modules/[submodulename]
    $ rm -rf [submodulename]
    • 确认删除 ./git/modules/[submodulename], 如果命令行删除不了, 就手动删除

    • 老仓库需要先更新主仓库再同步一下子模块

      1
      $ git submodule sync
    • submodulename 就是 .gitmodules 文件中的这个 3rd/aaa

      1
      [submodule "3rd/aaa"]

回滚

查看日志
1
2
3
4
5
6
7
8
9
10
11
12
$ git log -2 # 查看最后两次提交
commit 0e8a7eb6def60e4988dad024d4a75e9ba0434dab (HEAD -> master, origin/master, origin/HEAD)
Author: yangxuan <364105996@qq.com>
Date: Mon Sep 30 00:09:20 2019 +0800

a

commit 5fae8260e5ec54e213fd43a46f46587f847e6291
Author: yangxuan <364105996@qq.com>
Date: Mon Sep 30 00:08:43 2019 +0800

'Robot-DESKTOP-B3461GK'
  • commit 字段的值就是版本的 sha1 值

  • 也可以用 git reflog 命令

    1
    2
    3
    4
    5
    6
    7
    $ git reflog
    0373047 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: commit: Robot-wilker
    0e8a7eb HEAD@{1}: pull --progress -v origin master:master: fast-forward
    f2d943c HEAD@{2}: pull --progress -v origin master:master: fast-forward
    a0c8186 HEAD@{3}: commit: Robot-wilker
    72bf912 HEAD@{4}: commit: Robot-wilker
    f62778e HEAD@{5}: commit: Robot-wilker
回滚本地

命令行

1
2
$ git reset --hard 0e8a7eb6def60e4988dad024d4a75e9ba0434dab // 需要回滚到的目的版本
HEAD is now at 0e8a7eb a

或者 gui 操作

右键某次提交 -> Reset -> 选择 Hard -> OK

回滚远端
  1. 先回滚本地到某一次提交

    1
    2
    $ git reset --hard 0e8a7eb6def60e4988dad024d4a75e9ba0434dab // 需要回滚到的目的版本
    HEAD is now at 0e8a7eb a
  2. 强制堆到远端

    1
    2
    3
    4
    5
    $ git push origin master -f // 强推到远端
    Total 0 (delta 0), reused 0 (delta 0)
    remote: Powered By Gitee.com
    To gitee.com:yangxuan0261/z_mywiki.git
    + 0373047...0e8a7eb master -> master (forced update)
回退提交

只是回退提交, 保留现有的改变, 这种操作之后一般会有很多 dfif 文件

  1. reset 到目的版本
  2. revert 所有, 删除所有未追踪的文件

标签操作

首先说一下作用:Git 中的tag指向一次commit的id,通常用来给开发分支做一个标记,如标记一个版本号。

添加标签
1
$ git tag -a [tagname] -m "note" 

注解:git tag 是打标签的命令,-a 是添加标签,其后要跟新标签号,-m 及后面的字符串是对该标签的注释。

提交标签到远程仓库
1
$ git push origin --tags

注解:就像git push origin master 把本地修改提交到远程仓库一样,-tags可以把本地的打的标签全部提交到远程仓库

删除标签
1
$ git tag -d version

注解:-d 表示删除,后面跟要删除的tag名字

删除远程标签
1
$ git push origin :refs/tags/version

注解:就像git push origin :branch_1 可以删除远程仓库的分支branch_1一样, 冒号前为空表示删除远程仓库的tag

查看标签
1
$ git tag或者git tag -l

删除未监控文件

删除 untracked files

1
$ git clean -f

连 untracked 的目录也一起删掉

1
$ git clean -fd

连 gitignore 的untrack 文件/目录也一起删掉 (慎用,一般这个是用来删掉编译出来的 .o之类的文件用的)

1
$ git clean -xfd

在用上述 git clean 前,强烈建议加上 -n 参数来先看看会删掉哪些文件,防止重要文件被误删


工具

–pretty=format


查看所有 tag 及 sha1

有好几种中姿势, 参考: https://stackoverflow.com/questions/8796522/git-tag-list-display-commit-sha1-hashes

  • 姿势一: 最好姿势, 也最详细, 有 sha1,tag, 等

    1
    2
    3
    $ git log --decorate --tags --no-walk --pretty=format:"myflag:%H @ %S @ %an @ %ai @ %s"
    myflag:e232d5fdd4a6710ced07887140dfed27f7153e31 @ rls-patch-v1-0.9.1.1 @ aaa @ 2020-03-24 21:09:35 +0800 @ sfesdfsdf
    myflag:c5505467f5910363806fbdefcae4a608efab0734 @ rls-app-v1-0.9.0.1 @ bbb @ 2020-03-06 16:23:51 +0800 @ werwerwer

    简约版

    1
    2
    3
    $ git log --oneline --decorate --tags --no-walk
    e232d5fdd (tag: rls-patch-v1-0.9.1.1) sfesdfsdf
    c5505467f (tag: rls-app-v1-0.9.0.1) werwerwer
  • 姿势二:

    1
    2
    3
    $ git show-ref --tags
    c5505467f5910363806fbdefcae4a608efab0734 refs/tags/rls-app-v1-0.9.0.1
    a33cdc78a9675198cf9378713c229aecd3168ee4 refs/tags/rls-patch-v1-0.9.1.1

同步一个fork仓库

参考: Syncing a fork - https://help.github.com/en/articles/syncing-a-fork

自己的解决方案, 用自建的 gogs/gitea/gitlab 服务, 直接镜像 原仓库, 定时同步即可.


TortoiseGit 使用

查看提交日志 diff

  1. 选中某次提交, 可以看到 SHA-1 值: bf6504bc8c6ddd34b67efe5d22d5eb745a166d33
  2. 双击某个文件, 弹出 diff 窗口, 右边是 选中提交 的文本值, 左边是 选中提交 的上一次提交 的文本值

查看当前文件 diff

  1. 选中文件, 右键 -> git diff, 弹出 diff 窗口, 右边是 当前工作区的文本值, 左边是上一次提交的文本值.

设置大小写敏感

1
2
$ git config --global core.ignorecase false // 全局
$ git config core.ignorecase false // 项目级, 项目级 > 全局

换行符 LF (unix) 和 CRLF (windows) 问题

git 中有三个参数于换行符有关:

eol: 设置工作目录中文件的换行符,有三个值 lf, crlf 和 native(默认,同操作系统)

autocrlf:

  • true 表示检出是转换CRLF, 提交时转换为 LF
  • input 表示检出是不转换,提交时转换为 LF
  • false 表示不做转换

safecrlf:

  • true 表示不允许提交时包含不同换行符
  • warn 则只在有不同换行符时警告
  • false 则允许提价时有不同换行符存在

设置 lf 命令

  • 配置方法

    1
    2
    3
    4
    5
    6
    7
    <!--统一换行符为 lf-->
    git config --global core.eol lf
    <!--将自动转换关闭,避免转换失败不能不同进行提交-->
    git config --global core.autocrlf false
    <!--禁止混用 lf 和 crlf 两种换行符-->
    git config --global core.safecrlf true


增加配置文件 .gitattributes

虽然通过设置了 git 全局参数解决了问题,但是作为团队协作的话,并不能保证所有人都正确配好了。git 提供了.gitattributes文件解决了这个问题。在项目根目录新建.gitattributes文件,添加一下内容:

1
2
3
4
5
6
7
# Set the default behavior, in case people don't have core.autocrlf set.
* text eol=lf

# 指定不同文件 lf
*.js eol=lf
*.jsx eol=lf
*.json eol=lf

通过这种方式避免有人没有设置 core.autocrlf 参数,并且将该文件加入版本控制中。

另外根据需要 .gitattributes 文件可以在项目不同目录中创建,而一些非文本文件可以设置为二进制文件,不用考虑换行符问题。


Git的 .gitattributes 文件详解
  • .gitattributes 文件示例:

    1
    2
    3
    4
    5
    6
    *           text=auto
    *.txt text
    *.jpg -text
    *.vcproj text eol=crlf
    *.sh text eol=lf
    *.py eol=lf

    说明:

    第1行,对任何文件,设置text=auto,表示文件的行尾自动转换。如果是文本文件,则在文件入Git库时,行尾自动转换为LF。如果已经在入Git库中的文件的行尾为CRLF,则该文件在入Git库时,不再转换为LF。

    第2行,对于txt文件,标记为文本文件,并进行行尾规范化。

    第3行,对于jpg文件,标记为非文本文件,不进行任何的行尾转换。

    第4行,对于vcproj文件,标记为文本文件,在文件入Git库时进行规范化,即行尾为LF。但是在检出到工作目录时,行尾自动转换为CRLF。

    第5行,对于sh文件,标记为文本文件,在文件入Git库时进行规范化,即行尾为LF。在检出到工作目录时,行尾也不会转换为CRLF(即保持LF)。

    第6行,对于py文件,只针对工作目录中的文件,行尾为LF。


转换 CRLF 为 LF

使用 git 自带工具 dos2unix 即可

使用

  • 语法

    1
    $ dos2unix [-hkqV] [-c convmode] [-o file ...] [-n infile outfile ...]
  • 选项

    1. -k:保持输出文件的日期不变
    2. -q:安静模式,不提示任何警告信息
    3. -V:查看版本
    4. -c:转换模式,模式有:ASCII, 7bit, ISO, Mac, 默认是:ASCII
    5. -o:写入到源文件
    6. -n:写入到新文件
  • 示例

    1
    dos2unix -k -o file1 file2 file3

奇技淫巧

忽略文件夹下 除某些文件外 的所有文件

, 如: 忽略 patch 目录下所有文件, 但 a_packarg_ 开头的文件除外, 顺序一定要对, 先忽略哪些, 再取消忽略哪些

1
2
/patch/**/*
!/patch/a_packarg_*

GitHub PAT 使用

GitHub 即将抛弃 https 拉取仓库时使用 账号,密码 的方式, 而是采用 账号,token 的方式. 参考 文档 获取 token.

1
2
3
$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token

GitHub GPG 使用

参考: https://help.github.com/en/github/authenticating-to-github/generating-a-new-gpg-key#generating-a-gpg-key

  1. 下载 gpg 命令行工具. https://www.gnupg.org/download/

  2. 生成 gpg, 命令: gpg --full-generate-key, 期间会要求设置密码 passphrase, 查看秘钥是需要用到

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    $ gpg --full-generate-key
    Real name: aaa
    Email address: bbb@qq.com
    Comment: ccc
    You selected this USER-ID:
    "aaa (ccc) <bbb@qq.com>"

    gpg: C:/Users/asdasd/AppData/Roaming/gnupg/trustdb.gpg: trustdb created
    ...

    pub rsa2048 2020-03-17 [SC]
    wwwwwwwwwwwwwwwww // 公钥 signingkey, git 设置需要用到
    uid aaa (ccc) <bbb@qq.com>
    sub rsa2048 2020-03-17 [E]
  3. 查看 GPG key ID, 命令: gpg --list-secret-keys --keyid-format LONG

    1
    2
    3
    4
    5
    $ gpg --list-secret-keys --keyid-format LONG // 查看 GPG key ID
    sec rsa2048/asdasdasd 2020-03-17 [SC] // asdasdasd, 这个就是 GPG key ID, 查看/导出 gpg 公钥/秘钥 需要用到
    3A66AEDDD6B9305C256699918C5F94AB1E9C649A
    uid [ultimate] aaa (ccc) <bbb@qq.com>
    ssb rsa2048/aaaaaaaaa 2020-03-17 [E]
  4. 使用 GPG key ID 导出 公钥/秘钥

    1
    2
    $ gpg --armor --output secret-key-gmail.txt --export-secret-keys asdasdasd // 秘钥, 会要求输入 passphrase
    $ gpg --armor --output public-key-gmail.txt --export asdasdasd // 公钥

git 启用 gpg

1
2
git config --global commit.gpgsign true
git config --global user.signingkey wwwwwwwwwwwwwwwww
  • 查看 signingkey

    1
    2
    3
    4
    5
    6
    7
    $ gpg -k
    C:/Users/wolegequ/AppData/Roaming/gnupg/pubring.kbx
    ---------------------------------------------------
    pub rsa2048 2020-03-17 [SC]
    wwwwwwwwwwwwwwwww
    uid [ultimate] aaa (ccc) <bbb@qq.com>
    sub rsa2048 2020-03-17 [E]

github 创建 gist 同步仓库

用于 vscode, sublime 等编辑器的 设置同步, 生成 access token 及 gist id

  1. 生成 access token.

    1. settings -> developer settings -> personal access tokens, 也就是: https://github.com/settings/tokens

    2. 点击 generate new token, 只勾选 gist, 然后点击 generate token, 就会获取到: 1d4486a6c3efab66f37320810a487faaaaaaaaaa

  2. 生成 gist id.

    1. your gists, 也就是: https://gist.github.com/yourname
    2. 然后点击 + 创建. 创建完之后进入 gist 就可以在网页上就可以看出 gist id, 如: 811a8b2b70df4550391e23aaaaaaaaaa
  3. done. 然后就可以在 vscode, sublime 等编辑器 中使用了.


github 变成 vscode 环境浏览代码

只要在 github 加个 1s 即可.

1
2
3
https://github.com/yangxuan0261/CppLab
// 修改为
https://github1s.com/yangxuan0261/CppLab

递归列出所有 diff

1
2
3
4
$ git status -s --untracked-files=all
M mypylib/git_util.py
?? website/rmgbox.net/index.html
?? website_helper.py

命令行相关测试

  • 本地未提交, 有冲突返回 128, 不会合并代码
  • 本地已提交, 有冲突返回 1, 会合并代码, 需要 abort merge
  • 本地无提交 或者不在同一行冲突, 正常返回 0

中文文件名乱码

  • 执行命令

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
        $ git config --global gui.encoding utf-8
    $ git config --global core.quotepath false



    ---

    ### 右键卡顿

    - *icon overlays*, 禁用 status cache

    ![image-20220715152625829](https://pic05.wilker.cn/20220715152732-446.webp)



    ------

    ### 奇葩问题集合

    #### Q: 报错 :Please make sure you have the correct access rights and the repository

    > 1. 设置为 git 的 *ssh.exe*, 路径: `D:\Git\usr\bin\ssh.exe`
    >
    > ![](https://pic04.wilker.cn/20191220154654-1.png)



    #### Q: 本地一直在 `FETCH HEAD` 提交怎么破

    > 此时千万不要直接切到其他分支, 用当前分支 `FETCH HEAD` 为基础创建一个新分支 aaa, 然后推送远端, 然后本地切到 `master` 分支, 拉去远端的分支, 也可以把 aaa 分支 merge 过来.



    #### 远端文件夹大小写修改

    - https://blog.csdn.net/u013707249/article/details/79135639

    1. 设置大小写敏感 并 清除缓存

    ```json
    git config core.ignorecase false
    git rm -r --cached .
  1. 然后在 commit -> push 提交上去. 远端就是大小写敏感的 文件/文件夹

proxifier 代理下拉取 ssh 协议报错

错误: kex_exchange_identification: Connection closed by remote host

使用 443 端口的 https 协议拉取就正常

参考: https://www.v2ex.com/t/832742