【Git】git使用手册
2023/08/10 17:01:262022/03/08 22:50:04
配置
用户信息
git config --global user.name <user-name>:存在<user-name>时为修改用户名,否则为查看。git config --global user.email <user-email>:存在<user-email>时为修改用户邮箱地址,否则为查看。
远程仓库
git remote -v:查看当前远程仓库地址。git remote add <name> <remote-url>:添加远程仓库地址。git remote set-url <name> <remote-url>:修改远程仓库地址。git remote rm <name>:删除远程仓库。
行尾字符
git config --global core.autocrlf false:禁止 git 自动将 lf 转换成 crlf。vscode git 拉下来后 LF CRLF 问题
常用命令
push 推送到远程仓库
git push -f:用本地仓库覆盖线上仓库(当前分支)。
checkout 新建分支
git checkout -b <branch-name>:从当前分支迁出一个新分支。
merge 合并
git merge <branch-name>:将目标分支合并到当前分支,会将目标分支的所有commit都合并进去,并生成一个新的commit。git merge --squash <branch-name>:创建单个提交而不是进行合并,创建一个commit,包含目标分支中相对于当前分支较新的所有commit。
stash 隐藏
缓存当前工作区的修改并隐藏。
git stash:缓存当前工作区的修改,message为当前分支最后一次commit的message。git stash save "message:指定message。git stash pop stash@{$num}:命令恢复之前缓存的工作目录,将缓存堆栈中的对应 stash 删除,并将对应修改应用到当前的工作目录下,默认为第一个stash,即stash@{0},如果要应用并删除其他stash,命令:git stash pop stash@{$num},比如应用并删除第二个:git stash pop stash@{1}git stash apply stash@{$num}: 和pop作用类似,不删除。git stash drop stash@{$num}:删除指定的缓存。git stash clear:清空缓存。
worktree 工作空间
创建新的工作空间,如果需要在项目的不同分支上同时开发,可以用 worktree。
实质上是在本地把项目复制了一份。
reset 回退/重置
将代码回退到某个 commit 的状态。
实质上是将当前的 [head] 指向这个 commit,并抛弃这个 commit 之后的所有 commit。
revert 回滚/恢复
将代码恢复到某个 commit 的状态。
通过提交一个新的 commit,恢复另一个 commit 的修改,不改变历史 commit 节点。