Git 的一些使用小技巧

Git Logo

Git 是一款免费、开源的分布式版本控制系统。这篇文章用来整理我在使用
Git 的过程中知道的一些小技巧。

1. windows 下 git pull / git push 免密码

虽然网上有很多解决方案,但大多是将用户名和密码包含在远程地址中,然后使用 git config --global credential.helper store 将 credential 信息以明文方式存储在 .gitconfig 文件同目录下的 .git-credentials 中,会造成一定的安全隐患,_netrc 也是一样的。
git-credential-winstore 将 Git 登录用户名和密码保存在 Windows 自带的凭据管理系统中,比 credential.helper 方式更加安全。

  • git-credential-winstore.exe 下载地址
  • 安装方法:如果 Git 路径已包含在 PATH 环境变量中,直接双击运行即可。否则需要在命令行下用 -i 参数指定 Git 路径,例如:
git-credential-winstore -i C:\path\to\git.exe
  • 安装完成之后首次 git push 时,会弹出 Windows 凭据管理系统的登录窗口(如下图所示),填写用户名和密码。以后访问该版本库就不用再输入密码。

注: (1)git-credential-winstore 项目2015年10月起已不再维护,在 windows7 系统上运行正常,其他系统没有测试过; (2)新版本的 Git 已经搭载了更好的 credential manager tool: https://github.com/Microsoft/Git-Credential-Manager-for-Windows,

2. 使用 Git Clone 部署项目至 Linux 服务器,项目文件呈现 Modified 状态

这种情况的出现是因为项目文件在 linux 系统中的权限发生了变化,使用 git config 命令忽略这种变化:

git config core.fileMode false

# 当然如果你想对 全局/系统 级别的 config 进行修改,还可以增加参数
git config --add --global/system core.filemode false
3. .gitignore文件不生效

原因是 .gitignore 只能忽略那些原来没有被 track 的文件,如果某些文件已经被纳入了版本管理中,则修改 .gitignore 是无效的。那么解决方法就是先把本地缓存删除(改变成未 track 状态),然后再提交:

git rm -r --cached .
git add .
git commit -m 'update .gitignore'
4. repository 中包含带有 .git 文件夹的目录

For example:

warning: adding embedded git repository: vendor/mrgoon/aliyun-sms
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint:   git submodule add <url> vendor/mrgoon/aliyun-sms
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint:   git rm --cached vendor/mrgoon/aliyun-sms
hint:
hint: See "git help submodule" for more information.

这样会导致代码托管服务器上没有 vendor/mrgoon/aliyun-sms 目录。解决方法正如警告中所提示的:

git rm --cached vendor/mrgoon/aliyun-sms
参考文章:

Git忽略规则及.gitignore规则不生效的解决办法
使用git-credential-winstore保存https访问密码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,088评论 19 139
  • 声明:这篇文章来源于廖雪峰老师的官方网站,我仅仅是作为学习之用 Git简介 Git是什么? Git是目前世界上最先...
    横渡阅读 9,392评论 3 27
  • 文/恰恰天蓝 || 邀你一起,生命不息,日更不止 前段时间,侄女在县城里购置的新房装修进火,我因工作关系没能出席,...
    恰恰天蓝阅读 1,639评论 3 3
  • 52周回顾 1.参加年目标课程 完成 2.做完目前实验,开始整理数据 完成 3.运动 完成 4.写日记 未完成 5...
    幸福的小牙阅读 2,532评论 0 1
  • 淡水这个名字时常都会在歌中听见,空闲的礼拜天于是计划去淡水,一早起床就一直在哼唱着卢广仲的歌“就快要愛上了淡水~除...
    霹雳酷锤锤阅读 11,020评论 27 99

友情链接更多精彩内容