---
title: "linux 使用Git部署应用常用命令总结"
author: "TimerBin"
date: "2016-04-06T16:09:09Z"
tags: []
categories: ["开源软件"]
slug: "linux-git"
draft: false
lastmod: "2016-04-06T16:09:09Z"
id: 2288894
url: "https://timerbin.iteye.com/blog/2288894"
---

最近将项目的版本管理工具由SVN转为Git，下面记录下在此过程中遇到的问题：

 

第一步：检验当前linux系统是否已经安装git

 

 

```
[root@root root]# git
usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
           [-p|--paginate|--no-pager] [--no-replace-objects]
           [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
           [--help] COMMAND [ARGS]
........
See 'git help COMMAND' for more information on a specific command.
```

   以上内容表示已经安装git，可以直接跳到第三步。

 

 

第二步：安装git

    

```
yum install git
```

    注:如果非root用户时请执行sudo yum install git

 

第三步：验证git是否安装成功

  

 

```
[root@root root]# git --version
git version 1.7.1
```

 

 

第四步：设置Git 用户的name和email(第一次需要设置)

 

 

```
 $ git config --global user.name "timerbin"
 $ git config --global user.email "timerbin@xxxx.net"
```

 

 

第五步：配置git-ssh秘钥，在linux系统中执行如下命令

    

```
$ ssh-keygen -t rsa -C "timerbin"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/timerbin/.ssh/id_rsa): 
Created directory '/home/timerbin/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/timerbin/.ssh/id_rsa.
Your public key has been saved in /home/timerbin/.ssh/id_rsa.pub.
The key fingerprint is:
c4:87:78:f9:de:5a:6e:6b:24:80:d2:94:74:5f:b9:c7 timerbin
The key's randomart image is:
+--[ RSA 2048]----+
|     ....   ..   |
|      o+ + ..    |
|     o..* o  o   |
|    . oo.o  . E  |
|     .  S..  .   |
|         ....    |
|          .oo    |
|           +o    |
|          .oo.   |
+-----------------+
```

 

 

第六步：将第五步生成的rsa公钥上传到git用户，具体如下所示：

Profile settings --&gt;SSH key --&gt;add sshKe。

![](http://dl2.iteye.com/upload/attachment/0116/3842/60f1a706-16be-3999-beb9-50fa1a90ba42.bmp)  
 

注：其中的SSHkey的内容来源于第五步生成的id\_rsa.pub

 

第六步：在linux 命令行中输入如下内容，验证ssh授权是否成功：

 

```
$ ssh -T git@git.com
Address xxx.xxx.xxx.xxx maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
Welcome to GitLab, timerbin!
```

    如果出现以上内容表示验证成功

 

第七步：在linux系统中执行如下命令，新建git克隆

   

```
git clone git@git.timerbin.com:timerbin/timer.git
```

   

    注：如果在执行时提示出如下错误

```
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 @        WARNING: UNPROTECTED PRIVATE KEY FILE!          @
 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
 Permissions 0644 for 'id_rsa' are too open.
 It is required that your private key files are NOT accessible by others.
 This private key will be ignored.
 bad permissions: ignore key: id_rsa
 root@xxx.xxx.xxx.xxx's password:
```

   

   请到~/.ssh/目录下执行 chmod 600 id\_rsa 为id\_rsa进行授权

 

第七步：linux系统下git部署项目时常用命令：

        查看当前项目正在使用的分支和状态：git status

        查看项目远程分支：git branch -a

        查看项目本地分支：git branch

        拉取项目本地分支：git pull origin dev      注：dev远程分支分支名称

       切换本地项目分支：git checkout dev