Git操作 - 删除untracked files

  1. 删除 untracked files

    1git clean -f
    
  2. 连 untracked 的目录也一起删掉

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

    1git clean -xfd
    
  4. 在用上述 git clean 前,墙裂建议加上 -n 参数来先看看会删掉哪些文件,防止重要文件被误删

    1git clean -nxfd
    2git clean -nf
    3git clean -nfd**
    
  5. 针对公司或者GitHub设置不同的提交账号

    进入项目目录

    1git config user.name "wmsjhappy@gmail.com"
    2git config user.email clibing
    3
    4git config --global user.name "clibing"
    5git config --global user.email "wmsjhappy@gmail.com" 
    

    会在当前目录.git/config会生成提交的账号和邮箱地址

  6. 跳过证书的验证

    1git config --global http.sslVerify false
    
  7. 修改上一次已经提交的记录

    1git commit --amend
    
  8. 覆盖本地master与服务器一致

    1git fetch --all &&  git reset --hard origin/master && git pull
    
  9. 彻底删除某个敏感信息,包括历史提交记录 参考

    git-filter-repo source

    1brew install git-filter-repo
    2
    3git filter-repo --invert-paths --path 需要删除的文件(是相对项目的路径,不要以/开头)
    4
    5git rebase -i HEAD~5 需要修改当前分支的提交点~-5个提交, pick->edit修改提交记录, 修改pick->squash会合并多个提交记录
    
  10. 当commit msg不符合规范的是否需要修复commit

参考官方文档

1git rebase -i HEAD~3 # 代表修改当前分支最近3个commit的message
2提交后按照提示执行
3  单条修改,需要将'pick' -> 'edit'
4  多条合并修改,需要将'pick' -> 'squash'
5保存退出
6
7git commit --amend 修改内容
8
9git rebase --continue
  1. 查看tag的列表,展示时间
1git log --tags --simplify-by-decoration --pretty="format:%ai %d"
  1. Git push send-pack: unexpected disconnect while reading sideband packet 107 致命错误:远端意外挂断了
1git push

会提示如下问题

 1枚举对象中: 39, 完成.
 2对象计数中: 100% (30/30), 完成.
 3使用 4 个线程进行压缩
 4压缩对象中: 100% (15/15), 完成.
 5写入对象中: 100% (17/17), 171.37 KiB | 17.14 MiB/s, 完成.
 6总共 17(差异 11),复用 0(差异 0),包复用 0
 7错误:RPC 失败。curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)
 8send-pack: unexpected disconnect while reading sideband packet
 9致命错误:远端意外挂断了
10Everything up-to-date

需要设置为HTTP1.1即可

1git config http.version HTTP/1.1
  1. 清理当前仓库未跟踪、忽略的文件

13.1 可选参数

  • -n 不要删除任何内容,只显示将要删除的内容
  • -f 强行删除文件
  • -d 考虑未跟踪的目录以及未跟踪的文件
  • -x 还要考虑被忽略的未跟踪文件.gitignore

13.2 查看将要删除文件列表

  • git clean -n :仅列出未跟踪的文件,忽略.gitignore中的文件.
  • git clean -n -d :列出未跟踪的文件和文件夹,忽略中的.gitignore文件
  • git clean -n -x :列出所有未跟踪的文件,包括被忽略的文件.gitignore。
  • git clean -n -d -x :列出所有未跟踪的文件和文件夹,包括被忽略的文件和文件夹.gitignore

13.3 从存储库中删除文件

  • git clean -f: 只删除未被.gitignore
  • git clean -f -d: 删除未跟踪的文件和文件夹,不删除.gitignore的文件
  • git clean -f -x: 删除所有未跟踪的文件,包括那些被忽略的文件.gitignore。
  • git clean -f -d -x: 删除所有未跟踪的文件和文件夹,包括那些被忽略的文件和文件夹.gitignore。
  1. 设置默认的主分支名字
1git config --global init.defaultBranch <名称>
  1. 保存Git登录账号

快速连接

1
2# cache 模式会将凭证存放在内存中一段时间。 密码永远不会被存储在磁盘中,并且在15分钟后从内存中清除。
3git config --global credential.helper cache
4
5# store 模式会将凭证用明文的形式存放在磁盘中,并且永不过期。 这意味着除非你修改了你在 Git 服务器上的密码,否则你永远不需要再次输入你的凭证信息。 这种方式的缺点是你的密码是用明文的方式存放在你的 home 目录下。
6git config --global credential.helper store
7
8git config --global credential.helper 'store --file ~/.my-credentials'

如果你使用的是 Mac,Git 还有一种 “osxkeychain” 模式,它会将凭证缓存到你系统用户的钥匙串中。 这种方式将凭证存放在磁盘中,并且永不过期,但是是被加密的,这种加密方式与存放 HTTPS 凭证以及 Safari 的自动填写是相同的。

如果你使用的是 Windows,你可以安装一个叫做 “Git Credential Manager for Windows” 的辅助工具。 这和上面说的 “osxkeychain” 十分类似,但是是使用 Windows Credential Store 来控制敏感信息。 可以在 https://github.com/Microsoft/Git-Credential-Manager-for-Windows 下载。

  1. idea 显示变动的文件
1
2Settings --> Vesion Control -> Commit -> 反选 [] Use non-modal commit interface.
  1. git proxy

a. https访问

仅为github.com设置socks5代理(推荐这种方式, 公司内网就不用设代理了, 多此一举): git config --global http.https://github.com.proxy socks5://127.0.0.1:1086 其中1086是socks5的监听端口, 这个可以配置的, 每个人不同, 在macOS上一般为1086. 设置完成后, ~/.gitconfig文件中会增加以下条目:

1[http "https://github.com"]
2    proxy = socks5://127.0.0.1:1086

ssh访问 需要修改~/.ssh/config文件, 没有的话新建一个. 同样仅为github.com设置代理:

1Host github.com
2    User git
3    ProxyCommand nc -v -x 127.0.0.1:1086 %h %p

如果是在Windows下, 则需要个性%home%.ssh\config, 其中内容类似于:

1Host github.com
2    User git
3    ProxyCommand connect -S 127.0.0.1:1086 %h %p
1# 全局设置
2
3git config --global http.proxy socks5://127.0.0.1:1080
4git config --global https.proxy socks5://127.0.0.1:1080
5
6## 取消全局设置
7git config --global --unset http.proxy
8git config --global --unset https.proxy
1# 局部设置
2
3# 设置代理 只有github.com 走代理 socks5
4git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
5git config --global https.https://github.com.proxy socks5://127.0.0.1:1080
6
7# 取消
8git config --global --unset http.https://github.com.proxy
9git config --global --unset https.https://github.com.proxy

参考