Git 基本使用方法
依据文件在 Git中 的状态,可将其内部分为三个工作区域:
假设拿 Git 来管理项目的源码,那工作文件夹就是一个Workspace。当中的源码文件可依据其是否纳入Git的管理流程分为三类:
1.1)Untracked:未纳入Git管理流程的
1.2)Tracked:已纳入Git管理流程的
对于已纳入Git管理流程的能够再分为三类:
a)已改动,未暂存的 (Changes not staged for commit)
b)已改动,已暂存的,未提交的 (Changes to be committed)
c)已提交,未改动的 (commited)
1.3)不纳入Git管理流程的(在.gitignore文件里定义)
对于未纳入Git管理流程的文件和已纳入Git管理流程中处于 a)状态的文件,能够使用add命令来将其加入到暂存区域:
git add a.file
此时,a.file就会被保存到暂存区域。
3)本地仓库
git commit -m "Commit Staged files"
指定 -m 參数,能够后面加入对提交内容的凝视信息。
假设不指定 -m 參数,则 Git 会跳转到一个vi输入界面,等待用户输入凝视信息之后,再提交。
1)先利用git status查看一下当前的工作文件夹状态:
$ git status
# On branch master
nothing to commit (working directory clean)
2)加入一个文件 test.txt,再查看一下其状态,例如以下:
$ touch test.txt
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
nothing added to commit but untracked files present (use "git add" to track)
可看到,在Untracked files:以下存在一个test.txt文件,而最以下一行则提示没有文件须要提交,可是存在未跟踪的文件(未跟踪,也即未纳入Git管理流程),此时 test.txt 文件就是处于未纳入管理流程的状态。
$ echo "Add content to end of the a.txt" >> a.txt
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# test.txt
no changes added to commit (use "git add" and/or "git commit -a")
能够看到,对于a.txt文件,其已纳入Git管理流程,所以其显示的是Changes not staged for commit,即上面所述的状态 a)。
对于未纳入Git管理流程的文件, add 命令可将其纳入Git管理流程,同一时候将其加入到暂存区域。
对于已纳入Git管理流程的文件,add 命令可将改动过的文件加入到暂存区域。
切记,仅仅有加入到暂存区域的文件,才可以提交到本地仓库。
$ git add test.txt
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: test.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a.txt
#
利用add命令,将test.txt文件加入到暂存区域(Changes to be commited),可看到test.txt已经是处于暂存区域,等待提交了,其状态就是上述状态中的 b)状态了。
$ git add a.txt
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
# new file: test.txt
#
可看到,上面两个文件都被增加到暂存区域了。
git commit
Git 会启动文本编辑器,让用户输入本次的提交说明,例如以下:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
# new file: test.txt
当用户输入提交信息之后,保存在暂存区的内容就会被保存到本地的仓库了。
$ git commit
[master d15838d] Commit the changes
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test.txt
可看到,在提交成功之后,Git会计算出一个Hash值(如上面,最前面几位是:d15838d),作为这次提交的标识值,可利用git log来查看。
$ git commit -m "Add new files"
(0)工作文件夹中,创建文件。
(1)编辑文件,利用 add 加入进暂存区域。
(2)利用 commit 将暂存区域的文件保存到本地仓库。
(3)编辑文件,利用 add 将其加入进暂存区域,回到(2)步,如此循环。
$ git commit -am "Add and then Commit"
可是对于未纳入Git管理流程的文件,则不能这么做,必须先显式使用add命令将其纳入Git管理流程,保存进暂存区域,再提交。
$ git rm b.txt
rm 'b.txt'
查看其状态:
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: b.txt
#
相同的,这次删除操作也会被保存进暂存区域,直接提交了,才是真正从本地仓库中删除了,例如以下:
$ git commit -m "delete b.txt"
[master 2daa294] delete b.txt
0 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 b.txt
而此时,b.txt文件也从工作文件夹中被删除了。
$ ls
a.txt c.txt d.txt test.txt
$ git rm --cached d.txt
rm 'd.txt'
查看状态可发现d.txt已经存在于Untracked files以下了:
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: d.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# d.txt
提交之后,再看一下Git状态:
$ git commit -m "remove d.txt from git"
[master d625705] remove d.txt from git
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 d.txt
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# d.txt
nothing added to commit but untracked files present (use "git add" to track)
最后,还能够使用log命令来查看多次的提交信息,例如以下:
$ git log
commit d62570527d545ae2677708ac2f9b015aab2df86f
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:31:10 2014 +0800 remove d.txt from git commit 2daa294a500bf9b3b0af7fe586353d882f0c3592
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:27:50 2014 +0800 delete b.txt commit d15838d91e2c09e5e25a0a348e4dfb27c7e6b928
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:18:28 2014 +0800 Commit the changes commit b52882cca685b024991dec8b976c35a1cbc6c9cf
Author: linmiansheng <sheepjtgjfc@163.com>
Date: Tue Jun 17 14:11:13 2014 +0800
结束。
Git 基本使用方法的更多相关文章
- 在Linux下搭建Git服务器的方法是什么样?
第一步 安装git:可以通过命令的方式快速安装,不同的linux的安装方法可能不一样,我的是采用的yum方法.ubuntu可以用apt-get命令.sudo yum install git 第二步 添 ...
- android studio下gradle与Git错误解决方法
Error: Gradle: Execution failed for task ':mytask' > A problem occurred starting process 'command ...
- GIT工程迁移方法总结
Git工程迁移方法总结 Git最近准备迁移一下位置,这里采用命令行的方式,做如下操作. 1.git init 初始化git仓库,这个时候发现本地文件夹多了个.git的文件夹. 2.git remot ...
- Git工程迁移方法总结(命令行)
Git工程迁移方法总结 Git工程迁移方法总结 Git最近准备迁移一下位置,这里采用命令行的方式,做如下操作. 1.git init 初始化git仓库,这个时候发现本地文件夹多了个.git的文件夹. ...
- Git工程迁移方法总结(命令行) .(转载)
原文地址:http://blog.csdn.net/hongshan50/article/details/236630433 Git工程迁移方法总结 Git工程迁移方法总结 Git最近准备迁移一下位置 ...
- Ubuntu系统下Jenkins的git构建基本方法
上一博文讲到了本地脚本的构建方法. 本篇博文主要讲“Ubuntu系统下Jenkins的git构建基本方法”. 点击保存后即可完成简单的构建. 构建触发器 这个触发器是决定什么时候触发构建,可以设置为定 ...
- git log 退出方法
前言 使用git的过程中会有一些疑问,理当记录,方便自己随时查看,可能也会帮助他人解惑,甚好! 1.git log退出方法 使用git log之后无法回到主页面,如下图所示,最后只能暴力关闭git b ...
- GIT的使用方法
GIT的使用方法 1.电脑首先安装GIT, 2.在官网注册GitHub账号. 一,使用git在控制台进行本地操作 1.打开GitBash 2.填写用户名和邮箱作为标识分别执行以下命令: git/ co ...
- 简易搭建git仓库、关联远程和本地仓库方法。克隆仓库方法。同一台电脑上创建两个git ssh key方法。
一,在github上建仓库 react-js-antd-demo: 二:将远程仓库与本地仓库关联 git remote add origin git@github.com:begin256/react ...
- 独家git clone 加速方法
git clone 独家方法 最近需要下载网上很多github库,所以git clone 4kb/s 的速度可以把人逼疯,为了加速git clone才有了这篇博客 网上有很多加速的方案 比如 blog ...
随机推荐
- WebGoat学习(一)--环境搭建
参考https://www.owasp.org/index.php/Category:OWASP_WebGoat_Project https://github.com/WebGoat/WebGoat ...
- 83.const与类
const常量对象,无法改变数据,只能引用尾部带const方法 类的成员如果是const,可以默认初始化,也可以构造的初始化,不可在构造函数内部初始化 类中的const成员,无法直接修改,可以间接修改 ...
- POJ 3038 贪心(multiset)
题意: 思路: 1. 贪心 我们考虑肯定是走最近的最合适 想象自己是一个黑一日游的司机: 1.如果有乘客要上车,那么就让他上,收钱! 2.如果超载了,把距目的地最远的几个乘客踢下去,退钱. 3.行驶到 ...
- HTML 页面内容禁止选中
写一个小笔记,怎么禁止HTML页面不被选中,复制. CSS: *{ moz-user-select: -moz-none; -moz-user-select: none; -o-user-select ...
- ZJOI2005沼泽鳄鱼
矩阵优化dp ** 注意:矩阵乘法没有交换律 ** 思路:类比P2151hh去散步 代码特点在一维的答案矩阵 1.矩阵优化两点间方案数不必赘述 2.注意2,3,4可以办到以他们的lcm为周期,正是因为 ...
- Python 学习 第三天 课后总结:
PYTHON学习第三天课后总结: 1,注释:就是对代码起到说明注解的作用. 注释分为单行注释与多行注释. 单行注释:只注释一行代码在需要注释的所在行的行首使用#号来注释此行,注意#与代码之间需要 ...
- c# 用代码来设置程序的PrivatePath
原文:c# 用代码来设置程序的PrivatePath 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sweety820/article/detail ...
- 【习题 8-1 UVA - 1149】Bin Packing
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个背包只能装两个东西. 而且每个东西都要被装进去. 那么我们随意考虑某个物品.(不必要求顺序 这个物品肯定要放进某个背包里面的. ...
- js09--函数 call apply
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 【问题】VUE 同一页面路由参数变化,数据不刷新
依赖路由的params参数获取写在created生命周期里面,因为相同路由二次甚至多次加载的关系 没有达到监听,退出页面再进入另一个页面并不会运行created组件生命周期,导致数据还是第一次进入的数 ...