Chinaunix首页 | 论坛 | 博客
  • 博客访问: 827282
  • 博文数量: 143
  • 博客积分: 455
  • 博客等级: 一等列兵
  • 技术积分: 861
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-03 00:11
文章分类

全部博文(143)

文章存档

2018年(10)

2017年(6)

2016年(28)

2015年(14)

2014年(67)

2013年(1)

2012年(17)

我的朋友

分类: LINUX

2014-07-23 16:17:06

常用命令:

Linus为Linux Kernel Project发起的版本控制项目。
HEAD代表当前最新状态。
tag为某个状态的标签。
SHA1为每个提交日志的唯一标识。

install:

apt-get install git-core


git clone:

git仓库可以使用git clone获得:
git clone git://git.longene.org/unifiedkernel.git

也可以通过浏览器浏览。


通过git pull更新仓库,使用git init-db初始化自己的仓库。


config:

开发人员需要为git仓库配置相关信息,这样在提交代码时,这些信息会自动
反映在git仓库的日志中。

git config user.name "your name"
git config user.email yourname@insigma.com.cn
git config core.editor vim
git config core.paper "less -N"
git config color.diff true
git config alias.co checkout

git config alias表示,可以用git co代表git checkout。git var -l可以查看
已经设置的配置。


diff:

开发人员在本地进行开发后,可以使用git diff查看改动。
除了直接比较当前开发后的改动外,git diff还可以:

git diff tag 比较tag和HEAD之间的不同。
git diff tag file 比较一个文件在两者之间的不同。
git diff tag1..tag2 比较两个tag之间的不同。
git diff SHA11..SHA12 比较两个提交之间的不同。
git diff tag1 tag2 file or
git diff tag1:file tag2:file 比较一个文件在两个tag之间的不同。


ORIG_HEAD用于指向前一个操作状态,因此在git pull之后如果想得到pull的
内容就可以:

git diff ORIG_HEAD

git diff --stat 用于生成统计信息。
git diff --stat ORIG_HEAD


apply:

git apply相当于patch命令。
--check 检查能否正常打上补丁,-v verbose模式, -R reverse模式,反打补丁。


log:

git log file 查看一个文件的改动。
git log -p 查看日志和改动。
git log tag1..tag2 查看两个tag之间的日志。
git log -p tag1..tag2 file 查看一个文件在两个tag之间的不同。
git log tag.. 查看tag和HEAD之间的不同。


commit:

git commit -a -e 提交全部修改文件,并调用vim编辑提交日志。
git reset HEAD^ or
git reset HEAD~1 撤销最后一次提交。
git reset --hard HEAD^ 撤销最后一次提交并清除本地修改。
git reset SHA1 回到SHA1对应的提交状态。


add/delete/ls:

git add -a 添加所有文件。
git rm file 从git仓库中删除文件。
git commit 添加或是删除后要提交。

git ls-files -m 显示修改过的文件。
git ls-files 显示所有仓库中的文件。

git中有四种对象:blob、tree、commit、tag。
blob代表文件,tree代表目录,commit代表提交历史,tag代表标签。
这四种对象都是由SHA1值表示的。在仓库的.git目录中保存了git管理仓库
所需要的全部信息。

git ls-tree HEAD file 显示file在HEAD中的SHA1值。
git cat-file -t SHA1 显示一个SHA1的类型。
git cat-file type SHA1 显示一个SHA1的内容。type是blob、tree、commit、tag之一。

patch:

git format-patch -1 生成最后一个提交对应的patch文件。
git am < patch 把一个patch文件加入git仓库中。
git am --resolved 如果有冲突,在解决冲突后执行。
git am --skip 放弃当前git am所引入的patch。


conflict:

git merge 用于合并两个分支。
git diff 如果有冲突,直接使用diff查看,
冲突代码用<<<和>>>表示。手动修改冲突代码。
git update-index 更新修改后的文件状态。
git commit -a -e 提交为解决冲突而修改的代码。


branch:

git branch -a 查看所有分支。
git branch new_branch 创建新的分支。
git branch -d branch 删除分支。
git checkout branch 切换当前分支。-f参数可以覆盖未提交内容。


daemon:

有时更新公共代码仓库使用patch的方式,或者直接
用git pull git://ip/repo branch的方式更新每个人的代码。使用git pull的方式需要
提交代码的机器运行:
git daemon --verbose --export-all --enable=receive-pack --base-path=/repo


request-pull:


git request-pull start url 用于产生本次pull请求的统计信息。


clean:


git clean -dxf 用于清除未跟踪文件。
git clean -dnf 可以显示需要删除的文件,但不包括被.gitignore忽略的。
git reset --hard HEAD 用于清除跟踪文件的修改。

安装:

git,分布式版本控制系统,作者:Linus Benedict Torvalds。

  1.下载和安装git
  下载地址:

  或者wget获取:wget

  解压后切换到其目录:
  #tar zxvf git-1.7.6.tar.bz2
  #cd git-1.7.6

  使用默认配置进行安装,如果想修改配置,可以使用./configure --help来获取帮助。
  #./configure    配置
  #make           编译
  #make install   安装

  2.初始化配置
  git默认安装在/usr/local/bin,安装之后可以验证一下是否装好了。
  #whereis git
   git: /usr/bin/git /usr/share/man/man1/git.1.gz
  #git --version
  git version 1.7.6
  $git --help

  首先需要指定用户名和电子邮箱地址:
  # git config --global user.name "sasa"
  # git config --global user.email
  再验证一下配置信息:
  # git config --list
  user.name=sasa
  
  core.repositoryformatversion=0
  core.filemode=true
  core.bare=false
  core.logallrefupdates=true
   这些配置是存放在个人注目礼下的.gitconfig文件中的,可以查看#vi ~/.gitconfig

[user]
        name = sasa
        email = sasa@manyi.cc

 

 

  3.建立工程。
  本地存储的任何一个目录都可以建立git工程,如果已有工程位于/home/sasa/gitwork目录,就可以把这个目录定义为git工程。
  # cd /home/sasa/gitwork
  # git init    (注意:要切换到gitwork目录下,才能对该目录进行提交,初始化的也是针对该目录)
  Reinitialized existing Git repository in /home/sasa/gitwork/.git/
  这样就建立了一个名为.git的文件夹,这就是git用来存储信息和跟踪改动的文件夹。
  查看该文件夹下所有的文件:
  ## ls -altr .git 

复制代码
总用量 52 drwxr-xr-x. 4 root root 4096 3月 7 11:28 refs
drwxr-xr-x. 2 root root 4096 3月 7 11:28 branches -rw-r--r--. 1 root root 73 3月 7 11:28 description
drwxr-xr-x. 2 root root 4096 3月 7 11:28 info drwxr-xr-x. 2 root root 4096 3月 7 11:28 hooks -rw-r--r--. 1 root root 23 3月 7 11:28 HEAD -rw-r--r--. 1 root root 112 3月 7 11:48 index -rw-r--r--. 1 root root 303 3月 7 11:48 COMMIT_EDITMSG
drwxr-xr-x. 7 root root 4096 3月 7 11:49 objects
drwxr-xr-x. 3 root root 4096 3月 7 11:49 logs
drwxr-xr-x. 3 root root 4096 3月 7 11:51 .. -rw-r--r--. 1 root root 92 3月 7 12:41 config
drwxr-xr-x. 8 root root 4096 3月 7 12:41 .
复制代码

 

 

  4.向工程添加和提交文件。
  (目前gitwork目录下仅有文件msg.php)
  # git add msg.php
  # git commit

[master 306e67d] This is the first update. 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 msg.php

 

 

  5.更改文件和提交改动
  编辑文件、添加或者删除了一些字段:
  #vi msg.php
  查看和git仓库中的文件相比有了哪些改动:
  #git diff

复制代码
diff --git a/mynote.php b/mynote.php
deleted file mode 100644 index 6edf9e3..0000000 --- a/mynote.php +++ /dev/null @@ -1,2 +0,0 @@ -
-iii   hello ,this is my note work!
复制代码

 

  注:如果要提交,需要先确保文件添加到了临时区域(staging area),然后才能提交,提交时会自动打开系统的默认编辑器,用户添加一些注释后保存并退出编辑器的时候,这些注释就同时提交到仓库中去了。

  # git add msg.php
  # git commit

[master 9090cb1] This is the second change. ---->这里就是备注 1 files changed, 1 insertions(+), 1 deletions(-)

 

 

  查看状态:
  #git status

复制代码
# On branch master
# Changed but not updated:
#   (use "git add/rm ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#       deleted:    mynote.php
#
no changes added to commit (use "git add" and/or "git commit -a")
复制代码

 

  查看文件历史和以往的注释:
  # git log msg.php

复制代码
commit 347aee4f50a3e82a3e664fd5f6726b35d678788b
Author: sasa cc> Date:   Thu Mar 7 14:39:19 2013 +0800 This is the third change.

commit 9090cb164605d5945cadb4a0235cb25322277e96
Author: sasa cc> Date:   Thu Mar 7 14:36:21 2013 +0800 This is the second change.

commit e47644aaaf4d4159c16743668dbc53e790962a6f
Author: sasa cc> Date:   Thu Mar 7 14:34:59 2013 +0800 This is the first change.

commit 306e67dab039edc364e595f9089e67a8a4519dfd
Author: sasa cc> Date:   Thu Mar 7 12:47:59 2013 +0800 This is the first update.
复制代码

 

 

Git在windows下使用方法:
  Git是一个免费的、开源的版本控制软件。在Windows上安装git,一般为msysgit,官方下载地址为

(1)下载:在官方网站下载最新版本。

2 安装:点击下一步,直到出现步骤三。

3 在Windows Explorer integration选项中将“Git Bash here”和“Git GUI here”打对勾。

4 在“Adjusting your PATH environment”选项中,默认即可。

5 在“Configuring the line ending conversions”选项中, 第一个选项:如果是跨平台项目,在windows系统安装,选择; 第二个选项:如果是跨平台项目,在Unix系统安装,选择; 第三个选项:非跨平台项目,选择。

6 选择“Finish”,结束安装。

7 在桌面上,可以看到安装后的“Git Bash”选项,双击打开,可以进行操作了。

生成ssh公钥
(1)启动Git Bash:
(2)在程序中依次输入:配置email,命令如下:

$git config --global user.name "your name" $git config --global user.email 

""其中"your name"换成你的名字,""换成你的邮箱地址。
(3)生成公钥,用于身份认证:

ssh-keygen.exe -C "your@email.address" -t rsa

这样就可以在你的用户下取得需要的公钥匙,公钥位于你的电脑中“C:\Documents and Settings\Airen\.ssh”的“id_rsa.pub”文件下;
此时只要把你的公钥放进你项目的服务中就可以进行git的相关操作。

解决中文目录支持问题:
1、ls不能显示中文目录解决办法:
      在git/etc/git-completion.bash中增加一行:alias ls='ls --show-control-chars --color=auto'
2、git commit不能提交中文注释解决办法:
       修改git/etc/inputrc中对应的行:set output-meta on  set convert-meta off
3、git log无法显示中文注释解决办法:
      在git/etc/profile中增加一行:export LESSCHARSET=iso8859.

 

在外网服务器上搭建GIT服务器:

克隆:
$ git    (内网用户)
$ git clone  ssh:/sasa@210.22.155.236:9092/home/sasa/git1   (外网用户)

问题:clone、pull都没问题,但是push的时候出问题,错误提示:remote: error: To squelch this message and still keep the default behaviour....

解决办法:

这是由于git默认拒绝了push操作,需要进行设置,修改.git/config文件后面添加如下代码:

[receive]

 denyCurrentBranch = ignore

转载自:http://www.cnblogs.com/thinksasa/archive/2013/03/07/2948016.html

阅读(1030) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~