安装运用版本控制系统Git
一、安装git
1.在linux系统下安装git:
yum -y install git
2.指定用户名和Email地址
git config --global user.name “Your Name”
git config --global user.email “[email protected]”
二、创建版本库
1.找一个合适的地方创建一个空目录或者也可以不用创建空目录,选择一个目录作为git的版本库,但是最好新建目录,便于管理
mkdir git
cd git
2.通过git --bare init 命令将这个目录变成Git可以管理的仓库
[[email protected] git]# git --bare init
Initialized empty Git repository in /Users/michael/learngit/.git/
执行git config --global color.ui true,让输出的文件名称显示颜色
3.查看该目录下会新生成一个.git目录,这个目录是Git用来跟踪管理版本库的
[[email protected] git]# ls -an
drwxr-xr-x. 8 0 0 4096 11月 24 11:20 .git
这样Git仓库就已经创建好了
4.在该目录下随便创建一个文件,文件内容可以自定义
[[email protected] git]# cat ceshi.txt
1.word
5.用git add命令告诉git将文件添加到仓库
git add ceshi.txt
6.git commit 命令将该文件提交到git仓库
[[email protected] git]# git commit -m "wrote the first file"
[master (root-commit) f53ca5a] wrote the first file
1 files changed, 1 insertions(+), 0 deletions(-) (返回值告诉你一个文件被改动,插入了一行内容,在ceshi.txt里有一行内容)
create mode 100644 ceshi.txt
注意:该命令中的-m是只本次提交的说明,可以自定义说明文字,但是需要有意义,这样可以在历史记录找到改动的记录
三、查看工作状态和查看修改的内容
1.刚才我们已经成功的添加并且提交了一个ceshi.txt文件,接下来我们改动该文件的内容
[[email protected] git]# cat ceshi.txt
2.good
2.现在我们运行git status查看工作状态
[[email protected] git]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: ceshi.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
上面的返回告诉我们,ceshi.txt文件被修改过了,但是还没有提交
3.我们虽然把ceshi.txt文件修改过了,但是如果我们忘记了修改的内容,也是可以查看到的
[[email protected] git]# git diff
diff --git a/ceshi.txt b/ceshi.txt
index b05d8c7..bdbace1 100644
--- a/ceshi.txt
+++ b/ceshi.txt
@@ -1 +1 @@
-1.word
+2.good
上面的返回告诉我们将1.word修改成了2.good
4.接下来我们需要将ceshi.txt文件重新添加到git仓库
[[email protected] git]# git add ceshi.txt
5.然后我们再次查看下工作状态
[[email protected] git]# git status
# On branch master
# Changes to be committed:(将被提交的更改)
# (use "git reset HEAD <file>..." to unstage)
#
# modified: ceshi.txt
上面返回告诉我们被提交的修改包括ceshi.txt
6.这下我们可以放心提交该文件了
[[email protected] git]# git commit -m "worte the second file"
[master 4b291e1] worte the second file
1 files changed, 1 insertions(+), 1 deletions(-)
返回提示我们一个文件被更改,一行被插入
7.提交完成之后我们再次查看工作状态
[[email protected] git]# git status
# On branch master
nothing to commit (working directory clean)
返回提示我们没有需要提交的修改,工作目录为是干净的
四、版本回退(回档)
1.我们再次修改文件内容进行提交
[[email protected] git]# cat ceshi.txt
3.bad
[[email protected] git]# git add ceshi.txt
[[email protected] git]# git commit -m "wrote the third file"
[master 2a2747f] wrote the third file
1 files changed, 1 insertions(+), 1 deletions(-)
[[email protected] git]#
2.查看历史修改记录可用git log进行查看
[[email protected] git]# git log
commit 2a2747f9d2b8c2a114362f6e1858cae3e606e33d
Author: “wenbinbin” <“[email protected]”>
Date: Thu Nov 24 11:56:57 2016 +0800
wrote the third file
commit 4b291e14e0b6cb3f30fd0d9473b07eef14135075
Author: “wenbinbin” <“[email protected]”>
Date: Thu Nov 24 11:52:32 2016 +0800
worte the second file
commit f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1
Author: “wenbinbin” <“[email protected]”>
Date: Thu Nov 24 11:35:13 2016 +0800
wrote the first file
You have new mail in /var/spool/mail/root
[[email protected] git]#
以上显示的是从最近到最远的提交日志
最近的提交是wrote the third file,最远的提交是wrote the first file
3.或者我们可以用最简洁的查看方式
[[email protected] git]# git log --pretty=oneline
2a2747f9d2b8c2a114362f6e1858cae3e606e33d wrote the third file
4b291e14e0b6cb3f30fd0d9473b07eef14135075 worte the second file
f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1 wrote the first file
[[email protected] git]#
以上返回前面的是commit id后面的是提交说明
4.版本回退
a:回退到上一个版本
我们在用git log查看到当前版本是write the third file,现在我们回退到上一个版本
[[email protected] git]# git reset --hard HEAD^
HEAD is now at 4b291e1 worte the second file
提示中说明已经回退到上个版本,我们可以查看下文件内容
[[email protected] git]# cat ceshi.txt
2.good
[[email protected] git]#
确实已经回退到上个版本
我们用git log再看看现在的版本库状态
[[email protected] git]# git log
commit 4b291e14e0b6cb3f30fd0d9473b07eef14135075
Author: “wenbinbin” <“[email protected]”>
Date: Thu Nov 24 11:52:32 2016 +0800
worte the second file
commit f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1
Author: “wenbinbin” <“[email protected]”>
Date: Thu Nov 24 11:35:13 2016 +0800
wrote the first file
You have new mail in /var/spool/mail/root
[[email protected] git]#
提示说明当前版本是wrote the second file,但是我们最新的那个版本(wrote the third file)看不见了,我们可以通过刚才查看到的commit id进行回退到刚才的新版本
b:恢复刚才的回退操作
我们可以取刚才用git log --pretty=oneline命令查看到的commit id进行恢复
[[email protected] git]# git log --pretty=oneline
2a2747f9d2b8c2a114362f6e1858cae3e606e33d wrote the third file
4b291e14e0b6cb3f30fd0d9473b07eef14135075 worte the second file
f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1 wrote the first file
恢复时只取commit id的前几个数据即可
[[email protected] git]# git reset --hard 2a2747f9
HEAD is now at 2a2747f wrote the third file
[[email protected] git]#
提示告诉我们现在的版本是wrote the third file,恢复成功我们可以查看下ceshi.txt文件内容
[[email protected] git]# cat ceshi.txt
3.bad
[[email protected] git]#
C:回退到上上个版本
[[email protected] git]# git reset --hard HEAD^^
HEAD is now at f53ca5a wrote the first file
[[email protected] git]#
提示告诉我们现在回退到的版本是wrote the fist file,查看ceshi.txt验证
[[email protected] git]# cat ceshi.txt
1.word
[[email protected] git]#
确实已经回退
[[email protected] git]# git log
commit f53ca5aa0b8dbf569bc229c81f1e3118e66b34b1
Author: “wenbinbin” <“[email protected]”>
Date: Thu Nov 24 11:35:13 2016 +0800
wrote the first file
[[email protected] git]#
日志中显示当前版本是wrote the fist file
d:回退到上上上个版本用git reset --hard HEAD^^^
e:回退到第100个版本用git reset --hard HEAD~100
5.如果我们回退到了第一个版本又想恢复怎么办,在git中有个git reflog命令会记录你的每一次操作命令
[[email protected] git]# git reflog
f53ca5a [email protected]{0}: HEAD^^: updating HEAD
2a2747f [email protected]{1}: 2a2747f9: updating HEAD
4b291e1 [email protected]{2}: HEAD^: updating HEAD
2a2747f [email protected]{3}: commit: wrote the third file
4b291e1 [email protected]{4}: commit: worte the second file
[[email protected] git]#
可以查看到commit id进行回退操作
五、误操作恢复
1.当你在ceshi.txt文件中添加了一行,猛然发现你添加的这行代码是错的,这个时候你可以手动删除添加的那一行内容,然后执行git checkout -- ceshi.txt来舍弃之前的误操作
[[email protected] git]# git checkout -- ceshi.txt
[[email protected] git]# git status
# On branch master
nothing to commit (working directory clean)
[[email protected] git]#
2.如果你在修改完之后还做了git add。想要撤销需要执行下面的命令
[[email protected] git]# git reset HEAD ceshi.txt
Unstaged changes after reset:
M ceshi.txt
[[email protected] git]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: ceshi.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] git]# git checkout -- ceshi.txt
[[email protected] git]# git status
# On branch master
nothing to commit (working directory clean)
[[email protected] git]#
六、删除文件
1.我们新添加一个文件test.txt,并提交
[[email protected] git]# touch test.txt
[[email protected] git]# git add test.txt
[[email protected] git]# git commit -m "add test.txt"
[master 1a4d9dc] add test.txt
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
[[email protected] git]#
2.一般情况我们要删除文件是直接执行rm就可以
但是我们在git目录下将test.txt删除之后,在git版本库中这个文件还在
[[email protected] git]# rm -rf test.txt
[[email protected] git]# git status
# On branch master
# Changed but not updated:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[[email protected] git]#
Git告诉我们删除了一个test.txt文件但是版本库里的这个文件没有删除
a:如果你是不小心删除了这个文件可以进行恢复
[[email protected] git]# git checkout -- test.txt
You have new mail in /var/spool/mail/root
[[email protected] git]# ls
ceshi.txt test.txt
[[email protected] git]#
b:如果你需要在版本库里也删除的话,可以执行
[[email protected] git]# git rm test.txt
rm ‘test.txt‘
[[email protected] git]# git commit -m "remove test.txt"
[master 941e92c] remove test.txt
0 files changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.txt
[[email protected] git]# ls
ceshi.txt
[[email protected] git]#
七、配置别名
1.将git status用git st来代替,命令如下
git config --global alias.st status
这样我们既可以用git st 也可以用git status,其他的命令也可以自定义进行修改
2.显示最后一次提交信息需要执行
[[email protected] git]# git config --global alias.last ‘log -1‘
[[email protected] git]# git last
commit 941e92cc6807f67e5ba184dd796132626a94ae02
Author: “wenbinbin” <“[email protected]”>
Date: Thu Nov 24 15:14:56 2016 +0800
remove test.txt
[[email protected] git]#