分类 git 下的文章

git标签操作

列出所有标签

git tag

添加标签(同时生成独立的版本库)

# 给当前commit打标签
git tag -a v0.0.1 -m '第一次添加标签'

# 给指定的commit打标签,需要commit的head
git tag -a v0.0.1 0ed1dd07d324cb5aece243a95312187955619fcf

不加 -a 参数相当于是一个commit的引用

将标签推送到远程库

# 推送本地所有标签
git push origin --tags
# 推送指定标签
git push origin v0.0.1

跳转到指定标签

git checkout v0.0.1

和跳转到branch操作一样,但是因为是独立的版本库上,你在独立标签中的git branch和在主线中包含的branch显示的是不同的,独立版本库和分支的性质是不一样的

删除指定标签

git tag -d v0.0.1

删除远程分支

git push origin -d tag <tagname>

git推送远程失败

git push 的时候提示:

remote: Permission to ******/born.git denied to  ******/.
fatal: unable to access 'https://github.com/ ******//born.git/': The requested URL returned error: 403

是因为git的账号冲突,git误认为你使用的是另一个账号,解决办法也很简单

推送方式改成ssh的,这样即使是不同账号但是使用的是同一台设备,还不需要改动究竟和哪一个账号冲突了

查看本机ssh-key

cat ~/.ssh/id_rsa.pub

登录自己的github账户>自己的头像>Setting>SSH and GPG keys>New SSH key

把本机的那一串密码填里面就行

推送方式也改成ssh的地址

git remote set-url origin git@github.com:******/born.git

通过git rebase来合并commit提交记录

多人合作开发的时候就会因为本地的多个版本提交的很多杂乱的信息,导致git log查看的时候并不能很快的从一堆无意义的提交记录中找到我们想要的版本。
做完一个小功能随时提交是一个好习惯,但是这写小记录推送到远程就是个不大不小的麻烦

我们通过rebase命令将这些小版本合并成一个大的版本,然后推送到远程,前提是你的代码功能已经达到了。

  1. 查看log信息
commit 53097de5638d371da51bc6fef74a90ca1420d967
Author: GPF <5173180@qq.com>
Date:   Sun Dec 25 22:46:13 2016 +0800

    test3

commit 830026adb43506d0bc1172432f84639f84dae087
Author: GPF <5173180@qq.com>
Date:   Sun Dec 25 22:45:56 2016 +0800

    test2

commit 646f5be02ec285e30626f5682e6e7e9437762ac5
Author: GPF <5173180@qq.com>
Date:   Sun Dec 25 22:45:43 2016 +0800

    test1
...

比如我们就合并这前三个提交记录

  1. 开启rebase脚本
git rebase -i HEAD~3

HEAD~3的意思就是最近的san tiao三条提交信息

  1. 编辑脚本
    执行步骤2的时候就会进入一个编辑脚本如下:
pick 646f5be test1
pick 830026a test2
pick 53097de test3

# Rebase dbaf38a..53097de onto dbaf38a (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

带#号的都是注释内容,这里提供了很多命令信息。最上面的三行就是我们要执行的命令,pick 查看注释是 use commit 可以简写成 p , 而且你看排列commit记录的顺序是最早的排在最上面,我们这次是为了合并commit,因此我们要用的命令就是squash 简写成 s
将脚本修改成这样:

pick 646f5be test1
s 830026a test2
s 53097de test3

# Rebase dbaf38a..53097de onto dbaf38a (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

我们合并最早的两个到test1版本当中然后:wq退出,注意合并记录的时候,不能pick最近的一条,会产生报错,如果把报错的话就输入命令git rebase --abort终止这次执行就好

  1. 接下来会直接进入一个commit记录编辑界面,#都是注释过的,将非注释的内容编辑成我们想要的然后:wq保存就完毕了

centos上安装git2.2

centos自带的git版本太低了,安装个新吧

安装依赖环境

># yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc
># yum install  gcc perl-ExtUtils-MakeMaker   error: ```/utf8.c:463: undefined reference to `libiconv'```
># wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
># tar zxvf libiconv-1.14.tar.gz 
># cd libiconv-1.14
># ./configure --prefix=/usr/local/libiconv
># make && make install

卸载Centos自带的git1.7.1

># yum remove git

下载git2.2.1并将git添加到环境变量中

># wget https://github.com/git/git/archive/v2.2.1.tar.gz
># tar zxvf v2.2.1.tar.gz
># cd git-2.2.1
># make configure
># ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv
># makeall doc
># make install install-doc install-html
># echo"export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
># source /etc/bashrc

查看版本号

># git --version
>git version2.2.1