学习目标

要掌握的问题:

  • 什么是Git以及Git的工作原理
  • Git的常用命令Best Practise(避坑教学)
  • Git冲突是怎么引起的,如何解决
  • Git flow规范团队git使用教程

1.秘钥配置

想要本地推送到远程仓库或者是克隆仓库,需要ssh密钥

查看ssh

  1. 查看电脑是否存在id_rsa.pub
  2. cd ~/.ssh
  3. 如果不存在则会提示No such file or directory 否则会自动进入目录,用ls就可以看到
  4. ls
  5. id_rsa id_rsa.pub

创建一个SSH key

  1. ssh-keygen -t rsa -C "your_email@example.com"
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/c/Users/user/.ssh/id_rsa)://可以指定⽬
  4. 录也可以不指定⽬录。直接回⻋
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again://可以不输⼊密码,直接回⻋,以后每次push就不⽤输⼊密码
  7. Your identification has been saved in /c/Users/user/.ssh/id_rsa.
  8. Your public key has been saved in /c/Users/user/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. 这⾥是⽣成的key fingerprint
  11. The key's randomart image is:
  12. 这⾥是⽣成的key's randomart image
  13. 此时创建ssh成功

2.git常用命令

创建版本库

  1. git init
  2. git add . /git add <file name>
  3. git commit -m "****" //说明修改内容

仓库克隆

  1. git clone xxxxx.git //默认是main分支
  2. git clone -b <branch_name> xxxxxx.git //克隆特定分支

远程推送

  1. git remote add origin xxxxx.git
  2. git push -u origin <branch_name>

删除远程分支

  1. git push origin -d <branch_name>

删除本地分支

  1. git branch -D <branch_name>

查看本地分支和远程分支

  1. git branch //查看本地分支
  2. git branch -a //查看远程仓库和本地所有分支

创建分支

  1. git checkout -b <branch_name> //创建并切换到当前分支
  2. 等同于
  3. git branch <branch_name>
  4. git checkout <branch_name>

切换当前分支

  1. git checkout <branch_name>

其他命令

  1. git branch --set-upstream-to=origin/dev //设置git push.pull默认的提交获取分支
  2. git branch --unset-upstream master //取消对master的跟踪
  3. git status //显示工作区状态
  4. git init //初始化仓库
  5. git log //日志功能
  6. git merge dev //合并并指定分支到当前分支
  7. git stash //把当前的工作现场储藏起来,以备以后继续合作
  8. git stash list //查看隐藏的工作区
  9. git checkout -b branch-name origin/branch-name //在本地创建和远程分支对应的分
  10. git branch --set-upstream branch-name origin/branch-name //建立本地分支和远程分支的关联
  11. git rebase rebase //可以把本地未push的分支提交历史整理成直线
  12. git remote //可以查看关联的远程仓库
  13. git remote -v //可以查看关联的远程仓库的具体url

注意

当git无法自动合并冲突时,应先解决冲突再提交,再合并git status查看冲突的文件git log —graph可以看到分支合并图

3.使用git回滚版本

  1. //查看每次操作对于的commitid的账号
  2. git reflog
  3. //本地端口回滚指定的版本,这种回滚是不可逆的,会导致之前的提交记录都没有了
  4. git reset --hard commitId
  5. //强制推送到远程分支
  6. git push -f
  7. //查看所有提交的版本号
  8. git log --oneline
  9. //git revert命令是撤销某次操作,而在此次操作之前和之后的提交记录都会保留
  10. git revert commitid