git的创建和使用以及常用命令

1.git的工作流程:

图片引用:https://www.bilibili.com/video/BV1mb411n7Nw?p=3
git的创建和使用以及常用命令

2.开始创建git账号:以国内的码云为例
2.1 、到码云官网注册账号:https://gitee.com/signup

git的创建和使用以及常用命令
我就推荐使用这两个吧,方便的话就用微信,我直接绑定我的github就得。
git的创建和使用以及常用命令

3.新建仓库
3.1 :登录好之后就可以新建仓库了:

git的创建和使用以及常用命令
git的创建和使用以及常用命令
git的创建和使用以及常用命令
好的目前为止仓库就创建完成了。

备注:issues
git的创建和使用以及常用命令 issues功能被用来追踪各种想法,增强功能,任务,bug等。许多的项目通过centeral bug tracker收集用户的反馈。

4.git拉取项目操作:
4.1、 fork:从别人开放得远程仓库复制到自己的远程仓库

比如我从一个大佬的开源项目复制过来自己的仓库:
git的创建和使用以及常用命令
然后在我的仓库中就有这个项目作为一个分支:
git的创建和使用以及常用命令

4.2、 clone:从远程仓库克隆项目到自己电脑

比如我要克隆刚刚我新建的项目到我的上进项开发:
git的创建和使用以及常用命令
注:如果clone不是自己的项目,那么修改后没有开发者以上权限时不可以推送代码到别人远程仓库的

使用vscode克隆clone项目:
git的创建和使用以及常用命令
git的创建和使用以及常用命令
回车选择存储位置就得了。。。

4.3、 pull:从远程仓库合并代码到自己的工作区
git pull [分支],不写默认master分支

比如我仓库新增一个文件,达到效果:仓库更新但是本地还没跟新
git的创建和使用以及常用命令
然后在项目目录下使用:git pull
git的创建和使用以及常用命令

4.4、 fatch:从远程仓库合并代码到自己的本地仓库
git fetch

git的创建和使用以及常用命令

4.5、 merge:从自己本地仓库合并到代码到自己工作区
git merge

git的创建和使用以及常用命令

小总结:

fatch + merge == pull

5.git提交项目操作:
5.1、远程仓库没有源
  1. git init #初始化项目
  2. git add . # 提交项目到git本地库
  3. git commit -m “备注信息” # 提交到本地库
  4. git remote add origin https://gitee.com/miaoderen/VueDemo.git #关联远程仓库
  5. git push -u origin master #推送到github上

可能会报这个错误:

To https://gitee.com/miaoderen/VueDemo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to ‘https://gitee.com/miaoderen/VueDemo.git’
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push --help’ for details.

  • 原因是:

本地没有远程库的文件

  • 解决方法:

git pull --rebase origin master
再提交:
git push -u origin master

完成效果:
git的创建和使用以及常用命令

小总结:每次提交之前都要

  • git pull
  • git commit -m “修改的信息”
  • git push

三连操作

6.常用错误和莫名其妙:
  • fatal: refusing to merge unrelated histories
  1. git pull --tags origin master
    From https://gitee.com/miaoderen/VueDemo
    * branch master -> FETCH_HEAD
    fatal: refusing to merge unrelated histories

原因:拒绝合并无关历史

解决方法:

git pull origin master --allow-unrelated-histories

  • fatal: The current branch master has no upstream branch.

fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master

原因:没有将本地的分支与远程仓库的分支进行关联
解决方法:

git push -u origin master

7.常用命令:

打勾常用

  • git pull origin master --allow-unrelated-histories
  • git init :初始化一个git项目
  • git clone :克隆一个项目到本地
  • git pull : 合并远程仓库项目的更新
  • git pull https://gitee.com/miaoderen/VueDemo.git chaoge :从指定项目的分支拉去代码
  • git add :添加当前目录的所有文件到暂存区
  • git commit :提交暂存区到本地仓库
  • git push :提交代码到远程仓库
  • git diff :显示暂存区和工作区的差异
  • git checkout :切换到指定分支,并更新工作区
  • git fetch :下载远程仓库的所有更新
  • git merge :合并指定分支到当前分区
  • git branch -a :查看仓库所有分支
8.冲突解决:
  1. 强制push本地仓库到远程 (这种情况不会进行merge, 强制push后远程文件可能会丢失)
  • git push -u origin master -f
  1. 避开解决冲突, 将本地文件暂时提交到远程新建的分支中,创建完branch后, 再进行push。
  • git branch [name]
  • git push -u origin [name]