Git-增操作
假设我们已经成功地添加并提交了一个readme.txt文件,内容如下:
Git is a distributed version control system.
Git is free software.
- 现在,运行
git status
命令看看结果:
[it@VM_0_12_centos learngit]$ 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: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
git status
命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,readme.txt
被修改过了,但还没有准备提交的修改。
2. 运行git add readme.txt
命令进行提交,让后再次查看运行状态:
[it@VM_0_12_centos learngit]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#
git status
告诉我们,将要被提交的修改包括readme.txt
,下一步,就可以放心地提交了。
3.运行git commit -m "your content"
[it@VM_0_12_centos learngit]$ git commit -m "your content"
[master 742d6b3] your content
1 file changed, 1 insertion(+)
提交后,我们再用git status
命令看看仓库的当前状态:
[it@VM_0_12_centos learngit]$ git status
# On branch master
nothing to commit, working directory clean
注释:
在git add readme.txt
之前,我们可以用命令git diff readme.txt
查看新增内容与原来有啥不同。知道了对readme.txt作了什么修改后,再把它提交到仓库就放心多了。
Git-改,查操作
- 对readme.txt做一个修改,比如修改内容内容:
原内容
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software.
修改内容
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes.
用命令git add readme.txt
增加到缓存区和git status
查询状态
[it@VM_0_12_centos learngit]$ git add readme.txt
[it@VM_0_12_centos learngit]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# your content
然后,再修改readme.txt:
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
用git commit
命令提交内容。
[it@VM_0_12_centos learngit]$ git commit -m "your content frist revise"
[master f39d716] your content frist revise
1 file changed, 3 insertions(+), 1 deletion(-)
查看状态
[it@VM_0_12_centos learngit]$ 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: readme.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# your content
no changes added to commit (use "git add" and/or "git commit -a")
提交后,用git diff HEAD -- readme.txt
命令可以查看工作区和版本库里面最新版本的区别:
[it@VM_0_12_centos learngit]$ git diff HEAD -- readme.txt
diff --git a/readme.txt b/readme.txt
index 76d770f..a9c5755 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
-Git tracks changes.
+Git tracks changes of files.
可见,第二次修改确实没有被提交。
那怎么提交第二次修改呢?可以继续git add
再git commit
,也可以别着急提交第一次修改,先git add
第二次修改,再git commit
,就相当于把两次修改合并后一块提交了:第一次修改 -> git add ->
第二次修改 -> git add -> git commit
Git是如何跟踪修改的? 每次修改,如果不add到暂存区,那就不会加入到commit中。
Git-删操作
在readme.txt
中添加一行:
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
你猛然发现了“stupid boss”
可能会让你丢掉这个月的奖金!既然错误发现得很及时,就可以很容易地纠正它。你可以删掉最后一行,手动把文件恢复到上一个版本的状态。
还未运行git add
git checkout -- file
可以丢弃工作区的修改:
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
命令git checkout -- readme.txt
意思就是,把readme.txt
文件在工作区的修改全部撤销,这里有两种情况:
1. readme.txt
自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
2. readme.txt
已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit
或git add
时的状态。
还未git commit
现在假定是凌晨3点,你不但写了一些胡话,还git add到暂存区了:
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
[it@VM_0_12_centos learngit]$ git add readme.txt
庆幸的是,在commit
之前,你发现了这个问题。用git status
查看一下,修改只是添加到了暂存区,还没有提交:
[it@VM_0_12_centos learngit]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# your content
Git
同样告诉我们,用命令git reset HEAD file
可以把暂存区的修改撤销掉(unstage
),重新放回工作区:
[it@VM_0_12_centos learngit]$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
My stupid boss still prefers SVN.
[it@VM_0_12_centos learngit]$ git checkout -- readme.txt
[it@VM_0_12_centos learngit]$ cat readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
[it@VM_0_12_centos learngit]$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# your content
nothing added to commit but untracked files present (use "git add" to track)
git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最新的版本。
版本回退
现在,假设你不但改错了东西,还从暂存区提交到了版本库,怎么办呢?
在Git
中,我们用git log
命令查看历史记录:
[it@VM_0_12_centos learngit]$ git log
commit 429faa26d3bd734e1c025331baeb9680102db699
Author: it <wangkai@keking.cn>
Date: Fri May 4 11:02:19 2018 +0800
your content second revise
commit f39d7169c31680dc2545ceaed0735e518f773352
Author: it <wangkai@keking.cn>
Date: Fri May 4 10:35:08 2018 +0800
your content frist revise
commit ef9b88e11c4ec61697573a2bb51c3d6c23293ab6
Author: it <wangkai@keking.cn>
Date: Thu May 3 18:18:55 2018 +0800
add reading
commit 09bdf04e92e6ef98a4d754c4ead6995b95ce9712
Author: it <wangkai@keking.cn>
Date: Thu May 3 15:55:09 2018 +0800
wrote a readme file
[it@VM_0_12_centos learngit]$
git log
命令显示从最近到最远的提交日志。如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline
参数:
[it@VM_0_12_centos learngit]$ git log --pretty=oneline
429faa26d3bd734e1c025331baeb9680102db699 your content second revise
f39d7169c31680dc2545ceaed0735e518f773352 your content frist revise
ef9b88e11c4ec61697573a2bb51c3d6c23293ab6 add reading
09bdf04e92e6ef98a4d754c4ead6995b95ce9712 wrote a readme file
友情提示:你看到的一大串类似3628164…882e1e0的是commit id(版本号)
回退到上一个版本
首先,Git
必须知道当前版本是哪个版本,在Git
中,用HEAD
表示当前版本,也就是最新的提交3628164...882e1e0
(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^
,上上一个版本就是HEAD^^
,当然往上100
个版本写100
个^
比较容易数不过来,所以写成HEAD~100
。
现在,我们要把当前版本your content second revise
回退到上一个版本your content frist revise
,可以使用git reset
命令:
方法一(HEAD^方法)
[it@VM_0_12_centos learngit]$ git reset --hard HEAD^
HEAD is now at f39d716 your content frist revise
方法2(版本号 方法)
[it@VM_0_12_centos learngit]$ git reset --hard f39d
HEAD is now at f39d716 your content frist revise
[it@VM_0_12_centos learngit]$
版本号没必要写全,前几位就可以了,Git会自动去找。当然也不能只写前一两位,因为Git可能会找到多个版本号,就无法确定是哪一个了。该方法可以用来指向任意一个版本
特别提醒
现在,你回退到了上个版本,关掉了电脑,第二天早上就后悔了,想恢复到新版本怎么办?找不到新版本的commit id怎么办?
可以用 git reflog
查看命令历史,然后再用git reset --hard commit_id
以便确定要回到未来的哪个版本.
[it@VM_0_12_centos learngit]$ git reset --hard 429faa2
HEAD is now at 429faa2 your content second revise
情景使用
- HEAD指向的版本就是当前版本,因此,
Git
允许我们在版本的历史之间穿梭,使用命令git reset --hard commit_id
。 - 穿梭前,用
git log
可以查看提交历史,以便确定要回退到哪个版本。 - 要重返未来,用
git reflog
查看命令历史,以便确定要回到未来的哪个版本。
Git-删操作
在Git
中,删除也是一个修改操作,我们实战一下,先添加一个新文件test.txt
到Git并且提交:
[it@VM_0_12_centos learngit]$ touch test.txt
[it@VM_0_12_centos learngit]$ ls
readme.txt test.txt your content
[it@VM_0_12_centos learngit]$ git add test.txt
[it@VM_0_12_centos learngit]$ git commit -m "add test.txt"
[master 29a284e] add test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm
命令删了:
[it@VM_0_12_centos learngit]$ rm test.txt
[it@VM_0_12_centos learngit]$ git status
# On branch master
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: test.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# your content
no changes added to commit (use "git add" and/or "git commit -a")
这个时候,Git
知道你删除了文件,因此,工作区和版本库就不一致了,git status
命令会立刻告诉你哪些文件被删除了:
现在你有两个选择。
1. 确实要从版本库中删除该文件,那就用命令git rm
删掉,并且git commit
:
[it@VM_0_12_centos learngit]$ git rm test.txt
rm 'test.txt'
[it@VM_0_12_centos learngit]$ git commit -m "remove test.txt"
[master 54cc88e] remove test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.txt
现在,文件就从版本库中被删除了。
2. 另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
[it@VM_0_12_centos learngit]$ ls
readme.txt your content
[it@VM_0_12_centos learngit]$ git checkout -- test.txt
[it@VM_0_12_centos learngit]$ ls
readme.txt test.txt your conte
git checkout
其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。
小结
命令git rm
用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。
应用场景
- 场景1:当你改乱了工作区某个文件的内容,想直接丢弃工作区的修改时,用命令
git checkout -- file
。 - 场景2:当你不但改乱了工作区某个文件的内容,还添加到了暂存区时,想丢弃修改,分两步,第一步用命令
git reset HEAD file
,就回到了场景1,第二步按场景1操作。 - 场景3:已经提交了不合适的修改到版本库时,想要撤销本次提交,参考版本回退一节,不过前提是没有推送到远程库。