(一)本地版本库
以前不觉得代码备份的重要性,因为以前的代码都是可有可无的。而且觉得没了再写就是了,就当作锻炼。相信很多人也曾经有这样的想法。当看了一些牛人的博客后才知道这样的想法是多么白痴。最经典的一句话是不要自己想着造轮子。你可以根据自己的兴趣去学习相关的底层知识,不过是时间允许的情况下。在生产情况下,代码开发人员应该遵循的DRY这个原则(Don't Repeat Yourself)现在想如果当年写的一些快速排序代码什么的可以留下来。一些项目可以留下来。(那时候如果知道Git)现在将会省下多少”造轮子“的时间。随着项目的渐渐壮大。版本管理的重要性越来越显示出来,特别是想git这样的分布式管理工具,不但可以把本地版本库关联到互联网的版本库防止资料丢失。还可以自己搭建服务器实现代码的数据的安全性。
今天我第一天学习git暂时没打算深入学习。只想用些基本功能。我参考的廖雪峰先生的git教程。很快就入门了。(可能我用的是Linux避免了很多Windows下的麻烦配置吧).
我使用的是Fedora采用命令行的方式安装git
yum install git
如果是ubuntu只要把yum 改成apt-get就可以了。
然后
# git config --global user.name "Your Name"
# git config --global user.email "email@example.com"
添加你的名字还有邮箱。这些要具体情况不同。
我要使用源码库管理我的运维学习的一些源代码。首先创建文件夹,并进入目录中 ls -la
[root@localhost python]# mkdir Operations
[root@localhost python]# cd Operations/
[root@localhost Operations]# ls -la
total 8
drwxr-xr-x 2 root root 4096 Dec 30 21:41 .
drwxrwxr-x. 7 lan lan 4096 Dec 30 21:41 ..
[root@localhost Operations]#
大家都看到什么都没有,现在运行命令。
[root@localhost Operations]# git init
Initialized empty Git repository in /home/lan/Documents/python/Operations/.git/
创建这个文件作为源码管理文件夹
然后我把我之前写的代码都搞进去里面。
[root@localhost Operations]# ls
difflib filecmp nmap pycurl scapy test
dnspython IPy pexpect rrdtool smtplib XlsxWriter
目录下有东西了。现在可以通过版本查看
[root@localhost Operations]# git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
IPy/
XlsxWriter/
difflib/
dnspython/
filecmp/
nmap/
pexpect/
pycurl/
rrdtool/
scapy/
smtplib/
test/
比原来多了许多文件夹。接下来。我们将这些改变添加到git中。首先我们先把这些东西加入缓冲区中。
git add *
如果是一个文件可以指定文件。
然后查看。
[root@localhost Operations]# git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: IPy/simple1.py
new file: XlsxWriter/chart.xlsx
new file: XlsxWriter/chart_column.xlsx
new file: XlsxWriter/demo.xlsx
new file: XlsxWriter/simple1.py
new file: XlsxWriter/simple2.py
new file: difflib/lan.html
new file: difflib/simple1.py
new file: difflib/simple2.py
new file: difflib/simple3.py
new file: dnspython/simple1.py
new file: dnspython/simple2.py
new file: dnspython/simple3.py
new file: dnspython/simple4.py
new file: dnspython/simple5.py
new file: filecmp/lan/lanzhiheng
new file: filecmp/lan/lianghaidao
new file: filecmp/lan/linyilong
new file: filecmp/lan/some/lanzhiheng
new file: filecmp/lan/zhangwenfeng
new file: filecmp/simple1.py
new file: filecmp/simple2.py
new file: filecmp/wang/lanzhiheng
new file: filecmp/wang/lianghaidao
new file: filecmp/wang/linyilong
new file: filecmp/wang/some/lanzhiheng
new file: filecmp/wang/zhangwenfeng
new file: nmap/simple.py
new file: pexpect/mylog.txt
new file: pexpect/simple1.py
new file: pexpect/simple2.py
new file: pexpect/simple3.py
new file: pexpect/test1.py
new file: pexpect/test2.py
new file: pexpect/test3.py
new file: pexpect/test4.py
new file: pycurl/content.txt
new file: pycurl/simple1.py
new file: rrdtool/Flow.png
new file: rrdtool/Flow.rrd
new file: rrdtool/create.py
new file: rrdtool/graph.py
new file: rrdtool/update.py
new file: scapy/simple1.py
new file: scapy/test.svg
new file: smtplib/simple1.py
new file: smtplib/simple2.py
new file: smtplib/simple3.py
new file: test/f1
new file: test/f2
new file: test/f3
提示可以提交。可以同时添加很多文件或者文件夹到缓冲区,不过最后需要提交才能记录到版本库里面。
[root@localhost Operations]# git commit -m "operation1"
[master (root-commit) c33aa66] operation1
51 files changed, 3681 insertions(+)
create mode 100644 IPy/simple1.py
create mode 100644 XlsxWriter/chart.xlsx
create mode 100644 XlsxWriter/chart_column.xlsx
create mode 100644 XlsxWriter/demo.xlsx
create mode 100644 XlsxWriter/simple1.py
create mode 100644 XlsxWriter/simple2.py
create mode 100644 difflib/lan.html
create mode 100644 difflib/simple1.py
create mode 100644 difflib/simple2.py
create mode 100644 difflib/simple3.py
create mode 100644 dnspython/simple1.py
create mode 100644 dnspython/simple2.py
create mode 100644 dnspython/simple3.py
create mode 100644 dnspython/simple4.py
create mode 100644 dnspython/simple5.py
create mode 100644 filecmp/lan/lanzhiheng
create mode 100644 filecmp/lan/lianghaidao
create mode 100644 filecmp/lan/linyilong
create mode 100644 filecmp/lan/some/lanzhiheng
create mode 100644 filecmp/lan/zhangwenfeng
create mode 100644 filecmp/simple1.py
create mode 100644 filecmp/simple2.py
create mode 100644 filecmp/wang/lanzhiheng
create mode 100644 filecmp/wang/lianghaidao
create mode 100644 filecmp/wang/linyilong
create mode 100644 filecmp/wang/some/lanzhiheng
create mode 100644 filecmp/wang/zhangwenfeng
create mode 100644 nmap/simple.py
create mode 100644 pexpect/mylog.txt
create mode 100644 pexpect/simple1.py
create mode 100644 pexpect/simple2.py
create mode 100644 pexpect/simple3.py
create mode 100644 pexpect/test1.py
create mode 100644 pexpect/test2.py
create mode 100644 pexpect/test3.py
create mode 100644 pexpect/test4.py
create mode 100644 pycurl/content.txt
create mode 100644 pycurl/simple1.py
create mode 100644 rrdtool/Flow.png
create mode 100644 rrdtool/Flow.rrd
create mode 100644 rrdtool/create.py
create mode 100644 rrdtool/graph.py
create mode 100644 rrdtool/update.py
create mode 100644 scapy/simple1.py
create mode 100644 scapy/test.svg
create mode 100644 smtplib/simple1.py
create mode 100644 smtplib/simple2.py
create mode 100644 smtplib/simple3.py
create mode 100644 test/f1
create mode 100644 test/f2
create mode 100644 test/f3
这样就可以添加到版本库里面了,是不是很简单?
现在可以查看
[root@localhost Operations]# git status
On branch master
nothing to commit, working directory clean
提交成功了。
我们可以看到记录。
commit c33aa667642a55a480cf186b1f668f176bbae330
Author: lan <hengrj@126.com>
Date: Tue Dec 30 21:47:00 2014 +0800
operation1
这里有版本代号我们可以通过
git reset ****
回退到指定的版本代号。
(二)远程版本库
接下来肯定要在网上创个库来管理拉。不过注意都是共有的,如果要私有不让别人看是要钱的。除非是自己的服务器。
去github创建一个帐号,用邮箱。
首先我创建密钥
通过命令。
#ssh-keygen -t rsa -C "hengrj@126.com"
邮箱用你自己的邮箱跟之前的一样。跟github的帐号也一样。
然后一直回车。
[root@localhost Operations]# ssh-keygen -t rsa -C "hengrj@126.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ba:bd:b8:a9:3b:fa:57:af:a1:27:9d:22:e1:89:f4:f1 hengrj@126.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| |
| |
| S |
| . o .. |
| . + =.o.o |
| . * E*+.. |
| .o+**=+o |
+-----------------+
由于我是root用户创建的。所以我的密钥在/root/.ssh/下
分别有下面两个文件。
id_rsa id_rsa.pub
分别代表公共钥还有私钥。
我们把公钥发到github网站中。.pub后缀的就是公钥,来到github页面的setting中的这个页面。
随便用一个名字,把公钥的内容放在里面。然后点击Add Key 如果验证通过就添加成功了。
然后创建一个版本库。跟本地的对应。
右上角的create 那里可以找到。进入下面页面。
添加一个新的版本库跟本地对应。
回到本地文件夹中运行命令。
git remote add orign git@github.com:/lanzhiheng/Operations
关联本地与远程版本库。
然后通过命令。
git push -u origin master
就可以把本地版本库上传到远程。推送后就可以在网页看到本地上传的内容了。
以后可以简单的通过
git push origin master
进行推送版本。。。。
另外如果要在其他地方用到版本库。则直接用命令。
git clone git@github.com:/lanzhiheng/Operations
则可以把远程版本库下载到本地。
现在远程版本库的情况如下。
通过Git节省了很多代码管理方面的时间。真是个好东西。