git命令操作

[root@huangstts ~]# mkdir pcode
[root@huangstts ~]# cd pcode/
[root@huangstts pcode]# git init
初始化空的 Git 版本库于 /root/pcode/.git/
[root@huangstts pcode]# ls -a
.  ..  .git
[root@huangstts pcode]# vim readme.txt
->readme
[root@huangstts pcode]# git add readme.txt
[root@huangstts pcode]# git commit -m "readme" .
[master(根提交) 1f1325f] readme
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt
[root@huangstts pcode]# git status 
# 位于分支 master
无文件要提交,干净的工作区
[root@huangstts pcode]# vim readme.txt
[root@huangstts pcode]# git add .
[root@huangstts pcode]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#   修改:      readme.txt
#
[root@huangstts pcode]# git commit -m "123" .
[master 11433fb] 123
 1 file changed, 3 insertions(+)


[root@huangstts pcode]# git log
commit ae7c9df32c8b3c9b463df9096aff67384ee56d24
Author: huangstts <huangstts@126.com>
Date:   Mon Aug 19 18:55:50 2019 +0800

    test.txt

commit 11433fb65fda8fd03c4554037500db2ec9bef035
Author: huangstts <huangstts@126.com>
Date:   Mon Aug 19 18:51:01 2019 +0800

    123


commit 1f1325fa091f01bed40ff2d3693bce137c5061e7
Author: huangstts <huangstts@126.com>
Date:   Mon Aug 19 18:48:42 2019 +0800

    readme


[root@huangstts pcode]# git reset --hard HEAD
HEAD 现在位于 ae7c9df test.txt
[root@huangstts pcode]# git reset --hard HEAD^
HEAD 现在位于 11433fb 123
[root@huangstts pcode]# git reset --hard HEAD^
HEAD 现在位于 1f1325f readme


[root@huangstts pcode]# git reset --hard ae7c
HEAD 现在位于 ae7c9df test.txt
[root@huangstts pcode]# ls
newdir  readme.txt  test.txt
[root@huangstts pcode]# git reset --hard HEAD^
HEAD 现在位于 11433fb 123
[root@huangstts pcode]# ls
newdir  readme.txt
[root@huangstts pcode]# git reset --hard HEAD^
HEAD 现在位于 1f1325f readme
[root@huangstts pcode]# ls
newdir  readme.txt
[root@huangstts pcode]# cat readme.txt 
readme

reflog 记录每一次的变化,版本号都会记录
[root@huangstts pcode]# git reflog 
1f1325f HEAD@{0}: reset: moving to HEAD^
11433fb HEAD@{1}: reset: moving to HEAD^
ae7c9df HEAD@{2}: reset: moving to ae7c
1f1325f HEAD@{3}: reset: moving to HEAD^
11433fb HEAD@{4}: reset: moving to HEAD^
ae7c9df HEAD@{5}: commit: test.txt
11433fb HEAD@{6}: commit: 123
1f1325f HEAD@{7}: commit (initial): readme



------删除-----
创建一个useless.txt文件,将其更新到暂存区,删除源文件,查看git暂存区状态
[root@huangstts pcode]# touch useless.txt
[root@huangstts pcode]# git add useless.txt
[root@huangstts pcode]# rm -rf useless.txt 
[root@huangstts pcode]# git status 
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#   新文件:    useless.txt
#
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#   删除:      useless.txt
#

这时可将其从暂存区彻底删除,或者从暂存区恢复数据
----恢复
[root@huangstts pcode]# git checkout -- useless.txt
[root@huangstts pcode]# ls
newdir  readme.txt  useless.txt


----从暂存区删除

[root@huangstts pcode]# git rm useless.txt
rm 'useless.txt'
[root@huangstts pcode]# git status 
# 位于分支 master
无文件要提交,干净的工作区



在工作区删除的文件,添加到了暂存区,并且别也交了。此时,你也有两种选择:
----恢复
[root@huangstts pcode]# touch useless.txt
[root@huangstts pcode]# git add useless.txt
[root@huangstts pcode]# git commit -m "useless.txt" useless.txt
[master 122f6cd] useless.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 useless.txt

[root@huangstts pcode]# rm -rf  useless.txt 
[root@huangstts pcode]# ls useless.txt
ls: 无法访问useless.txt: 没有那个文件或目录

[root@huangstts pcode]# git checkout -- useless.txt
[root@huangstts pcode]# ls useless.txt 
useless.txt

----从版本库删除

[root@huangstts pcode]# git rm useless.txt
rm 'useless.txt'
[root@huangstts pcode]# git commit -m "del file useless.txt"
[master 1123783] del file useless.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 useless.txt


**创建分支fenzhi**
[root@huangstts pcode]# git branch fenzhi
[root@huangstts pcode]# git checkout fenzhi
切换到分支 'fenzhi'

**创建分支并进入**
[root@huangstts pcode]# git checkout -b fz
切换到一个新分支 'fz'

查看所在分支
[root@huangstts pcode]# git branch 
  fenzhi
* fz
  master

[root@huangstts pcode]# echo "change on fz" >>readme.txt 
[root@huangstts pcode]# git add .
[root@huangstts pcode]# git commit -m "change on fz" .
[fz d38f3a4] change on fz
 1 file changed, 1 insertion(+)
[root@huangstts pcode]# cat readme.txt 
readme
change on fz

切换回master,之前的修改并没有作用到master上
[root@huangstts pcode]# git checkout master 
切换到分支 'master'
[root@huangstts pcode]# cat readme.txt 
readme

**分支合并**
[root@huangstts pcode]# git merge fz
更新 1123783..d38f3a4
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)
[root@huangstts pcode]# cat readme.txt 
readme
change on fz

**删除分支fz**
[root@huangstts pcode]# git branch -d fz
已删除分支 fz(曾为 d38f3a4)。
[root@huangstts pcode]# git branch 
  fenzhi
* master


**tag标签管理**

[root@huangstts pcode]# git branch 
  fenzhi
  master
* tagtagtag
[root@huangstts pcode]# git tag v1.0
[root@huangstts pcode]# git tag
v1.0

给历史版本打标签
git log 查看历史版本
[root@huangstts pcode]# git log --pretty=oneline --abbrev-commit
d38f3a4 change on fz
1123783 del file useless.txt
122f6cd useless.txt
1f1325f readme
[root@huangstts pcode]# git tag v0.2 1f1325f
[root@huangstts pcode]# git tag
v0.2
v1.0








©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容