Git 常用命令及配置
分享一下这些年我常用的 git [gɪt] 配置。以下内容基于当前版本 2.34.1,如出现不一致可先升级自己的版本!
版权声明:本文为博主 xwi88 的原创文章,遵循 CC BY-NC 4.0 版权协议,禁止商用,转载请注明出处,欢迎关注 https://github.com/xwi88
账户基本配置
--global表示全局配置,非全局配置需要在具体 git 项目目录下面设置
git config --global user.name [your_name]git config --global user.email [your_email]git config --global --list全局 git 配置查看git config --list项目 git 配置查看
签名提交设置
configure your Git client to sign commits by default for a local repository, in Git versions 2.0.0 and above
git config --global user.signingkey [KeyID]git config --global commit.gpgsign true
可参考: Git 使用 GPG 对提交进行签名认证 进行设置
密码存储
为了避免每次都要输入密码,我们需要让机器记住密码。MacOS 会借助 keychain Access 存储你的密码及其他凭证。
git config --global credential.helper store设置本机永久保存你的密码或凭证git config --global credential.helper 'cache --timeout=300'保存密码 300s
更多内容见:
如果你开启了多因素认证,则需要使用 Personal Token 之类的代替密码输入。
- github 设置路径: Settings Developer settings->Personal access tokens->Generate new token
Personal access tokens 📢
- 只有生成的时候可见,注意保存
- 务必注意权限及过期时间设置
密码清除
密码改变或过期后请务必清除存储的密码,否则可能造成
登录失败,无权限登录等问题
git config --system --unset credential.helpergit config --global credential.helper 'cache --timeout=5'或设置一个极短的过期时间,清除缓存密码git config --global credential.helper store重新开启密码存储
日志输出美化
原始的
git log太过简陋不方便查看更具体的信息,我们需要对其进行格式化,借助git alias可以很方便的定义我们自己的 git log 命令,请务必不要覆盖掉git自带的命令。
我常用的 git log 配置如下
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset | %C(red)%cs%Creset' --abbrev-commit"
git lg -4
|
|
如果你需要查看 gpg 签名信息可以如下配置:
git config --global alias.lsg "log --color --graph --pretty=format:'%C(cyan)%G?%Creset %Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset | %C(red)%cs%Creset' --abbrev-commit"
git lsg -4
|
|
更详细签名信息可如下配置:
git config --global alias.llsg "log --color --graph --pretty=format:'%C(cyan)%G?%Creset %Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset | %C(red)%cs%Creset [%GK trust:%GT] %C(yellow)%GS%Creset' --abbrev-commit"
git llsg -4
|
|
- 务必指定输出日志条数
- 务必不要配置的过于复杂,可能会导致输出较慢
- 如果使用
gpg 签名,务必保证你不要有太多个gpg key
基本命令
关于命令更多细节,可通过
git <command> --help查看
git add [<path_spec>...]添加文件到Indexgit add [<path_spec>...] -n显示命令执行后结果,不真正执行
git status -s当前分支状态查看,显示文件在工作目录与暂存区不同状态:修改,未添加 Index,暂存但未提交git diffgit diff [<path>...]比较工作目录文件和暂存区文件差异git diff <commit>...<commit> [<path>...]比较 commits 之间差异git diff --cached [<commit>] [<path>...]比较暂存区和版本库差异
git commit暂存区变动提交到本地仓库git commit -m <message>提交到本地仓库git commit -S -m <message>签名并提交到本地仓库,需要配置你的签名 GPG keygit commit -a -m添加并提交全部变动到本地仓库,跳过了git add,一般请勿执行此操作git commit --amend重新提交最近的提交,用于最近提交的提交日志信息修改或变动追加,请勿连续此操作,已推送远程仓库提交请避免此操作
git reset变更撤销,具体见 checkout 与 resetgit rm从暂存区和工作目录删除文件git mv <old> <new>文件的重定向,相当于:mv old new,git add新文件 和git rm旧文件
分支操作
远程分支地址设置
git remote -vgit remote add <name> <url>远程仓库地址配置git remote rename <old> <new>远程仓库地址名修改git remote set-url <name> <new_url>远程仓库地址修改
代码下载 clone
git clone -b branch_name --single-branch <url>单分支下载git clone <url> [dir]
代码拉取与同步
git fetch --all||git merge仅拉取代码到本地远程分支 如origin/main,upstream/main等,不直接合并到相应本地分支,在需要时同步合并git pull拉取并合并远程分支代码到当前分支,一般情况不建议直接此操作,除非你很明确可能产生的后果git push -u <repo_name> <branch_name>主要用于本地新建分支与指定远程分支间的跟踪,一般用于新分支首次推送
checkout 与 reset
git chekoutgit resetgit revertcli 不常用
| Git Command | Scope | Common use cases |
|---|---|---|
| reset | Commit | Discard commits in a private branch or throw away uncommited changes |
| reset | File | Unstage a file |
| checkout | Commit | Switch between branches or inspect old snapshots |
| checkout | File | Discard changes in the working directory |
| revert | Commit | Undo commits in a public branch |
| revert | File | (N/A) |
--softThe staged snapshot and working directory are not altered in any way.
--mixedThe staged snapshot is updated to match the specified commit, but the working directory is not affected. This is the default option.
--hardThe staged snapshot and the working directory are both updated to match the specified commit.
代码合并提交
多人或大规模团队协作情况下,请务必掌握并使用
git rebase进行操作,且要保证各自分支相比 base 分支有最小的 commits 数量,便于各自 rebase 及冲突解决。如果你提交了
PR或MR,reviewers 审查前,请务必保证你提交的分支相较 base 是Fast-Forward的,禁止在界面直接进行 Rebase
git rebase慎重操作 演示,请点击git merge不建议此操作
分支推送与删除
git push当前分支推送,需要与远程分支建立追踪;首次推送,请使用git push -u <name> <branch>git push -f强制推送当前分支到对应远程仓库git push -f <name> :<remote_branch>推送空分支到远程仓库,即删除远程仓库对应分支git branch -d本地分支删除
撤销设置项
Remove the line matching the key from config file.
git config --global --unset [key]全局配置中删除git config --unset [key]项目配置中删除
全局配种中删除误操作创建项
git config --global --unset alias.llg
配置文件编辑
git config --global -e全局配置文件编辑,文件位置:~/.gitconfiggit config -e项目配置文件编辑,文件位置:.git/config