在rhel5.8中安装git


1.从网上获取git的最新版本,我找的是git-1.7.9.4.tar.gz

2. #  ./configure –prefix=/usr/local/git

3. #  make   

4. #  make install

5.

最后别忘了,设置PATH(除非你喜欢用绝对路径执行git)

vi /etc/profile 添加:export PATH=/usr/local/git/bin:/usr/local/git/libexec/git-core:$PATH

vi ~/.bashrc 添加:export PATH=/usr/local/git/bin:/usr/local/git/libexec/git-core:$PATH

(因为bin目录只有4个命令,其它的几十个命令在libexec/git-core目录下,所在,在PATH搜索路径下,也要加上才能找到)

新建一个项目后一定要写name和email,方便辨认

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"


在客户端(我用的是win7)上取时使用命令

$ git clone ssh://user@IP[:port]/path/to/repo.git/ 

提示:

bash: git-upload-pack: command not found
fatal: The remote end hung up unexpectedly


翻了一下google,发现是服务器端的git安装路径不是默认路径导致的


要写对方的git-upload-pack目录


改成:

$ git clone --upload-pack /usr/local/git/libexec/git-core/git-upload-pack ssh://user@IP[:port]/

path/to/repo.git/ 

还是提示这个错误,那是为什么呢,原来必须在user这个账号下的.bashrc中加入路径,在.bash_profile下加都没有用。所以用user登录服务器


$vi ~/.bashrc


PATH=/usr/local/git/bin:/usr/local/git/libexec/git-core:$PATH


export PATH


保存,客户端再登录就可以了。



$mkdir testGit #建立仓库目录
$cd testGit #进入仓库目录
$git init #这会在当前的目录下建一个仓库
$git status #查看当前仓库的状态
在这个仓库里添加点内容吧
$ echo “hello Git” > readme.txt #建立一个含有 hello Git 的文本文件
$ git add readme.txt #将readme.txt添加到暂存区中
$ git status #查看当前仓库的状态

好的,文件即然被暂存到暂存区中,我们现在就可以把它提交到仓库里面去:)


$ git commit -m "project init"  #将刚才的修改提交到本地仓库中

现在你执行一下git log 命令就会看到刚才的提交记录

$ git log 
$ git diff #查看仓库里未暂存内容和仓库已提交内容的差异 

$ git push    ###push到默认本地分支跟踪的远程分支
$ git push (remote名称,如果是单独clone下来的仓储为origin) (本地分支):(远程分支)
例如: git push origin mybranch:master 

$ git stash  //暂存临时代码  ,,http://www.cppblog.com/deercoder/archive/2011/11/13/160007.html
$ git merge <分支名称>   //合并分支 
$ git rebase      //合并分支,和merge结果一样,但log不一样,区别见
$ git checkout   //主要功能:
1.The checkout command is used to copy files from the history (or stage) to the working directory
2.optionally switch branches.
$ git branch –r     //查看分支信息



查看branch信息(当然你也可以用git show-branch查看,不过有时并不好用),获得如下branch信息:

origin/android
origin/mesa-es
origin/mesa-es-dri

  此时我们需要的是mesa-es分支的代码,那么此时就要进行checkout了。



$ git checkout -b origin/mesa-es   //切换到远程mesa-es分支上



  你再看你的目录(mesa)下是不是有了代码了?其它的branch同理。

  

  



$ git push ssh://git@dev.lemote.com/rt4ls.git master // 把本地仓库提交到远程仓库的master分支中

$ git remote add origin ssh://git@dev.lemote.com/rt4ls.git
$ git push origin master 

这两个操作是等价的,第二个操作的第一行的意思是添加一个标记,让origin指向ssh://git@dev.lemote.com /rt4ls.git,也就是说你操 作origin的时候,实际上就是在操作ssh://git@dev.lemote.com/rt4ls.git。origin在这里完全可以理解为后者 的别名。

    需要说明的是,默认情况下这条语句等价于提交本地的master仓库到远程仓库,并作为远程的master分支。
    如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的master分支,或者作为另外一个名叫test的分支,那么可以这么做。

$ git push origin test:master         // 提交本地test分支作为远程的master分支
$ git push origin test:test              // 提交本地test分支作为远程的test分支

如果想删除远程的分支呢?类似于上面,如果:左边的分支为空,那么将删除:右边的远程的分支。

$ git push origin :test              // 刚提交到远程的test将被删除,但是本地还会保存的,不用担心

$ git-clone   //克隆远程 
$ git-pull     //获取远程一分支



在使用Git Push代码到数据仓库时,提示如下错误:

[remote rejected] master -> master (branch is currently checked out)
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 git@192.168.1.X:/var/git.server/.../web
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to "git@192.168.1.X:/var/git.server/.../web"

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

    [receive]
    denyCurrentBranch = ignore


无法查看push后的git中文件的原因与解决方法

 

在初始化远程仓库时最好使用 git --bare init   而不要使用:git init

如果使用了git init初始化,则远程仓库的目录下,也包含work tree,当本地仓库向远程仓库push时,   如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题), 那么push后的结果不会反应在work tree上,  也即在远程仓库的目录下对应的文件还是之前的内容。


git忽略特定文件或目录

在将某个目录添加到git版本库、或提交某个git版本库的改动时,可能希望忽略掉一些文件或目录(如编译时生成的.o、.a文件等),可以修改.git/info/exclude文件来实现。举例如下:
vi .git/info/exclude
*.[oa]        #忽略以.o或.a结尾的文件或目录
*.pyc        
*.exe       #忽略以.exe结尾的文件或目录
.*              #忽略以.开头的文件或目录
*.rar