Git

Git

1、安装

1.1、关于

1.2、Windows

1.3、Linux

# 安装 epel-release 依赖
yum install -y epel-release
# 安装 DNF
yum install -y dnf
# 安装 git
dnf install -y git-all

1.4、Mac

2、配置

2.1、基本

# 查看git系统、用户配置
git config -l

# 查看git系统配置
# Windows物理文件位置:Git安装目录\etc\gitconfig
git config --system --list
# 查看git用户配置
# Windows物理文件位置:C:\Users\用户登录名\.gitconfig
git config --global --list

# 添加配置:用户名、邮箱
git config --global user.name "MovingBricks_Lee"
git config --global user.email "123456@163.com"

2.2、免密

# 生成密钥,回车
ssh-keygen -t rsa
# 复制公钥内容(非root:cat /home/登录名/.ssh/id_rsa.pub)
cat /root/.ssh/id_rsa.pub
# gitlab
个人中心 -> Preferences -> SSH密钥 -> 粘贴

# 若还需要输入密码,解决方法:
# 长期存储密码
git config --global credential.helper store
# 设置记住密码(默认:15min)
git config --global credential.helper cache
# 自定义时间记住密码(单位:s)
git config credential.helper 'cache –timeout=3600'

3、操作

3.1、操作分支

# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出所有本地、远程分支(显示结果:带*且是绿色的为当前本地分支,红色的为远程分支)
git branch -a

# 切换到分支
git checkout 分支名

# 合并指定分支到当前分支
git merge 指定分支名

# 新建一个本地分支
git branch 分支名
# 新建一个本地分支,并切换到该分支
git checkout -b 分支名
# 删除本地分支
git branch -d 分支名

3.2、提交代码

# 查看修改了哪些文件
git status
# 添加所有代码到暂存区(提交部分:git add xxx.java)
git add .
# 提交代码到本地库
git commit -m "提交信息"
# 提交到远程git仓库的 master 分支
git push origin master

3.3、更新代码

# 更新本地 master 分支
git pull origin master

3.4、删除仓库

# 删除远程仓库指定文件
# 先删除本地文件,再 add、commit、push 覆盖远程仓库即可
git rm 文件名.java
git add .
git commit -m "删除文件名.java"
git push origin master

3.5、覆盖本地

# 强制覆盖本地代码,与远程仓库保持一致
# 拉取所有更新,不同步
git fetch --all
# 本地代码同步线上最新版本(会覆盖本地所有与远程仓库上同名的文件)
git reset --hard origin/master
# 再更新一次(其实也可以不用,第二步命令做过了其实)
git pull

3.6、覆盖线上

  • 恢复指定版本
  • 注意:当前版本最好进行本地备份(恢复版本后只显示恢复版本的历史提交记录
# 目标版本号可通过命令 git log 或 gitee 仓库在线查看
git reset --hard 目标版本号
# 强制覆盖线上
git push -f

4、问题

  1. The file will have its original line endings in your working directory

    # warning: LF will be replaced by CRLF in xxxxx
    # The file will have its original line endings in your working directory
    
    git config --global core.autocrlf false
    
posted @ 2021-05-14 10:37  MovingBricks_Lee  阅读(158)  评论(0)    收藏  举报