git config

查看配置

1
git config --list

全局

1
2
3
cat ~/.gitconfig
git config --global user.name "username"
git config --global user.email "email"

github令牌

2021年8月13号之后,github已经不让直接使用账号名密码来登录了,必须使用personal access token(个人访问令牌)

  • 明文的密码很容易被泄露,如果换成有时限的token,即使泄露了影响也会非常有限。
  • 可以为同一个github账号根据不同的使用途径,生成不同的token,并且随时都可以控制token的有效状态和不同token代表的权限。最大限度的保证账号的安全性。
  • 生成的token可随时撤销,并且令牌的随机性更高,不容易被暴力破解。

我的账号->settings->Developer settings->Personal access tokens->Generate new token->(pc名字,粘贴key)->(Note:哪台电脑,有效期(Expiration)选90天,Select scopes选repo)

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

ssh访问github

1
2
3
4
#如果没有~/.ssh 目录就执行下面,一直回车到结束即可
ssh-keygen -t rsa -C "your email"
#查看ssh key并且拷贝
cat ~/.ssh/id_rsa.pub

我的账号->settings->SSH and GPG keys->New SSH key->(pc名字,粘贴key)

1
2
3
ssh -T git@github.com
#运行结果出现类似如下
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

局部(项目内的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cd projectdir/
cat .git/config
git config user.name "username"
git config user.email "email"
cat .git/config

#设置记住密码(默认15分钟):
git config credential.helper cache
#设置记住密码1小时
git config credential.helper 'cache --timeout=3600'
#长期存储密码:记住密码,以明文存在~/.git-credentials电脑送人后记得删掉该文件
git config credential.helper store
#重新指定.git-credentials文件路径
git config credential.helper 'store --file /data/git/.git-credentials'
cat .git/config

记住或取消密码记录

1
2
3
(repository_home)/.git/config # for the subject repository.
~/.gitconfig # for this particular user.
/etc/gitconfig # for all users on this system.
1
2
3
4
5
6
7
8
9
#记住密码记录
git config --set credential.helper 'cache --timeout=600'
git config --global --set credential.helper 'cache --timeout=600'
git config --system --set credential.helper 'cache --timeout=600'

#取消密码记录
git config --unset-all credential.helper
git config --global --unset-all credential.helper
git config --system --unset-all credential.helper

本地origin分支与远程不同步

1
2
git remote show origin # 发现不是tracked的分支(远程已删,本地还有)
git remote prune origin # 删除远程没有本地还在的分支

windows删除git记录

  1. 进入控制面板
  2. 选择用户账户
  3. 选择管理你的凭据
  4. 选择Windows凭据
  5. 选择git保存的用户信息
  6. 选择编辑或者进行删除操作

git查看各分支之间关系

1
git log --graph --decorate --oneline --simplify-by-decoration --all

git status中文乱码

1
git config --global core.quotepath false