一、GIT命令行

[root@localhost ~]# git
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path][-p|--paginate|--no-pager] [--no-replace-objects][--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE][--help] COMMAND [ARGS]The most commonly used git commands are:add        Add file contents to the indexbisect     Find by binary search the change that introduced a bugbranch     List, create, or delete branchescheckout   Checkout a branch or paths to the working treeclone      Clone a repository into a new directorycommit     Record changes to the repositorydiff       Show changes between commits, commit and working tree, etcfetch      Download objects and refs from another repositorygrep       Print lines matching a patterninit       Create an empty git repository or reinitialize an existing onelog        Show commit logsmerge      Join two or more development histories togethermv         Move or rename a file, a directory, or a symlinkpull       Fetch from and merge with another repository or a local branchpush       Update remote refs along with associated objectsrebase     Forward-port local commits to the updated upstream headreset      Reset current HEAD to the specified staterm         Remove files from the working tree and from the indexshow       Show various types of objectsstatus     Show the working tree statustag        Create, list, delete or verify a tag object signed with GPGSee 'git help COMMAND' for more information on a specific command.

二、Git创建仓库

(1)创建一个项目

[root@localhost ~]# mkdir public_html
[root@localhost ~]# cd public_html/
[root@localhost public_html]# echo 'My website is alive!' > index.html
[root@localhost public_html]# ls
index.html

(2)执行git init将目录转化为版本库

[root@localhost public_html]# git init
Initialized empty Git repository in /root/public_html/.git/
[root@localhost public_html]# ls -a
.  ..  .git  index.html

在执行完成 git init 命令后,Git 仓库会生成一个 .git 目录,该目录包含了资源的所有元数据,其他的项目目录保持不变(不像 SVN 会在每个子目录生成 .svn 目录,Git 只在仓库的根目录生成 .git 目录)。

(3)将文件添加到版本库

[root@localhost public_html]# git add index.html 

如果目录中有多个文件,使用git add . 命令将当前目录及子目录的文件都添加到版本库中。

(4)查看状态

[root@localhost public_html]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   index.html
#

(5)提交

[root@localhost public_html]# git commit -m "Initial contents of public_html" --author="tong <tong@test.com>"
[master (root-commit) b4e2a14] Initial contents of public_html1 files changed, 1 insertions(+), 0 deletions(-)create mode 100644 index.html

(6)让git在提交时打开编辑器,设置你的GIT_EDITOR环境变量

[root@localhost public_html]# export GIT_EDITOR=vim
[root@localhost public_html]# git status
# On branch master
nothing to commit (working directory clean)

(7)编辑文件再次提交

[root@localhost public_html]# cat index.html
<html>
<body>
My website is alive!
</body>
</html>
[root@localhost public_html]# git commit index.html  这时会进入编辑器
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Explicit paths specified without -i nor -o; assuming --only paths...
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   index.html
#
This is new html!
保存退出
[root@localhost public_html]# git commit index.html
[master 0a30716] This is new html!1 files changed, 4 insertions(+), 0 deletions(-)

(8)查看提交

[root@localhost public_html]# git log
commit 0a3071601cc10777e271a952ead46cffba233e24
Author: tong <tong@test.com>
Date:   Tue Feb 27 11:52:29 2018 +0800This is new html!commit b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
Author: tong <tong@test.com>
Date:   Tue Feb 27 11:45:23 2018 +0800Initial contents of public_html[root@localhost public_html]# git show b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
commit b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
Author: tong <tongxiaoda@anzhi.com>
Date:   Tue Feb 27 11:45:23 2018 +0800Initial contents of public_htmldiff --git a/index.html b/index.html
new file mode 100644
index 0000000..34217e9
--- /dev/null
+++ b/index.html
@@ -0,0 +1 @@
+My website is alive![root@localhost public_html]# git show-branch --more=5
[master] This is new html!
[master^] Initial contents of public_html

(9)查看提交差异

[root@localhost public_html]# git diff 0a3071601cc10777e271a952ead46cffba233e24 b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
diff --git a/index.html b/index.html
index 8638631..34217e9 100644
--- a/index.html
+++ b/index.html
@@ -1,5 +1 @@
-<html>
-<body>My website is alive!
-</body>
-</html>

(10)版本库内文件的删除和重命名

[root@localhost public_html]# ls
index.html  poem.html
[root@localhost public_html]# git rm poem.html
rm 'poem.html'
[root@localhost public_html]# git commit -m "Remove a poem"
[master 19a473c] Remove a poem1 files changed, 0 insertions(+), 4 deletions(-)delete mode 100644 poem.html
[root@localhost public_html]# ls
bar.html  index.html
[root@localhost public_html]# mv bar.html foo.html
[root@localhost public_html]# git rm bar.html
rm 'bar.html'
[root@localhost public_html]# git add foo.html
[root@localhost public_html]# git commit -m "mv bar to foo"
[master aa431d9] mv bar to foo1 files changed, 0 insertions(+), 0 deletions(-)rename bar.html => foo.html (100%)

(11)创建版本库的副版本
这就是不同开发者如何通过Git在相同的文件上从事项目开发,并保持与其他版本库同步。

[root@localhost ~]# git clone public_html my_website
Initialized empty Git repository in /root/my_website/.git/
[root@localhost ~]# ls -lsa public_html my_website
my_website:
total 20
4 drwxr-xr-x  3 root root 4096 Feb 27 13:08 .
4 dr-xr-x---. 8 root root 4096 Feb 27 13:08 ..
4 -rw-r--r--  1 root root   51 Feb 27 13:08 foo.html
4 drwxr-xr-x  8 root root 4096 Feb 27 13:08 .git
4 -rw-r--r--  1 root root   51 Feb 27 13:08 index.htmlpublic_html:
total 20
4 drwxr-xr-x  3 root root 4096 Feb 27 13:05 .
4 dr-xr-x---. 8 root root 4096 Feb 27 13:08 ..
4 -rw-r--r--  1 root root   51 Feb 27 13:04 foo.html
4 drwxr-xr-x  8 root root 4096 Feb 27 13:06 .git
4 -rw-r--r--  1 root root   51 Feb 27 11:50 index.html
[root@localhost ~]# diff -r public_html my_website
Only in public_html/.git: COMMIT_EDITMSG
diff -r public_html/.git/config my_website/.git/config
5a6,11
> [remote "origin"]
>   fetch = +refs/heads/*:refs/remotes/origin/*
>   url = /root/public_html
> [branch "master"]
>   remote = origin
>   merge = refs/heads/master
Binary files public_html/.git/index and my_website/.git/index differ
diff -r public_html/.git/logs/HEAD my_website/.git/logs/HEAD
1,6c1
< 0000000000000000000000000000000000000000 b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d tong <tong@test.com> 1519703123 +0800 commit (initial): Initial contents of public_html
< b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d 0a3071601cc10777e271a952ead46cffba233e24 tong <tong@test.com> 1519703549 +0800 commit: This is new html!
< 0a3071601cc10777e271a952ead46cffba233e24 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 tong <tong@test.com> 1519703956 +0800 commit: add poem.html
< 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 19a473c2e935efa59b8edea19d2d12be96987a97 tong <tong@test.com> 1519704003 +0800 commit: Remove a poem
< 19a473c2e935efa59b8edea19d2d12be96987a97 1ed9a862e62bd5513021838cb435cf8170e2173d tong <tong@test.com> 1519707877 +0800 commit: new bar
< 1ed9a862e62bd5513021838cb435cf8170e2173d aa431d938e85445f6c22c7389a37349f587d5b01 tong <tong@test.com> 1519707981 +0800 commit: mv bar to foo
---
> 0000000000000000000000000000000000000000 aa431d938e85445f6c22c7389a37349f587d5b01 tong <tong@test.com> 1519708133 +0800 clone: from /root/public_html
diff -r public_html/.git/logs/refs/heads/master my_website/.git/logs/refs/heads/master
1,6c1
< 0000000000000000000000000000000000000000 b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d tong <tong@test.com> 1519703123 +0800 commit (initial): Initial contents of public_html
< b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d 0a3071601cc10777e271a952ead46cffba233e24 tong <tong@test.com> 1519703549 +0800 commit: This is new html!
< 0a3071601cc10777e271a952ead46cffba233e24 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 tong <tong@test.com> 1519703956 +0800 commit: add poem.html
< 5be473e1ed6d04ed9e96b7fa3e9e2860607cbd31 19a473c2e935efa59b8edea19d2d12be96987a97 tong <tong@test.com> 1519704003 +0800 commit: Remove a poem
< 19a473c2e935efa59b8edea19d2d12be96987a97 1ed9a862e62bd5513021838cb435cf8170e2173d tong <tong@test.com> 1519707877 +0800 commit: new bar
< 1ed9a862e62bd5513021838cb435cf8170e2173d aa431d938e85445f6c22c7389a37349f587d5b01 tong <tong@test.com> 1519707981 +0800 commit: mv bar to foo
---
> 0000000000000000000000000000000000000000 aa431d938e85445f6c22c7389a37349f587d5b01 tong <tong@test.com> 1519708133 +0800 clone: from /root/public_html
Only in my_website/.git: packed-refs
Only in my_website/.git/refs: remotes

转载于:https://www.cnblogs.com/tongxiaoda/p/8477911.html

GIT使用—创建一个版本库相关推荐

  1. Git教程——如何创建一个版本库(Repository)

    文章目录 1.创建版本库 (init) 2.添加文件管理 (add) 3.提交改变 (commit) 4.流程图 1.创建版本库 (init) 我们先要确定要把哪个文件夹里的文件进行管理. 比如说我桌 ...

  2. Git版本控制管理——远程版本库

    之前提到的Git的所有操作都是在本地完成的,而实际项目开发并不是一个人就可以搞定的,通常需要团队的协作,而这些协作可能又不是在同一个地区的,这就涉及到Git的分布式特性了. Git的分布式特定会涉及到 ...

  3. git clone从远程主机克隆一个版本库

    远程操作的第一步,通常是从远程主机克隆一个版本库,这时就要用到git clone命令. $ git clone <版本库的网址> 比如,克隆jQuery的版本库. $ git clone ...

  4. Git快速入门-git stash 暂存变更,git reset 撤销commit,git revert 回退远程版本库

    Git快速入门系列文章 - Git快速入门-安装配置篇 - Git快速入门-常用命令之独奏篇 - Git快速入门-常用命令之交响乐篇 - Git快速入门-git stash 暂存变更,git rese ...

  5. Git汇总--对象及版本库存储

    下述内容为团队内部分享整理所得,实用性较强,整体性偏差! PS:关于完整的Git内容,请参照之前发表过一系列文章,详见:Git Pro深入浅出(一).Git Pro深入浅出(二).Git Pro深入浅 ...

  6. git 回退上一个版本

    git 回退上一个版本 在 Git 中,HEAD 指针指向的是当前版本,也就是最新的 commit id,上一个版本是 HEAD^,上上一个版本就是 HEAD^^,上50个版本可以写成 HEAD~50 ...

  7. c语言标准函数库怎么建立教程,C语言入门教程-创建一个函数库

    描述 创建一个函数库 上述程序中的rand和bubble_sort函数很实用,很可能在您写其他程序时也能派上用场.为了能更方便地重复使用,您可以为它们创建一个实用工具函数库. 所有的函数库都包括两部分 ...

  8. 从另一个分支在Git中创建一个分支

    我有两个分支: master和dev 我想从dev分支创建一个"功能分支". 目前在分支机构dev上,我执行以下操作: $ git checkout -b myfeature de ...

  9. 优雅的创建一个JavaScript库

    这篇文章的目的是通过演示一个简单的例子来介绍在JS中实例化和定义一个库的正确方法,以优化他人编写或维护自己的JS库. 在我们深入之前,我做了两点假设: 你知道简单的JavaScript或C语言. 你不 ...

最新文章

  1. 计算机基础知识_2020年河北省高职单招计算机基础知识和实践技能培训
  2. 玩转Mixly – 8、Arduino AVR编程 之 变量
  3. 搭建SpringMVC+Hibernate4+Spring3+Ajax+Maven项目(二)
  4. clickhouse hadoop_大数据分析之解决Hadoop的短板,实时大数据分析引擎ClickHouse解析...
  5. 钢笔墨水能否代替打印机墨水_LAMY钢笔应该如何选择墨水?
  6. android插件化-获取apkplug框架已安装插件-03
  7. oracle 11g表空间之最大最小
  8. junit 生成html报告,gradle – 如何为JUnit 5测试创建HTML报告?
  9. 第三方登录 steam_如何在Steam中激活第三方游戏代码
  10. java校园自行车租赁买卖系统ssm
  11. 部分古钱知识--(4)
  12. [高数][高昆轮][高等数学上][第二章-导数与微分]02.函数的求导法则
  13. Is the influences futural AI bring to software engineers are that scary?
  14. 软件设计师(软考中级)考试大纲
  15. 你想要的Window、Office办公快捷键都在这
  16. vue 使用百度地图(全景图)
  17. Swagger2 总结
  18. 渗压计的使用安装方法
  19. 曾国藩:做人守拙,做事守缺
  20. Hcash:见证量子计算和后量子密码的“矛盾较量”

热门文章

  1. EAI激光雷达X4使用gmapping与laser_scan_matcher建图(二)
  2. 《因你而存在的故事》
  3. Android layer type与WebView白屏
  4. 为什么我的iPhone iOS 15少了一些新功能
  5. 永磁电机参数的测量获取(电感、电阻、极对数、磁链常数)
  6. 自己做的一个炸碉堡的小游戏(SWING)
  7. solidworks重建模型好慢_这几个设置可以提高你的SolidWorks模型打开速度,快尝试一下...
  8. 黑马程序员 交通灯管理系统
  9. biz 域名是什么样的?biz 域名的价值高吗?
  10. /cygdrive/c/MinGW/bin/autoconf-2.68: line 501: /mingw/bin/autom4te-2.68: No such file or directory