1 安装git
centos:yum install –y git
ubuntu:apt –get install git
2 配置用户
[root@localhost github]# git config --global user.name "fbx"
[root@localhost github]# git config --global user.email "fbx@idmxh.com"
3 创建版本库、初始化、及提交
1) 创建仓库
[root@localhost home]# mkdir github
[root@localhost home]# cd github
[root@localhost github]# pwd
/home/github
2) 初始化
[root@localhost github]# git init
Initialized empty Git repository in /home/github/.git/
该步骤完成后会新建.git文件。
[root@localhost github]# ls -a
. .. .git
3) 添加目录/文件入仓库
[root@localhost github]# cp -R /home/wwwroot/default/newtravel .
[root@localhost github]# ls
newtravel
[root@localhost github]# git add newtravel
4) 提交改变
[root@localhost github]# git commit -m "add newtravel floder"
注:-m后为本次提交的改变的注释
4 查看仓库状态
[root@localhost github]# 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: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
5 查看文件更改的内容
[root@localhost github]# git diff readme.txt
diff --git a/readme.txt b/readme.txt
index 34884aa..a98f1ba 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,2 +1 @@
this is a file to help you know me!
-add new line!
6 查看更改历史
[root@localhost github]# git log
commit 5e3d07b852cbd460a51b84058b48304fc0bb55d8
Author: fbx <fbx@idmxh.com>
Date: Sat Jan 9 17:44:08 2016 +0800
delete last line
commit 916e1b47e125ef83e1751b23016b8d0f3843a08c
Author: fbx <fbx@idmxh.com>
Date: Sat Jan 9 17:42:55 2016 +0800
delete last line
commit 465f50a73bf48eccb23f3d839f725c00a3dcd39e
Author: fbx <fbx@idmxh.com>
Date: Sat Jan 9 17:39:51 2016 +0800
add new file to readme.txt
注:如想简化显示,则添加参数—pretty=oneline
[root@localhost github]# git log --pretty=oneline
5e3d07b852cbd460a51b84058b48304fc0bb55d8 delete last line
916e1b47e125ef83e1751b23016b8d0f3843a08c delete last line
465f50a73bf48eccb23f3d839f725c00a3dcd39e add new file to readme.txt
c126fd1f4b406950aeb2b29f29dd6dc3dcd9fc3e add readme.txt file
fe4a9132d0f83c53746640962ab21d4c89bf12fe add newtravel floder
7 版本回退
HEAD表示当前版本,HEAD^表示上一版本,HEAD^^上两个版本,HEAD~20表示回退到20版本
回退到后续版本:
[root@localhost github]# git reset --hard HEAD^^
HEAD is now at 465f50a add new file to readme.txt
回退到未来版本:
[root@localhost github]# git reset --hard 5e3d07b852cbd460a51b84058b48304fc0bb55d8
HEAD is now at 5e3d07b delete last line
查看历史提交:
[root@localhost github]# git reflog
5e3d07b HEAD@{0}: 5e3d07b852cbd460a51b84058b48304fc0bb55d8: updatin
465f50a HEAD@{1}: HEAD^^: updating HEAD
5e3d07b HEAD@{2}: commit: delete last line
916e1b4 HEAD@{3}: commit: delete last line
465f50a HEAD@{4}: commit: add new file to readme.txt
c126fd1 HEAD@{5}: commit: add readme.txt file
fe4a913 HEAD@{6}: commit (initial): add newtravel floder
8 撤销修改
[root@localhost github]# git checkout -- readme.txt
9 文件删除
[root@localhost github]# rm readme.txt
rm: remove regular file `readme.txt'? y
[root@localhost github]# ls
newtravel
[root@localhost github]# 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: readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
此时并未真正删除:
1)恢复:
git checkout – readme.txt
[root@localhost github]# git checkout -- readme.txt
[root@localhost github]# ls
newtravel readme.txt
2) 删除
[root@localhost github]# ls
newtravel
[root@localhost github]# git rm readme.txt
rm 'readme.txt'
[root@localhost github]# git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: readme.txt
#
[root@localhost github]# git commit -m "delete readme.txt file"
[master ab53274] delete readme.txt file
1 files changed, 0 insertions(+), 2 deletions(-)
delete mode 100644 readme.txt
[root@localhost github]# git status
# On branch master
nothing to commit (working directory clean)
[root@localhost github]# git log --pretty=oneline
ab532746073c9fe2ddf0580a71995152d148c20e delete readme.txt file
5e3d07b852cbd460a51b84058b48304fc0bb55d8 delete last line
916e1b47e125ef83e1751b23016b8d0f3843a08c delete last line
465f50a73bf48eccb23f3d839f725c00a3dcd39e add new file to readme.txt
c126fd1f4b406950aeb2b29f29dd6dc3dcd9fc3e add readme.txt file
fe4a9132d0f83c53746640962ab21d4c89bf12fe add newtravel floder
至此,简单的GIT操作已经学完,尽情享受GIT带来的变成享受吧!ps:GIT除了可以用作编程版本托管,还有很多作用,比如最简单的Word文档写作的时候,再也不用担心未保存了。
所有评论(0)