Git基本操作
- 2025-06-04 22:23:00
- 丁国栋
- 原创 123
Git 基础设置
git config --global --get user.name
git config --global --get user.email
git config --global user.name "dingguodong"
git config --global user.email "dingguodong@thedf.cc"
git config --global pull.rebase false
git config --global core.editor "vim"
git config --global diff.tool vimdiff
#不对0x80以上的字符进行quote,解决git status/commit时中文文件名乱码
git config --global core.quotepath false
# 忽略文件权限的变更
git config core.fileMode false
# ------------------------------------------------------------
git config --get user.name
git config --get user.email
git config --get core.editor
git config --global --get pull.ff
git config --global --get pull.rebase
git config --global --get merge.ff
# repo 级别
cd my/example-repo-dir
git config user.name "dingguodong"
git config user.email "dingguodong@thedf.cc"
# ------------------------------------------------------------
# 信任证书
git config --global http.sslverify false
# 查看当前的remote
git remote -v
# 添加一个名字为public的remote
git remote add public https://git.thedf.cc/my/example.git
# 从公共库下载所有更改到本地
git fetch --all
# 将本地私有库与公共库合并,注意rebase只能用于本地库,不能用于远程库
git rebase public/master
注: 这git fetch --all 和 git rebase public/master 两步也可以使用 git pull public master,缺点是会产生一次Merge的commit。
# 查看某个作者的提交
git log -p --author=dingguodong
# 清除本地存在但远程上已经删掉的分支
git remote prune origin
# 删除本地分支
git branch -d feature/chris/cicd
# 恢复工作区内的文件到版本库版本
git checkout /path/to/file
# 从远程分支检出到本地分支
git checkout -b release --track origin/release
# 在本地创建一个新分支,然后推送到远程
git checkout -b sprint/1989
git push --set-upstream origin sprint/1989
裸仓库转换为工作区仓库
如果我有一个git仓库的raw数据,这个git仓库raw数据是服务器端的git仓库(通常是一个裸仓库 bare repository),如何把它转换为工作区可用的仓库?
方法1(推荐):git clone /path/to/your/bare/repo.git new_working_dir
方法2:在一个空的仓库中添加远程 git remote add origin /path/to/your/bare/repo.git 后检出。
以下是几种设置 Git 记住账号密码的方法:
方法一:使用凭证缓存(推荐)
1. 设置缓存时间
# 设置缓存15分钟(900秒)
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=900'
# 设置缓存1小时(3600秒)
git config --global credential.helper 'cache --timeout=3600'
# 设置缓存8小时
git config --global credential.helper 'cache --timeout=28800'
2. 检查当前配置
git config --global --list | grep credential
方法二:使用系统凭证存储(更安全)
Windows(Git Credential Manager)
git config --global credential.helper manager
macOS(钥匙串访问)
git config --global credential.helper osxkeychain
Linux
# 使用 libsecret
git config --global credential.helper libsecret
# 或者使用 gnome-keyring
git config --global credential.helper gnome-keyring
方法三:永久存储凭证(不推荐用于敏感项目)
在URL中直接存储(不安全)
git remote set-url origin https://用户名:密码@github.com/用户名/仓库名.git
使用凭证存储文件
一般可(推)以(荐)使用这种方式。
git config --global credential.helper store
使用 .netrc 文件
cat >>~/.netrc<<EOF
machine github.com
login 用户名
password 密码
EOF
验证配置
- 执行一次 Git 操作(如 pull/push)
- 输入账号密码
- 后续操作将自动使用缓存的凭证
清除已保存的凭证
# 清除缓存
git credential-cache exit
# 或者删除凭证文件(store方式)
# Windows: %USERPROFILE%\.git-credentials
# Linux/Mac: ~/.git-credentials
安全建议
- 推荐使用缓存方式而不是永久存储
- 对于重要项目,考虑使用 SSH密钥认证 更安全
- 在公共计算机上不要使用永久凭证存储