陈佳浩
陈佳浩
发布于 2024-06-26 / 32 阅读
0
0

git使用

  • git使用
    • git操作命令
      • git初始化
      • git三区
      • 差异对比
      • 版本号和日志
      • 版本回退+穿梭+撤销
      • 删除文件
      • 分支
      • 版本冲突
      • 远程仓库
      • 标签
    • windows生成ssh公钥私钥

git操作命令

git初始化

#查看配置
git config 配置(例如:user.name)

#指定用户名邮箱
git config --global user.name "chenjiahao"
git config --global user.mail "placjh@163.com"

#git命令带颜色提示
#使用默认
git config --global color.ui true
git config --global color.ui false
#针对设置
git config --global color.status auto
git config --global color.diff auto
git config --global color.branch auto
git config --global color.interactive auto

#关闭自动修改格式
git config --global core.autocrlf false

#初始化代码仓库
git init

git三区

  • 工作区:修改编写文件的区域
  • 暂存区:执行 git add xxx 将工作区的文件添加到暂存区
  • 版本区:执行 git commit -m '提交信息' 将暂存区文件提交到版本区
#查看文件状态
git status

差异对比

#对比暂存区与工作区
git diff
#对比版本区与暂存区
git diff --cached
#对比版本区与工作区
git diff master

版本号和日志

#查看git日志
git log
git reflog

版本回退+穿梭+撤销

#回退一次提交
git reset --hard HEAD^
#回退到指定版本
git reset --hard 版本号
#使用版本库中文件替换暂存区文件
git reset HEAD
#使用暂存区中指定文件替换工作区文件(危)
git checkout --文件名
#使用版本库中指定文件替换暂存区和工作区文件(危)
git checkout HEAD --文件名
#从暂存区删除文件
git rm --cached 文件名

删除文件

#删除文件
git rm 文件名
#删除目录
git rm -r 目录名

分支

#创建并切换分支
git checkout -b 分支名称
#查看分支
git branch
#切换分支
git checkout 分支名称
#合并其他分支到当前分支
git merge 其他分支名称
#删除分支
git branch -d 分支名称
#对比两个分支有差异的文件内容
git diff 分支1 分支2
#对比两个分支有差异的文件列表
git diff 分支1 分支2 --stat
#显示指定文件的详细差异
git diff 分支1 分支2 文件名
#推送所有分支
git push -u origin --all

版本冲突

远程仓库

#关联远程仓库
git remote add origin url  #origin是个别名
#删除关联
git remote remove origin
#记住账号名密码
git config --global credential.helper store
#推送
git push origin 分支名
#拉取
git pull origin 分支名
#拉取并新建一个分支,将拉取的代码复制到新建分支
git fetch origin 分支名:新建分支名
#克隆仓库,本地没有仓库,要获取一个完整的仓库
git clone url
#将本地没有的分支拉取下来
git pull
#忽略文件
在工作区创建.gitignore文件
将要忽略的文件名放进去

标签

#当前状态创建标签
git tag v1.0
#将指定commit创建标签
git tag -a v1.0 b720aaf -m "v1.0"
#删除标签
git tag -d v1.0
#回滚到指定标签
git reset --hard v1.0
#查看标签
git tag
#查看标签
git show v1.0
#将本地仓库v1.0的tag推送到远程
git push origin v1.0
#将本地仓库所有的tag都推送到远程
git push origin --tags
#只推送指定的tag到远程
git push origin --tag v1.0

windows生成ssh公钥私钥

首先Windows操作系统需要安装git.
安装完成后,再到任意的文件夹内,点击右键.选择git bash here
打开之后,输入ssh-keygen,一路按enter键.
全部结束后,再到C:\Users\Administrator\.ssh 文件夹下,打开id_rsa.pub文件,复制文件内的公钥.

评论