Git 基本操作、常用指令
Git 基本操作、常用指令
Git 的工作就是创建和保存你项目的快照及与之后的快照进行对比。
本章将对有关创建与提交你的项目快照的命令作介绍。
Git 常用的是以下 6 个命令:git clone、git push、git add 、git commit、git checkout、git pull,后面我们会详细介绍。

说明:
- 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)

浙公网安备 33010602011771号