网站开发技术

记点笔记、 学点技术 欢迎交流建站技术。本站关注lamp技术

您尚未登录。

#1 2014-10-29 11:47:06

admin
管理员

git 使用入门

首先是安装, 这里在linux环境下,直接用 yum install git 进行安装

windows 环境的安装可以 参考百度经验 

http://jingyan.baidu.com/album/48b37f8d6854ef1a65648851.html


文件夹和代码仓库

代码仓库是git中的一个重要的概念。 通常我们程序都在一个文件夹里, 在git中是再一个代码仓库中。

文件夹和代码仓库很类似, 只是多了一些东西(一个名字是 .git 的文件夹)。 这个文件夹的信息是非常重要,版本控制的所有信息都在里面。 不过我们一般也不用理会之类的什么东西。有git自动管理。


一秒钟把一个文件夹变成一个代码仓库。

[ipbbs.net@hk ~]$ cd ~
[ipbbs.net@hk ~]$ mkdir test
[ipbbs.net@hk ~]$ cd test
[ipbbs.net@hk test]$ git init
Initialized empty Git repository in /home/ipbbs.net/test/.git/

 安装git后, 建立一个文件夹、 进入这个文件 后 git init 

 可以查看当前的目录了多了一些什么 

[ipbbs.net@hk test]$ ls -al
total 12
drwxrwxr-x 3 ipbbs.net ipbbs.net 4096 Oct 29 11:36 .
drwx------ 3 ipbbs.net ipbbs.net 4096 Oct 29 11:36 ..
drwxrwxr-x 7 ipbbs.net ipbbs.net 4096 Oct 29 11:36 .git
[ipbbs.net@hk test]$

正是一个.git的目录


 填写用户名和邮箱。 

[ipbbs.net@hk test]$ git config --global user.name 'ipbbs'
[ipbbs.net@hk test]$ git config --global user.email 'user#ipbbs.net'



ipbbs.net

离线

#2 2014-10-29 12:04:05

admin
管理员

Re: git 使用入门

现在可以正是的工作。 

git 是一个分布式的版本控制的工具。 通过一系列的小功能来学习git.


第一: 提交一个代码 , 把代码提交到仓库里

代码的提交三个步骤, 1. 创建一个文件 2. 添加跟踪(意思是可以知道文件的状态), 3. 提交 3个步骤就完成了全部的操作。

先提交一个 test.txt 文件

[ipbbs.net@hk test]$ echo 1111 > test.txt                # 创建一个文件
[ipbbs.net@hk test]$ git add test.txt                    # 跟踪这个文件
[ipbbs.net@hk test]$ git commit -m '第一次提交'          # 提交
[master (root-commit) 017cb8c] 第一次提交
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

这里的详细步骤可以通过 git status 开查看。 现在重复上面的过程, 用一个文件名是 test2.txt , 查看详细的过程

首相先  git status 

[ipbbs.net@hk test]$ git status
# On branch master
nothing to commit (working directory clean)

这是说工作目录是干净的, 没有任何提交。 这是初始的状态

[ipbbs.net@hk test]$ echo aaa > test2.txt
[ipbbs.net@hk test]$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       test2.txt
nothing added to commit but untracked files present (use "git add" to track)

这里创建了一个文件 test2.txt , 其他的什么都没有操作

这里提示的 git 已经发现了一个新的文件, 并且是没有添加跟踪 , 并提示可以用 git add 对这个文件进行跟踪。现在跟踪一下这个文件。

[ipbbs.net@hk test]$ git add test2.txt
[ipbbs.net@hk test]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   test2.txt
#
[ipbbs.net@hk test]$

这里可以看到状态已经改变, 可以进行提交了。 进行提交 

[ipbbs.net@hk test]$ git commit -m '测试2 test2.txt'
[master 985af02] 测试2 test2.txt
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test2.txt
[ipbbs.net@hk test]$ git status
# On branch master
nothing to commit (working directory clean)
[ipbbs.net@hk test]$

 可以看到提交后, 代码仓库现在又是干净的了, 说明已经成功提交了一个文件。


ipbbs.net

离线

#3 2014-10-29 12:27:29

admin
管理员

Re: git 使用入门

第二: 从仓库了删除一个文件, 首先直接删除一个文件,看看发生了什么

[ipbbs.net@hk test]$ rm test2.txt
[ipbbs.net@hk test]$ git stattus
fatal: cannot exec 'git-stattus': Permission denied
[ipbbs.net@hk test]$ git status
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    test2.txt

 git 已经提示 test2.txt 删除了  , 但是并没有

[ipbbs.net@hk test]$ git status -s
 D test2.txt
[ipbbs.net@hk test]$ git ls-files
test.txt
test2.txt

通过查看git ls-files 查看 , 文件tes2.txt 还是存在的。并没有从git删除。

如果要彻底的删除 

[ipbbs.net@hk test]$ git rm test2.txt
rm 'test2.txt'
[ipbbs.net@hk test]$ git ls-files
test.txt

这样就从git中删除了,但是并不是彻底删除了, 因为还可以找回来。


ipbbs.net

离线

#4 2014-10-29 12:39:48

admin
管理员

Re: git 使用入门

被删除文件的恢复

现在觉得 test2.txt 文件还是有用的。 如何进行恢复呢?

[ipbbs.net@hk test]$ git reflog
017cb8c HEAD@{0}: HEAD^: updating HEAD
985af02 HEAD@{1}: commit: 测试2 test2.txt
[ipbbs.net@hk test]$
[ipbbs.net@hk test]$ git reset --hard  985af02
HEAD is now at 985af02 测试2 test2.txt
[ipbbs.net@hk test]$ ls
test2.txt  test.txt

 通过 git reflog 查看以前的操作记录, 通过最前面的标识码就可以恢复到以前的状态


ipbbs.net

离线

页脚

Powered by FluxBB