安装
git 镜像下载,选择合适的安装包下载。
打开 cmd,输入指令检测是否安装成功:
git --version
输出版本信息即为安装成功。
基本使用
初始化仓库
在本地文件夹中创建 git 仓库:
git init
配置
查看全部配置:
git config --list
配置全局用户名以及邮箱,操作一次:
git config --global user.name <your name>
git config --global user.email <your email>
SSH 连接
生成 RSA 密钥:
ssh-keygen -t rsa [-C <注释>]
获取 RSA 公钥内容,并配置到 gitee/github 的 SSH公钥 中,操作一次:
cat ~/.ssh/id_rsa.pub
添加远程仓库
查看远程仓库:
git remote -v
添加远程仓库:
git remote add origin <远程仓库ssh链接>
删除远程仓库:
git remote rm origin
拉取远程仓库
克隆远程仓库,只需要操作一次即可:
git clone <ssh链接>
拉取远程仓库 origin 中的新分支与数据:
git fetch origin
将新分支与数据合并到本地仓库:
git merge origin/master
推送到远程仓库
将工作区的文件添加到暂存区:
git add <file>
将暂存区的文件提交到本地仓库:
git commit -m <"提交说明">
将本地仓库的分支与数据推送到远程仓库 origin 中的 master 上:
git push origin master
进阶使用
分支管理
查看本地及远程的所有分支:
git branch -a
创建分支并切换:
git checkout -b <branch name>
等价于,创建分支,然后切换到该分支:
git branch <branch name>
git checkout <branch name>
合并分支到当前 HEAD 所在分支:
git merge <branch name>
删除本地分支:
git branch -d <branch name> // 删除本地分支
git branch -D <branch name> // 强制删除未合并的分支
删除远程分支:
git push origin --delete <branch name>
checkout 新版本分化
其作用是检出,可以切换分支,可以将暂存区的文件检出到工作区,也可以检出特定提交版本。
git checkout <branch name> // 切换到指定分支
git checkout <file name> // 将文件从暂存区检出到工作区
git checkout <commit hash> // 检出指定版本
switch
专门用来切换分支:
git switch <branch name>
git switch -c <branch name> // 创建并切换分支
日志管理
查看日志:
git log
git log --oneline // 以一行的形式查看
回退与恢复版本:
git checkout <commit hash> -- <file name> // 切换指定文件到指定版本
git checkout <commit hash> // 切换到指定版本
git reset --soft <commit hash> // 保留暂存区和工作区
git reset --mixed <commit hash> // 保留工作区
git reset --hard <commit hash> // 回退到指定版本,不保留
git revert <commit hash> // 撤销提交,以一次新的提交将文件回滚到指定版本
查看历史操作记录:
git reflog
指令别名
查看全局指令别名配置:
cat ~/.gitconfig
创建指令别名,注意指令在一行上:
git config --global alias.lm
"log
--no-merges // 过滤掉 merge commit
--color // 颜色
--date=format:'%Y-%m-%d %H:%M:%S' // 日期模式
--author='<author>' // 作者
--pretty=format:'%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' // 自定义显示模式
--abbrev-commit" // 7 位短hash