Linux环境下Git安装与使用

Linux环境下Git安装和使用

Linux环境下Git安装与使用

安装

  1. 官网下载并解压

    1
    2
    3
    [root@VM_0_11_centos ~]# wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.17.0.tar.gz
    [root@VM_0_11_centos ~]# tar -xvf git-2.17.0.tar.gz
    [root@VM_0_11_centos ~]# cd git-2.17.0
  2. 移除旧版本

    1
    [root@VM_0_11_centos ~]# yum remove git
  3. 安装依赖库

    1
    2
    [root@VM_0_11_centos ~]# yum install libcurl-devel
    [root@VM_0_11_centos ~]# yum install autoconf automake libtool
  4. 执行

    1
    2
    3
    4
    5
    6
    7
    [root@VM_0_11_centos ~]# make configure
    GIT_VERSION = 2.17.0
    GEN configure
    [root@VM_0_11_centos ~]# ./configure --prefix=/usr/local/git --with-iconv =/usr/local/lib(建议优先尝试后者)
    或者
    ./configure --prefix=/usr/local/git --with-iconv --with-curl --with-expat=/usr/local/lib(如果没有安装libiconv请自行安装)
    [root@VM_0_11_centos git-2.17.0]# make && make install
  5. 配置环境变量

    1
    2
    3
    4
    [root@VM_0_11_centos git-2.17.0]# vim ~/.bash_profile
    在文件末尾追加上下面命令:
    PATH=$PATH:/usr/local/git/bin
    export PATH
  6. 重新加载环境变量

    1
    [root@VM_0_11_centos git-2.17.0]# source ~/.bash_profile
  7. 查看git版本

    1
    [root@VM_0_11_centos git-2.17.0]# git --version

使用

  1. 在本地建立本地仓库

    1
    2
    3
    [root@VM_0_11_centos ~]# mkdir test
    [root@VM_0_11_centos test]# cd test
    [root@VM_0_11_centos test]# git init
  2. 把文件纳入版本控制(加入暂存区)

    1
    2
    [root@VM_0_11_centos test]# git add <filename> # 将修改后的文件加入暂存区
    [root@VM_0_11_centos test]# git add . # add后跟.是将当前文件夹下面的所有文件及文件夹都加入暂存区
  3. 提交到仓库(-m 后是描述)

    1
    [root@VM_0_11_centos test]# git commit -m '本次提交文件的相关描述信息'

    如果提交报错,看否是缺少user.nameuser.email,可执行下面的命令解决:

    1
    2
    [root@VM_0_11_centos test]# git config --global user.name 'your-name'
    [root@VM_0_11_centos test]# git config --global user.email 'your-email'
  4. 查看放入暂存区的文件

    1
    [root@VM_0_11_centos test]# git status
  5. 查看版本

    1
    [root@VM_0_11_centos test]# git log
  6. 回滚到某个版本

    1
    [root@VM_0_11_centos test]# git reset --hard 版本号
  7. 显示版本包括历史版本

    1
    2
    [root@VM_0_11_centos test]# git reflog
    [root@VM_0_11_centos test]# git reflog --pretty=oneline # 单行显示
  8. 把暂存区的内容全撤回来(可以在本地做修改,然后再次add进暂存区做提交)

    1
    [root@VM_0_11_centos test]# git checkout -- [可跟上文件名]
  9. 添加远端仓库

    1
    [root@VM_0_11_centos test]# git remote add origin https://git.coding.net/gavinliu/test.git
  10. 将本地仓库和远端仓库同步

    1
    [root@VM_0_11_centos test]# git push -u origin master
  11. 创建分支

    1
    [root@VM_0_11_centos test]# git branch [分支名]
  12. 查看所有分支

    1
    [root@VM_0_11_centos test]# git branch
  13. 切换分支

    1
    [root@VM_0_11_centos test]# git checkout [分支名]
  14. 删除文件

    1
    [root@VM_0_11_centos test]# git rm [filename]
  15. 合并分支

    1
    [root@VM_0_11_centos test]# git merge [分支名]
  16. 克隆项目到本地

    1
    [root@VM_0_11_centos ~]# git clone https://git.coding.net/jackfrued/HelloGit.git
  17. 推送到服务器,origin是原始名字,master是分支

    1
    [root@VM_0_11_centos test]# git push origin master
  18. 拉取服务器代码

    1
    [root@VM_0_11_centos test]# git pull

-------------本文结束感谢您的阅读-------------

本文标题:Linux环境下Git安装与使用

文章作者:GavinLiu

发布时间:2017年08月27日 - 18:08

最后更新:2017年08月27日 - 18:08

原始链接:http://gavinliu4011.github.io/post/9fdd7050.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

请博主吃个鸡腿吧
0%