记录一下如何在Windows和Ubuntu系统中使用GitHub进行版本管理。
Windows
GitHub Desktop
GitHub Desktop是GitHub开发的客户端,使用图形化交互界面代替了Git的命令行操作,可以满足大部分需求,但是其内置的Git无法在编辑器中使用。
Git for Windows
安装
在官网下载Git安装包Git-2.39.1-64-bit.exe并运行:
- Information许可证信息;
- Select Destination Location选择安装位置:- D:\Program Files\Git;
- 
    Select Components选择安装组件:- 勾选Additional icons->On the Desktop;
- 勾选Add a Git Bash Profile to Windows Terminal;
- 其余选项保持默认;
 
- 勾选
- Select Star Menu Folder选择开始菜单文件夹;
- Choosing the default editor used by Git选择Git使用的默认编辑器:- Use Visual Studio Code as Git's default editor;
- Adjusting the name of the initial branch in new repositories调整新仓库初始分支名:- Let Git decide(目前默认初始分支名为- master,后续可能更改);
- Adjusting your PATH environment调整PATH环境变量:- Git from the command line and also from 3rd-party software(推荐);
- Choosing the SSH executable选择SSH可执行文件:- Use bundled OpenSSH;
- Choosing HTTPS transport backend选择HTTPS传输后端:- Use the OpenSSL library;
- Configuring the line ending conversions配置行尾序列转换:- Checkout Windows-style, commit Unix-style like line endings;
- Configuring the terminal emulator to use with Git Bash配置Git Bash使用的终端仿真器:- Use MinTTY(the default terminal of MSYS2);
- Choose the default behavior of 'git pull'选择- git pull的默认行为:- Default(fast-forward or merge);
- Choose a credential helper选择安全证书助手:- Git Credential Manager;
- Configuring extra options配置其他选项:- Enable file system caching;
- Configuring experimental options配置实验选项:保持默认,全不勾选;
配置
- 
    在VS Code中配置Git扩展的 Git: Path选项,在settings.json文件中指定Git可执行文件的路径和文件名:"git.path": "D:\\Program Files\\Git\\bin\\git.exe"
- 
    在终端仿真器中配置字符编码为UTF-8,防止中文显示乱码: git config --global core.quotepath false # 设置不转义特殊字符,文件路径不用编码模式显示 git config --global gui.encoding utf-8 # 图形界面编码 git config --global i18n.commit.encoding utf-8 # 提交信息编码 git config --global i18n.logoutputencoding utf-8 # 输出log编码 export LESSCHARSET=utf-8 # less命令编码,输出log默认使用less命令分页
使用
- 安装并配置Git后可以在VS Code中使用Git的全部功能;
- 可以在Git Bash终端仿真器中使用Git的命令行操作;
Ubuntu
使用VS Code,其UI已经集成Git命令,结合GitHub相关扩展进行管理。
- 
    设置Git用户名和邮箱: - 系统设置,所有系统用户的设置,保存在/etc/gitconfig文件中,极少使用,优先级最低;
- 
        全局设置,当前系统用户的设置,保存在 ~/.gitconfig文件中,优先级中等:git config --global user.name "your name" git config --global user.email "your@email.com" git config --global -l # 列出所有全局设置
- 
        本地设置,当前仓库的设置,保存在 .git/config文件中,优先级最高:git config --local user.name "your name" git config --local user.email "your@email.com" git config --local -l # 列出所有本地设置
- 更改全局设置前需要删除~/.ssh/id_rsa以及~/.ssh/id_rsa.pub;
- 设置的邮箱需要添加到GitHub账号中才能计算贡献;
 
- 系统设置,所有系统用户的设置,保存在
- 
    创建SSH key: ssh-keygen -t rsa -b 2048 -C "your@email.com"- 连续点三次回车确认;
- 美国国家标准与技术研究院(National Institute of Standards and Technology,NIST)建议使用至少2048位的SSH key;
 
- 
    保存SSH key: 复制 ~/.ssh/id_rsa.pub中的内容,在GitHub中Setting->SSH and GPG keys->New SSH key添加新的SSH key。
- 
    完成通信设置: ssh -T git@github.com输入 yes确认。
- 
    安装相关VS Code扩展,首次使用需要在GitHub网站上进行验证,授权VS Code登录; - GitHub Pull Requests and Issues
- GitLens——Git supercharged
 
- 
    使用SSH进行git clone和git push时不需要使用用户名和密码: git clone git@github.com:<username>/<repository>.git
- 
    使用HTTPS进行git clone和git push时仍需要使用用户名和密码,自2021年8月14日起,GitHub取消了在命令行内使用用户名和密码的clone方式,改为使用个人访问令牌(Personal Access Token,PAT),其作用和使用方式与密码相同: git clone https://github.com/<username>/<repository>.git
- 
    及时删除不再使用的SSH key以及PAT; 
GitHub
- Contribution settings->勾选- Private contributions
- Settings->- Emails->勾选- Keep my email addresses private;
- 
    PAT的生成和使用: - Settings->- Developer settings->- Personal access tokens->- Generate new token;
- 设置token名称、有效期,有效范围(勾选repo);
- 点击Generate token,将生成的token保存;
 
- 
    双因素身份校验(Two-Factor Authentication,2FA)的设置和使用,自2023年10月6日起,在登录GitHub时需要使用2FA: - Settings->- Password and authentication->- Two-factor authentication;
- 使用神锁离线版扫描二维码,保存秘钥,用于生成基于时间的一次性密码(Time-based One-Time Password,TOTP);
- 在登录GitHub时除了需要使用账号密码,还需要使用根据秘钥和当前时间生成的一次性密码(One-Time Password,OTP),OTP每30秒动态更新一次;
- 保存恢复码(recovery codes),用于在丢失密码和秘钥时登录GitHub,否则会永远丢失账号;
- 也可以使用短信验证的方式进行2FA,但是目前暂时不支持中国大陆;
 
参考
- GitHub Desktop
- desktop/desktop
- Git
- git/git
- Git for Windows
- git-for-windows/git
- Differences between Git-scm, msysGit & Git for Windows-Stack Overflow
- Windows系统Git安装教程(详解Git安装过程)-maanlong的文章-知乎
- git显示中文和解决中文乱码-YuSoLi的文章-知乎
- 解决Git在windows下中文乱码的问题-小明同学的文章-知乎
- Git客户端设置Windows下的字符编码-华为云
- 在VScode上配置Git-浪晋的文章-知乎
- git config的全局和本地配置-chloneda的文章-知乎
- git config的配置(system、global、local)
- Recommendation for Key Management-NIST
- Missing contributions-GitHub Docs
- Managing email preferences-GitHub Docs
- Token authentication requirements for Git operations-GitHub Blog
- Creating a personal access token-GitHub Docs
- GitHub防黑客新措施:弃用账密验证Git操作,改用token或SSH密钥,今天0点已执行-量子位的文章-知乎
- GitHub强制要求开启两步验证了,但是1password要收费,怎么办?-游凯超的文章-知乎
- GitHub要求2FA?不慌,有它帮你-神锁离线版的文章-知乎
- 开启GitHub的2FA-bilibili
