CentOS 上Git安装及使用

来源:互联网 发布:mac zookeeper 客户端 编辑:程序博客网 时间:2024/06/06 04:32

下载

下载网址:https://github.com/git/git/releases

wget https://github.com/git/git/archive/v2.14.0.tar.gztar -zxvf git-v2.14.0.tar.gz

安装Git

依赖包安装
yum -y install gcc-c++ zlib-devel openssl-devel cpio expat-devel gettext-devel curl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker

cd git-2.14.0/make prefix=/usr/local allmake prefix=/usr/local install

查看版本
git –version


生成密钥

  1. ssh-keygen -t rsa -C “desc key”
  2. ssh-add ~/.ssh/id_rsa
    Could not open a connection to your authentication agent.

    eval ssh-agent
    ssh-add ~/.ssh/id_rsa

  3. cat ~/.ssh/id_rsa.pub

注意:每台电脑每个用户生成的key不一样,-C后面的参数尽量描述清楚生成的key属于哪台电脑哪个用户


配置全局环境变量

  • git config –global user.name “xxx”
    全局变量用户名
  • git config –global user.email “xxx@xxx.com”
    全局变量用户邮箱
  • git config –global core.autocrlf false
    Carriage-Return Line-Feed 回车换行设置
    https://cnbin.github.io/blog/2015/06/19/git-core-dot-autocrlf-pei-zhi-shuo-ming/
  • git config –global core.quotepath off
    http://www.01happy.com/centos-git-support-chinese/
  • git config –global gui.encoding utf-8
    字符集

查看配置参数
git config –list


常规使用

  • git init
    初始化版本库

  • git status
    查看状态

  • git add .
    添加文件

  • git commit -am ‘commit message’
    提交

  • git remote add origin git@git.oschina.net:xxx/xxx.git
    添加远程分支

  • git branch
    查看分支
    git branch -r –>查看远程分支

  • git push -u origin master
    提交代码到远程master分支

    Warning: Permanently added the RSA host key for IP address '120.55.226.24' to the list of known hosts.To git.oschina.net:xxx/xxx.git! [rejected]        master -> master (fetch first)error: failed to push some refs to 'git@git.oschina.net:xxx/xxx.git'hint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changeshint: (e.g., 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.

    提交代码前先pull一下代码
    或者强制提交,覆盖
    git push -u -f origin master

  • git pull
    更新代码

  • git branch –set-upstream-to=origin/master master
    本地分支与远程分支关联
    http://higoge.github.io/2015/07/06/git-remote03/

  • git checkout -b v1.0 origin/master
    建立分支

  • 切换远程分支

    1. git remote set-url origin url
    2. git remote rm origin
      git remote add origin git@github.com:sheng/demo.git
    3. 如果你的项目有加入版本控制,那可以到项目根目录下,查看隐藏文件夹, 发现.git文件夹,找到其中的config文件,就可以修改其中的git remote origin地址了
  • git config remote.origin.push refs/heads/*:refs/for/*
    当执行push命令时,将会推送到refs/for/当前head所在的分支上

  • ERROR: [e1171c3] missing Change-Id in commit message footer

    gitdir=$(git rev-parse –git-dir); scp -p -P 29418 username@IP:hooks/commit-msg ${gitdir}/hooks/
    git commit –amend
    git push

原创粉丝点击