GIT使用—创建一个版本库
一、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 index
bisect Find by binary search the change that introduced a bug
branch List, create, or delete branches
checkout Checkout a branch or paths to the working tree
clone Clone a repository into a new directory
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
fetch Download objects and refs from another repository
grep Print lines matching a pattern
init Create an empty git repository or reinitialize an existing one
log Show commit logs
merge Join two or more development histories together
mv Move or rename a file, a directory, or a symlink
pull Fetch from and merge with another repository or a local branch
push Update remote refs along with associated objects
rebase Forward-port local commits to the updated upstream head
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
show Show various types of objects
status Show the working tree status
tag Create, list, delete or verify a tag object signed with GPG
See '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_html
1 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 +0800
This is new html!
commit b4e2a14de84d29ea8890a2e19f039eb08bc2fc7d
Author: tong <tong@test.com>
Date: Tue Feb 27 11:45:23 2018 +0800
Initial 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 +0800
Initial contents of public_html
diff --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 poem
1 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 foo
1 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.html
public_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
GIT使用—创建一个版本库的更多相关文章
- 安装git,创建本地版本库
安装 由于我使用的是Ubuntu,因此安装很简单,输入:sudo apt-get install git 如果是其他Linux版本,可以直接通过源码安装.先从Git官网下载源码,然后解压,依次输入:. ...
- 创建一个版本库,把文件夹用Git管理起来
创建一个文件夹,把这个文件夹用Git管理起来,那么这个文件夹的改变都可以被Git跟踪到,当然也可以将Git中的文件还原到某一个时刻. 首先创建一个空的目录,然后将空的目录由Git来管理 1.建立一个文 ...
- git 创建远程版本库(亲测有效)
一.github远程版本库 1.创建SSH Key(windows) ssh-keygen -t rsa -C "youremail@example.com" 2.连接版本 ...
- 『现学现忘』Git基础 — 8、Git创建本地版本库
目录 1.Git版本库介绍 2.创建本地版本库 场景一:创建一个空的本地版本库. 场景二:项目中已存在文件时,创建该项目的本地版本库. 场景三:在GitHub网站上创建仓库,克隆到本地. 1.Git版 ...
- git学习2:版本库
创建版本库 版本库,又称仓库,英文名为repository,版本库内的所有文件都可以被Git管理起来,即每个文件的修改.删除,Git都能跟踪. 1,在目录中创建版本库 在目录中有两种创建版本库的方法, ...
- git怎么创建本地版本仓库
git怎么创建本地版本仓库 安装git我就不用说了吧!下载地址:https://github.com/msysgit/msysgit/releases/download/Git-1.9.4-previ ...
- Git的使用(1) —— 版本库
1. 简介 Git作为一个分布式版本控制系统,其优点是不需要一直连接远端版本库就可以使用. 故其为实现分布版本控制专门设计了一整套的存储区间和语句,用来实现. (1) 本地版本库:建立在本机磁盘上的文 ...
- linux服务器上创建svn版本库
1. 创建存放各个svn版本库的目录svnrepos(自己定义路径) -bash: cd /usr/local/apache/htdocs/ -bash: mkdir svnrepos 2. 假设我要 ...
- linux 创建svn版本库,并在svn上配置checkstyle做代码风格检查
一.创建SVN版本库 1.安装svn服务器 yum install subversion 2.查看版本 svnserve --version 3.建立SVN版本库目录(即你的SVN服务器里面的文件存放 ...
随机推荐
- [转]软件测试- 3 - Mock 和Stub的区别
由于一直没有完全搞明白Mock和Stub的区别,所以查了很多文章,而这一篇是做好的: http://yuan.iteye.com/blog/470418 尤其是8楼,Frostred的发言,描述地相当 ...
- FreeRTOS系列第17篇---FreeRTOS队列
本文介绍队列的基本知识,具体源代码分析见<FreeRTOS高级篇5---FreeRTOS队列分析> 1.FreeRTOS队列 队列是基本的任务间通讯方式.能够在任务与任务间.中断和任务间传 ...
- 修改Chem 3D模型的化学键属性的方法有哪些
很多的用户在绘制化学图形过程中发现很多的图形都是立体结构的,这个时候就需要用Chem3D,它是ChemOffice的核心组件之一,在绘制立体模型和计算化学数据方面具有不可替代的作用.虽然ChemDra ...
- select标签设置只读的方法(下拉框不可选但可传值)
1. <select id="s1" name="s1" onfocus="this.defaultIndex=this.selectedInd ...
- VMware虚拟机Host-Only(仅主机模式)
转载于:https://www.linuxidc.com/Linux/2016-09/135521p3.htm 三.Host-Only(仅主机模式) Host-Only模式其实就是NAT模式去除了虚拟 ...
- [LintCode] 带重复元素的排列
递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of unique pe ...
- oracle 实现多字段匹配一个关键字查询语句
oracle 实现多字段匹配一个关键字查询语句:有两种方法(经测试,10g中不能用,11g才行): 第一种. select * from table where ('字段名1' ||'字段名2' || ...
- grunt的简单应用
grunt是干什么的呢,一句话:自动化.对于需要反复重复的任务,例如压缩(minification).编译.单元测试.linting等,自动化工具可以减轻你的劳动,简化你的工作.当你在 Gruntfi ...
- 【转】va_list 详解
原文出自http://www.cppblog.com/xmoss/archive/2009/07/20/90680.html VA_LIST 是在C语言中解决变参问题的一组宏 他有这么几个成员: 1) ...
- ubuntu14.04 编译安装CPU版caffe
本文,试图中一个干净的ubuntu14.04机器上安装caffe的cpu版本. http://blog.csdn.net/sinat_35188997/article/details/735304 ...