Git 基本操作、常用指令

Git 基本操作、常用指令

Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比。

本章将对有关创建与提交你的项目快照的命令作介绍。

Git 常用的是以下 6 个命令:git clonegit pushgit addgit commitgit checkoutgit pull,后面我们会详细介绍。

img

说明:

  • workspace:工作区
  • staging area:暂存区/缓存区
  • local repository:版本库或本地仓库
  • remote repository:远程仓库

一个简单的操作步骤:

$ git init    
$ git add .    
$ git commit  
  • git init - 初始化仓库。
  • git add . - 添加文件到暂存区。
  • git commit - 将暂存区内容添加到仓库中。

创建仓库命令

下表列出了 git 创建仓库的命令:

命令说明
git init初始化仓库
git clone拷贝一份远程仓库,也就是下载一个项目。

提交与修改

Git 的工作就是创建和保存你的项目的快照及与之后的快照进行对比。

下表列出了有关创建与提交你的项目的快照的命令:

命令说明
git add添加文件到仓库
git status查看仓库当前的状态,显示有变更的文件。
git diff比较文件的不同,即暂存区和工作区的差异。
git commit提交暂存区到本地仓库。
git reset回退版本。
git rm删除工作区文件。
git mv移动或重命名工作区文件。

提交日志

命令说明
git log查看历史提交记录
git blame <file>以列表形式查看指定文件的历史修改记录

远程操作

命令说明
git remote远程仓库操作
git fetch从远程获取代码库
git pull下载远程代码并合并
git push上传远程代码并合并

创建仓库并上传项目实例

$ git config --global user.name "yangyanfa"

$ git config --global user.email "yangyanfa@stu.ouc.edu.cn"

$ git config user.name
yangyanfa

$ git config user.email
yangyanfa@stu.ouc.edu.cn

$ git init
Initialized empty Git repository in C:/Users/yangyanfa/IdeaProjects/mybatisdemo/.git/

yangyanfa@yangyanfa MINGW64 ~/IdeaProjects/mybatisdemo (master)
$ git add .

$ git commit -m 'mybatisCRUD'

$ git remote add origin https://gitee.com/yangyanfa/mybatis

$ git push -u origin master
To https://gitee.com/yangyanfa/mybatis
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitee.com/yangyanfa/mybatis'
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 push -f origin master //如果仓库为空的话,强制推
Enumerating objects: 36, done.
Counting objects: 100% (36/36), done.
Delta compression using up to 2 threads
Compressing objects: 100% (23/23), done.
Writing objects: 100% (36/36), 54.94 KiB | 3.23 MiB/s, done.
Total 36 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-5.0]
To https://gitee.com/yangyanfa/mybatis
 + 51368d3...0aec0e6 master -> master (forced update)

yangyanfa@yangyanfa MINGW64 ~/IdeaProjects/mybatisdemo (master)


3...0aec0e6 master -> master (forced update)

yangyanfa@yangyanfa MINGW64 ~/IdeaProjects/mybatisdemo (master)
posted @ 2021-06-29 10:39  笨鸟贤妃  阅读(69)  评论(0)    收藏  举报