Git Command

安装

基本使用

初始化仓库

配置

SSH 连接

添加远程仓库

拉取远程仓库

推送到远程仓库

进阶使用

分支管理

checkout 新版本分化

日志管理

指令别名


安装

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
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值