Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。
1.安装git,建立存放git代码的目录以及初始化
[root@server1 ~]# yum install -y git
[root@server1 ~]# mkdir demo
[root@server1 ~]# cd demo/
[root@server1 demo]# git init ##初始化
Initialized empty Git repository in /root/demo/.git/
[root@server1 demo]# git status #显示工作目录和暂存区的状态
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
2.创建一个文件,git status显示
[root@server1 demo]# touch file1
[root@server1 demo]# echo dzh > file1
[root@server1 demo]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# file1
nothing added to commit but untracked files present (use "git add" to track)
[root@server1 demo]# git status -s
?? file1 ##新添加的未跟踪的文件前面有??标志
3.把文件添加进去(将文件修改添加到暂存区)
[root@server1 demo]# git add file1
[root@server1 demo]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached ..." to unstage)
#
# new file: file1
#
[root@server1 demo]# git status -s
A file1
[root@server1 demo]# git commit -m "add file1"
*** Please tell me who you are.
Run
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@server1.(none)')
[root@server1 demo]# git config --global user.email "[email protected]"
[root@server1 demo]# git config --global user.name "dzh"
[root@server1 demo]# git commit -m "add file1"
[master (root-commit) 1aadc8d] add file1
1 file changed, 1 insertion(+)
create mode 100644 file1
[root@server1 demo]# git status -s
[root@server1 demo]# echo zyx >> file1
[root@server1 demo]# git status
# On branch master
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: file1
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@server1 demo]# git status -s
M file1
[root@server1 demo]# git add file1
[root@server1 demo]# git status -s
M file1
[root@server1 demo]# echo zyx2 >> file1
[root@server1 demo]# git status -s
MM file1
[root@server1 demo]# git add file1
[root@server1 demo]# git status -s
M file1
[root@server1 demo]# git commit -m "change file1"
[master 1a7080c] change file1
1 file changed, 2 insertions(+)
6.忽略文件
一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。 通常都是些自动生成的文件,比如日志文件,或者编译过程中创建的临时文件等。 在这种情况下,我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式。
[root@server1 demo]# l.
. .. .git
[root@server1 demo]# cd
[root@server1 ~]# l.
. .bash_logout .bashrc .gitconfig .viminfo
.. .bash_profile .cshrc .tcshrc
[root@server1 ~]# cp .bashrc demo/
[root@server1 ~]# cd demo/
[root@server1 demo]# git status -s
?? .bashrc
[root@server1 demo]# vim .gitignore
.*
[root@server1 demo]# git status -s
[root@server1 demo]# l.
. .. .bashrc .git .gitignore
7.版本回退
文件修改到一定程度的时候,就可以“保存一个快照”,这个快照在Git中被称为commit。一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复
丢弃工作区的修改,让这个文件回到最近一次git commit或git add时的状态
[root@server1 demo]# echo zyx3 >> file1
[root@server1 demo]# cat file1
dzh
zyx
zyx2
zyx3
[root@server1 demo]# git checkout -- file1
[root@server1 demo]# cat file1
dzh
zyx
zyx2
[root@server1 demo]# git status
# On branch master
nothing to commit, working directory clean
[root@server1 demo]# git log
commit 1a7080cf454c2c4f34222726cb8d32d92698e16a
Author: dzh <[email protected]>
Date: Thu Jun 20 20:29:34 2019 +0800
change file1
commit 1aadc8d1ba6bc9c68996169a5c07f87583c146f3
Author: dzh <[email protected]>
Date: Thu Jun 20 20:25:53 2019 +0800
add file1
[root@server1 demo]# echo zyx3 > file1
[root@server1 demo]# cat file1
zyx3
[root@server1 demo]# git add file1
[root@server1 demo]# git commit -m "v3"
[master 2aded87] v3
1 file changed, 1 insertion(+), 3 deletions(-)
[root@server1 demo]# git log
commit 2aded872e2279847678299973f0f5dd1976dab62
Author: dzh <[email protected]>
Date: Thu Jun 20 20:41:54 2019 +0800
v3
commit 1a7080cf454c2c4f34222726cb8d32d92698e16a
Author: dzh <[email protected]>
Date: Thu Jun 20 20:29:34 2019 +0800
change file1
commit 1aadc8d1ba6bc9c68996169a5c07f87583c146f3
Author: dzh <[email protected]>
Date: Thu Jun 20 20:25:53 2019 +0800
add file1
[root@server1 demo]# git log --pretty=oneline
2aded872e2279847678299973f0f5dd1976dab62 v3
1a7080cf454c2c4f34222726cb8d32d92698e16a change file1
1aadc8d1ba6bc9c68996169a5c07f87583c146f3 add file1
1aadc8d1ba6bc9c68996169a5c07f87583c146f3 add file1
[root@server1 demo]# git reflog
2aded87 HEAD@{0}: commit: v3
1a7080c HEAD@{1}: commit: change file1
1aadc8d HEAD@{2}: commit (initial): add file1
[root@server1 demo]# git status -s
[root@server1 demo]# cat file1
zyx3
[root@server1 demo]# git reset --hard HEAD^
HEAD is now at 1a7080c change file1
[root@server1 demo]# cat file1
dzh
zyx
zyx2
[root@server1 demo]# git log --pretty=oneline
1a7080cf454c2c4f34222726cb8d32d92698e16a change file1
1aadc8d1ba6bc9c68996169a5c07f87583c146f3 add file1
[root@server1 demo]# git reflog
1a7080c HEAD@{0}: reset: moving to HEAD^
2aded87 HEAD@{1}: commit: v3
1a7080c HEAD@{2}: commit: change file1
1aadc8d HEAD@{3}: commit (initial): add file1
[root@server1 demo]# git reset --hard 2aded87
HEAD is now at 2aded87 v3
[root@server1 demo]# cat file1
zyx3
8.删除后恢复
[root@server1 demo]# rm -f file1
[root@server1 demo]# git checkout -- file1
[root@server1 demo]# ls
file1
[root@server1 demo]# rm -f file1
[root@server1 demo]# git rm file1
rm 'file1'
[root@server1 demo]# git commit -m "del file1"
[master 8ad5e62] del file1
1 file changed, 1 deletion(-)
delete mode 100644 file1
[root@server1 demo]# git reflog
8ad5e62 HEAD@{0}: commit: del file1
2aded87 HEAD@{1}: reset: moving to 2aded87
1a7080c HEAD@{2}: reset: moving to HEAD^
2aded87 HEAD@{3}: commit: v3
1a7080c HEAD@{4}: commit: change file1
1aadc8d HEAD@{5}: commit (initial): add file1
[root@server1 demo]# git reset --hard 2aded87
HEAD is now at 2aded87 v3
[root@server1 demo]# ls
file1