赞
踩
作为一名程序猿,可能大家都遇到过这种需求:
- 自己在业余时,出于某些目的(比如,学习、私活 ;) 等)写了一些代码,想进行版本控制,以备将来不时之需。
- 自己的一些资料,可能需要时常更新,但又想保留历史版本(比如我自己,就有一份专门记录所有常用密码的txt文件)
对于这种需求,我们自然而然的会想到一些 VCS (Version Control System)来实现。
就我自己的实践而言,之前在使用 SVN 时,这点很好实现。大体步骤有以下几步(以 win 系统为例说明):
下载 TortoiseSVN.exe (我的是 1.9.3 版本,如下图),并进行安装
新建一个文件夹,如 test (假设 全路径为: E:\test), 作为仓库
在 test 文件夹中右键,选择 Create repository here 菜单,如下图
在其他想导出这个仓库的地方,新建一个文件夹 123 (假设全路径为 E:\123)。在 123 文件夹中右键,选择 SVN Checkout 菜单,如下图
在弹出的对话框中,填入以下内容
然后,就可以在 123 这个文件夹中,新建&修改&删除 文件,并提交至 svn 进行版本控制了啊。下图为新建文件的示例。
以上就是 svn 的使用方式。
下面,说一下,目前使用很广泛的 git 如何实现这一需求(以 CentOS 7 为例进行说明)。
确认一下 git 的版本
> git --version
git version 1.8.3.1
创建仓库地址(假设全路径为 /tmp/git_repo)
> mkdir /tmp/git_repo
初始化仓库
> cd /tmp/git_repo
> git init --bare
初始化空的 Git 版本库于 /tmp/git_repo
在想要导出仓库内容的地方,新建一个文件夹(假设全路径为 /tmp/123)
> mkdir /tmp/123
导出仓库
> cd /tmp/123
> git clone /tmp/git_repo/ ./
正克隆到 'git_repo'...
warning: 您似乎克隆了一个空版本库。
完成。
现在就可以往 git 仓库里 新建&修改&删除 文件了。下面以 新建文件进行说明。
> touch readme.md
> git status
# 位于分支 master
#
# 初始提交
#
# 未跟踪的文件:
# (使用 "git add <file>..." 以包含要提交的内容)
#
# readme.md
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
> git add .
> git commit -m "add readme.md"
[master(根提交) ad3a9d5] add readme.md
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.md
> git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 210 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /tmp/git_repo/
* [new branch] master -> master

这个时候,我们切回到 /tmp/git_repo 看下 log
PS: 其实在 /tmp/123 中看 log 也是一样的
> cd /tmp/git_repo
> git log
commit ad3a9d5d77682dbfb846c605d609c9b5b07b9ab8
Author: xxxxxxx <yyyyyyyy@123.com>
Date: Tue Feb 14 17:08:48 2017 +0800
add readme.md
以上就是使用 git 来实现这一需求的过程。
最后,说一下,在使用 git 来实现的时候,有一个关键点,需要注意一下。那就是,当我在在初始 git仓库 时,使用的命令:
git init --bare
当中的 –bare 这个参数千万不能少。否则,当你在 123 中往 git_repo中提交的时候,你会得到以下的错误提示:
> git push origin master
Counting objects: 29, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (28/28), 4.63 KiB | 0 bytes/s, done.
Total 28 (delta 3), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To /tmp/git_repo/
! [remote rejected] master -> master (branch is currently checked out)
error: 无法推送一些引用到 '/tmp/git_repo/'

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。