git日常使用经验总结

###初始化一个git仓库
初始化一个仓库,用如下命令。
git init
创建一个文件
echo “Git is a version contron system.”> readme.txt
提交一个文件,提交文件到仓库
git add readme.txt
git commit -m “write a readme file”

###案例1:创建一个git库
[root@test-devops git]# yum install git -y
[root@test-devops git]# mkdir /home/docker/git
[root@test-devops git]# cd /home/docker/git
[root@test-devops git]# git init
[root@test-devops git]# git config –global user.name “Steven”
[root@test-devops git]# git config –global user.email “admin@opsnotes.net”
[root@test-devops git]# git add file1.txt
[root@test-devops git]# git commit -m ‘add file1.txt’
[master 812c99b] add file1.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file1.txt
[root@test-devops git]#

###git status的用法:
修改readme.txt
[root@test-devops git]# cat readme.txt
Git is a version contron system.
Git is a version contron system.
[root@test-devops git]#
git status命令可以让我们时刻掌握仓库当前的状态,上面的命令告诉我们,readme.txt被修改过了,但还没有准备提交的修改。
[root@test-devops 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:   readme.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@test-devops git]#

###git diff的用法
[root@test-devops git]# git diff readme.txt
diff –git a/readme.txt b/readme.txt
index 3dd18aa..8e49e51 100644
— a/readme.txt
+++ b/readme.txt
@@ -1 +1,2 @@
Git is a version contron system.
+Git is a version contron system.
[root@test-devops git]#

###git add上传文件
[root@test-devops git]# git add readme.txt
[root@test-devops git]# git status

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    modified:   readme.txt
#
[root@test-devops git]#

###git commit 提交代码
[root@test-devops git]# git commit -m ‘add readme.txt’
[master 8d3fb4d] add readme.txt
1 files changed, 1 insertions(+), 0 deletions(-)
[root@test-devops git]#
git status查看是不是有文件
[root@test-devops git]# git status

# On branch master
nothing to commit (working directory clean)
[root@test-devops git]#

###git log查看日志
[root@test-devops git]# git log
commit 8d3fb4d2deac9f8dffa4fb88dcddda6a98bcf3f0
Author: Steven admin@opsnotes.net
Date: Wed Jul 8 14:55:46 2015 +0800

    add readme.txt

commit 812c99b437c852046d0909b47f3fdf7322739158
Author: Steven <admin@opsnotes.net>
Date:   Wed Jul 8 14:46:41 2015 +0800

    add file1.txt

commit af84ff0f589dd796830db9948b313bd58bf5b729
Author: root <root@test-devops.(none)>
Date:   Wed Jul 8 14:45:03 2015 +0800

    add file.txt

commit d8528dda54fa17e0af306295182331993367ef18
Author: root <root@test-devops.(none)>
Date:   Wed Jul 8 14:43:04 2015 +0800

    write a readme file
[root@test-devops git]#


git log命令显示从最近到最远的提交日志,我们可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file。
如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:


[root@test-devops git]# git log --pretty=oneline
8d3fb4d2deac9f8dffa4fb88dcddda6a98bcf3f0 add readme.txt   需要记住这个ID
812c99b437c852046d0909b47f3fdf7322739158 add file1.txt
af84ff0f589dd796830db9948b313bd58bf5b729 add file.txt
d8528dda54fa17e0af306295182331993367ef18 write a readme file
[root@test-devops git]#

###版本回滚
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,也就是最新的提交3628164…882e1e0(注意我的提交ID和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。
回退到上一个版本
[root@test-devops git]# git reset –hard HEAD^
HEAD is now at 812c99b add file1.txt
[root@test-devops git]#
查看文件是不是回退到以前了
[root@test-devops git]# cat readme.txt
Git is a version contron system.
[root@test-devops git]#

查看git log
[root@test-devops git]# git log --pretty=oneline
812c99b437c852046d0909b47f3fdf7322739158 add file1.txt
af84ff0f589dd796830db9948b313bd58bf5b729 add file.txt
d8528dda54fa17e0af306295182331993367ef18 write a readme file
[root@test-devops git]#

回滚到添加文件最新的状态
是在回滚之前的ID
[root@test-devops git]# git reset --hard 8d3fb
HEAD is now at 8d3fb4d add readme.txt
[root@test-devops git]# 
查看文件回来了。
[root@test-devops git]# cat readme.txt
Git is a version contron system.
Git is a version contron system.
[root@test-devops git]#

###git 历史命令
[root@test-devops git]# git reflog
8d3fb4d HEAD@{0}: 8d3fb: updating HEAD
812c99b HEAD@{1}: HEAD^: updating HEAD
8d3fb4d HEAD@{2}: commit: add readme.txt
812c99b HEAD@{3}: commit: add file1.txt
af84ff0 HEAD@{4}: commit: add file.txt
d8528dd HEAD@{5}: commit (initial): write a readme file
[root@test-devops git]#

###git 版本库和工作区的区别:
git版本库
[root@test-devops git]# ll -d /home/docker/git/.git/
drwxr-xr-x 8 root root 4096 7月 8 15:07 /home/docker/git/.git/
[root@test-devops git]#
git工作区
[root@test-devops git]# ll -d /home/docker/git/
drwxr-xr-x 3 root root 4096 7月 8 15:07 /home/docker/git/
[root@test-devops git]#

OpsNotes wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!