

sudo apt install git
验证安装:
git --version
--global即在项目下使用特定的用户git config --global user.name "your name"
git config --global user.email "your email"
查看 git 的配置信息
git config --list
git help <verb>
创建仓库有两种方法:
mkdir proj && cd proj
git init
git clone url
git add <path>
// 比如跟踪目录下所有文件
git add .
// 跟踪 .cpp 文件
git add *.cpp
git add 将文件添加到暂存区,git rm 将文件从暂存区删除(不再被 git 管理)
// 从暂存区删除且将文件删除
git rm -f <file>
// 仅将文件从暂存区删除
git rm --cached <file>
git commit -a -m "修改了......"
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
type:
scope:
subject:
body:
footer:
// ssh-keygen [-t type] [-b bits] [-C comment] [-f output_keyfile] [-N new_passphrase] [-P passphrase]
ssh genkey -t rsa -b 2048 -C "youremail"
然后将密钥填到 github 上。
查看分支:
git branch
// 修改 master 为 main
git branch -M main
// git remote add <remote_name> <remote_repository_url>
git remote add origin url
// 查看关联了哪些远程仓库
git remote -v
推送
第一次推送时加上 -u [set-upstream]建立追踪关系,后续不加分支名会自动推送到这个分支。本地分支可以追踪多个分支。
// git push <remote_name> <local_branch>:<remote_branch>
git push -u origin main:main
冲突
git pull,然后手动解决冲突。git config pull.rebase false # 合并(默认策略)
git config pull.rebase true # 变基
git config pull.ff only
git log
git log --oneline
// 显示几行
git log --[length]
// 跳过skip条,显示length条
git log --skip=[skip] -[length]
git log -p
git log --stat
git shortlog
// 过滤
// data
git log --after="2000-10-1"
git log --before="2024-5-1"
// author
git log --author="ningao"
// 覆盖上一次修改id
git commit --amend
// 修改任意提交的message
git rebase -i id
git rebase -i id
git log --oneline 查看历史提交记录
git diff hash1 hash2 --stat 查看两次提交之间的差异,这里因为建库时没有写.gitignore文件,所以tracking 了 build 文件夹
git diff hash1 hash2 filename查看某一个文件修改了那些地方// git-checkout - Switch branches or restore working tree files
git checkout branches/stable-1.1
git checkout a5673b8
git checkout a.cpp
// git-reset - Reset current HEAD to the specified state
// --soft
// --mixed
// --hard