首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Git 常用命令详解(一) 原

Git 常用命令详解(一) 原

作者头像
阿dai学长
发布2019-04-03 10:40:48
7560
发布2019-04-03 10:40:48
举报
文章被收录于专栏:阿dai_linux阿dai_linux

Git ——分布式版本管理工具

Git 安装

CentOS中:
[root@localhost ~]# yum install -y epel-release git
#安装Git之前需要先安装yum扩展源

Ubuntu上:
# sudo apt-get install git

Windows上安装:msysgit

配置个人信息

配置用户名和email

[root@localhost ~]# git config --global user.name "adai"
[root@localhost ~]# git config --global user.email "adai_mail@163.com"

[root@localhost ~]# ls -a
.   anaconda-ks.cfg  .bash_profile  .cshrc      .pki
..  .bash_logout     .bashrc        .gitconfig  .tcshrc

[root@localhost ~]# cat .gitconfig 
[user]
	name = adai
	email = adai_mail@163.com

创建仓库推送文件

创建git目录:
[root@localhost ~]# mkdir /home/gitroot
#该目录作为版本仓库使用

[root@localhost ~]# cd /home/gitroot/

初始化:
[root@localhost gitroot]# git init 
初始化空的 Git 版本库于 /home/gitroot/.git/
#目的是使该目录成为版本仓库

创建一个测试文件:
[root@localhost gitroot]# vim test.txt
#此时该文件与git仓库没任何关系

将测试文件添加到库并提交:
[root@localhost gitroot]# git add test.txt 
[root@localhost gitroot]# git commit -m "add a test file test.txt"
[master(根提交) b6c647c] add a test file test.txt
 1 file changed, 6 insertions(+)
 create mode 100644 test.txt
#-m:=mark,后面跟该文件的描述信息(做标记)

更改测试文件,查看效果:
[root@localhost gitroot]# vim test.txt 
[root@localhost gitroot]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	修改:      test.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

[root@localhost gitroot]# git checkout -- test.txt
#不提交内容,并清除更改
[root@localhost gitroot]# git status
# 位于分支 master
无文件要提交,干净的工作区

再次编辑test:
[root@localhost gitroot]# vim test.txt
比较本地和仓库中文件的区别:
[root@localhost gitroot]# git diff
diff --git a/test.txt b/test.txt
index f4bd661..b7fc199 100644
--- a/test.txt
+++ b/test.txt
@@ -4,3 +4,4 @@ sdgsdg
 hf
 
 zfzf
+new  new2  new2

提交:
[root@localhost gitroot]# git add test.txt 
[root@localhost gitroot]# git commit -m "add a new line"
[master 4303122] add a new line
 1 file changed, 1 insertion(+)

只添加不提交:
[root@localhost gitroot]# git add test.txt 
[root@localhost gitroot]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	修改:      test.txt

[root@localhost gitroot]# git reset HEAD test.txt 
重置后撤出暂存区的变更:
M	test.txt
[root@localhost gitroot]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	修改:      test.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

注: 每部操作之后都可以使用“git status”查看当前状态,可以根据提示信息进行后续操作。

版本变更

[root@localhost gitroot]# echo "new333333333333333" > test.txt 
[root@localhost gitroot]# git add "test.txt" 
[root@localhost gitroot]# git commit -m "change test.txt "
[master 0b0baf4] change test.txt
 1 file changed, 1 insertion(+), 7 deletions(-)
[root@localhost gitroot]# echo "new4444444444" > test.txt 
[root@localhost gitroot]# git add "test.txt" 
[root@localhost gitroot]# git commit -m "change test.txt again"
[master 21a521c] change test.txt again
 1 file changed, 1 insertion(+), 1 deletion(-)

查看更改记录:
[root@localhost gitroot]# git log
commit 21a521c06adb127a4aa7e427da7312c13027b4c7
Author: adai <adai_mail@163.com>
Date:   Wed Sep 20 13:39:25 2017 +0800

    change test.txt again

commit 0b0baf4f354bcf41357341cbe2ea2a7dd04d76dc
Author: adai <adai_mail@163.com>
Date:   Wed Sep 20 13:39:00 2017 +0800

    change test.txt

commit 4303122dce2bcb73202848a6dd756de58f6b3997
Author: adai <adai_mail@163.com>
Date:   Wed Sep 20 12:51:23 2017 +0800

    add a new line

commit b6c647c50be49919b179ceb1952fa75b570e33bf
Author: adai <adai_mail@163.com>
Date:   Wed Sep 20 12:39:12 2017 +0800

    add a test file test.txt

[root@localhost gitroot]# git log --pretty=oneline
21a521c06adb127a4aa7e427da7312c13027b4c7 change test.txt again
0b0baf4f354bcf41357341cbe2ea2a7dd04d76dc change test.txt
4303122dce2bcb73202848a6dd756de58f6b3997 add a new line
b6c647c50be49919b179ceb1952fa75b570e33bf add a test file test.txt

恢复至某版本“git reset”

根据由git log得到的各个版本的信息进行恢复:

[root@localhost gitroot]# git reset --hard 4303
HEAD 现在位于 4303122 add a new line

##注:最少跟指定版本的版本号的前四位

[root@localhost gitroot]# cat test.txt 
test  file:
were
sdgsdg
hf

zfzf
new  new2  new2
[root@localhost gitroot]# git log --pretty=oneline
4303122dce2bcb73202848a6dd756de58f6b3997 add a new line
b6c647c50be49919b179ceb1952fa75b570e33bf add a test file test.txt

说明: 使用reset会恢复至指定版本之前的那个版本。。。 且使用git log无法获取其他版本信息,那么如何切换至其他版本呢?

[root@localhost gitroot]# git reflog
4303122 HEAD@{0}: reset: moving to 4303
21a521c HEAD@{1}: commit: change test.txt again
0b0baf4 HEAD@{2}: commit: change test.txt
4303122 HEAD@{3}: commit: add a new line
b6c647c HEAD@{4}: commit (initial): add a test file test.txt

[root@localhost gitroot]# git reset --hard 0b0baf4
HEAD 现在位于 0b0baf4 change test.txt
[root@localhost gitroot]# cat test.txt 
new333333333333333

注意: log中是最近一次修改排在最前面!!!

文件恢复

[root@localhost gitroot]# git checkout -- test.txt

恢复至更改前的状态。

文件的删除

创建、提交新文件:
[root@localhost gitroot]# cp test.txt test2.txt
[root@localhost gitroot]# git status
# 位于分支 master
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	test2.txt
提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@localhost gitroot]# git commit -m "add test2.txt"
[master db26d1c] add test2.txt
 1 file changed, 1 insertion(+)
 create mode 100644 test2.txt

删除文件:
[root@localhost gitroot]# rm -f test.txt 
[root@localhost gitroot]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	删除:      test.txt
#
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
##此时还可以将刚刚删除的文件恢复

彻底删除文件:
[root@localhost gitroot]# git rm test.txt 
rm 'test.txt'
[root@localhost gitroot]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	删除:      test.txt
#
[root@localhost gitroot]# git commit -m "delete test.txt"
[master 12b6208] delete test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt
[root@localhost gitroot]# git status
# 位于分支 master
无文件要提交,干净的工作区

创建远程仓库

GitHub官网:github.com 注册账号并激活,然后开始创建主机的仓库! 创建完成后,添加key: 点击浏览器右上角头像——setting——SSH and GPG keys(选择SSH keys)——在服务器(虚拟机)执行ssh-keygen命令生成密钥对(/root/.ssh/id_rsa-私钥, /root/.ssh/id_rsa.pub-公钥)——将公钥复制到浏览器后点“添加”。

在远程创建一个和本地名称一致的仓库:

[root@localhost gitroot]# cd /home
[root@localhost home]# mkdir study_git
[root@localhost home]# cd study_git/
[root@localhost study_git]# git init
初始化空的 Git 版本库于 /home/study_git/.git/
[root@localhost study_git]# git remote add origin git@github.com:adaigit/study_git.git

至此,本地仓库和远程仓库连接到了一起。

文件推送:

创建文件:
[root@localhost study_git]# vim study.txt
study study study study study
study study study study
study study study
study study
study

提交到本地仓库:
[root@localhost study_git]# git add study.txt 
[root@localhost study_git]# git commit -m "add study.txt"
[master(根提交) aab304a] add study.txt
 1 file changed, 5 insertions(+)
 create mode 100644 study.txt

推送至远程:
[root@localhost study_git]# git push -u origin master 
The authenticity of host 'github.com (192.30.255.113)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.113' (RSA) to the list of known hosts.
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 225 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:adaigit/study_git.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

首次推送会有提示信息:Are you sure you want to continue connecting。

推送完成后可以上浏览器进行查看。

更改文件再次推送:

[root@localhost study_git]# vim study.txt
study study study study study
study study study study
study study study
study study
study
study study
study study study
study study study study
study study study study study

提交:
[root@localhost study_git]# git add study.txt 
[root@localhost study_git]# git commit -m "the first time to change study.txt"
[master 7a09d24] the first time to change study.txt
 1 file changed, 4 insertions(+)

推送:
[root@localhost study_git]# git push 
warning: push.default 未设置,它的默认值将会在 Git 2.0 由 'matching'
修改为 'simple'。若要不再显示本信息并在其默认值改变后维持当前使用习惯,
进行如下设置:

  git config --global push.default matching

若要不再显示本信息并从现在开始采用新的使用习惯,设置:

  git config --global push.default simple

参见 'git help config' 并查找 'push.default' 以获取更多信息。
('simple' 模式由 Git 1.7.11 版本引入。如果您有时要使用老版本的 Git,
为保持兼容,请用 'current' 代替 'simple' 模式)

Warning: Permanently added the RSA host key for IP address '192.30.255.112' to the list of known hosts.
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 275 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:adaigit/study_git.git
   aab304a..7a09d24  master -> master

我们看到上面有好多无用的提示信息,为了之后不显示这些信息,根据提示执行命令:

[root@localhost study_git]# git config --global push.default matching
[root@localhost study_git]# git config --global push.default simple

在浏览器查看:信息会变更!!!

克隆远程仓库

[root@localhost study_git]# cd /home/

克隆:
[root@localhost home]# git clone git@github.com:aminglinux/lanmp.git
正克隆到 'lanmp'...
remote: Counting objects: 26, done.
remote: Total 26 (delta 0), reused 0 (delta 0), pack-reused 26
接收对象中: 100% (26/26), 5.46 KiB | 0 bytes/s, done.
处理 delta 中: 100% (4/4), done.

[root@localhost home]# ls
gitroot  lanmp  study_git

[root@localhost home]# cd lanmp/
[root@localhost lanmp]# ls
lanmp.sh  README.md

编辑仓库:

[root@localhost lanmp]# vim lanmp.sh 
[root@localhost lanmp]# git add lanmp.sh 
[root@localhost lanmp]# git commit -m "add the last line to test git-clone"
[master bbe891c] add the last line to test git-clone
 1 file changed, 1 insertion(+)

此时只是提交到了本地。

推送:

[root@localhost lanmp]# git push
ERROR: Permission to aminglinux/lanmp.git denied to adaigit.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

因为没得到该库(私人)的授权(将本地密钥存入对方ssh key中),所以无法推送。

分支管理

查看分支

[root@localhost home]# cd study_git/
[root@localhost study_git]# git branch
* master

创建分支

[root@localhost study_git]# git branch adai
[root@localhost study_git]# git branch
  adai
* master

带*的表示当前所在的分支,使用如下命令切换分支:

[root@localhost study_git]# git checkout adai
切换到分支 'adai'
[root@localhost study_git]# git branch
* adai
  master

创建文件:
[root@localhost study_git]# vim linux.txt
[root@localhost study_git]# ls
linux.txt  study.txt
[root@localhost study_git]# git add linux.txt 
[root@localhost study_git]# git commit -m "create a new file linux"
[adai 297709a] create a new file linux
 1 file changed, 3 insertions(+)
 create mode 100644 linux.txt
[root@localhost study_git]# git checkout master
切换到分支 'master'
[root@localhost study_git]# ls
study.txt

由此可见,在不同的分支可看到的文件的内容是不同的,在所有分支都能看到master分支的文件。

推送:

[root@localhost study_git]# git push
fatal: 当前分支 adai 没有对应的上游分支。
为推送当前分支并建立与远程上游的跟踪,使用

    git push --set-upstream origin adai

[root@localhost study_git]# git push --set-upstream origin adai
Counting objects: 9, done.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 712 bytes | 0 bytes/s, done.
Total 9 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To git@github.com:adaigit/study_git.git
 * [new branch]      adai -> adai
分支 adai 设置为跟踪来自 origin 的远程分支 adai。

分支的合并和删除

分支合并

[root@localhost study_git]# git checkout master
切换到分支 'master'
[root@localhost study_git]# git merge adai
更新 7a09d24..297709a
Fast-forward
 linux.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 linux.txt
[root@localhost study_git]# ls
linux.txt  study.txt

合并分支前先切换到将要合并到的分支上(如上,把adai分支的内容合并到master)。

分支合并原则
在adai分支下更改Linux文件:
[root@localhost study_git]# vim linux.txt 
[root@localhost study_git]# git add linux.txt 
[root@localhost study_git]# git commit -m "change linux.txt 1 time"
[adai e53b1a0] change linux.txt 1 time
 1 file changed, 1 insertion(+)

[root@localhost study_git]# git checkout master

在master分支更改Linux文件:
[root@localhost study_git]# vim linux.txt 
[root@localhost study_git]# git add linux.txt 
[root@localhost study_git]# git commit -m "change linux.txt  in master branch"
[master 3fb4603] change linux.txt  in master branch
 1 file changed, 2 insertions(+)

合并分支adai到master:

[root@localhost study_git]# git merge adai
自动合并 linux.txt
冲突(内容):合并冲突于 linux.txt
自动合并失败,修正冲突然后提交修正的结果。

此时,发生冲突!!!需要先修复冲突后才能合并。。。

解决冲突:

使用git diff或cat查看冲突在哪:

[root@localhost study_git]# git diff
diff --cc linux.txt
index 79a4151,ba241d8..0000000
--- a/linux.txt
+++ b/linux.txt
@@@ -2,4 -2,3 +2,7 @@@ linux linux linu
  linux linux
  linus
  linux linux
++<<<<<<< HEAD
 +linux linux linux
++=======
++>>>>>>> adai
[root@localhost study_git]# cat linux.txt 
linux linux linux
linux linux
linus
linux linux
<<<<<<< HEAD
linux linux linux
=======
>>>>>>> adai


解决办法:
在master中编辑Linux文件,内部有提示符,将冲突部分删除。  
[root@localhost study_git]# vim linux.txt 
[root@localhost study_git]# git add linux.txt 
[root@localhost study_git]# git commit -m  "after changed linux.txt"
[master e94153c] after changed linux.txt
[root@localhost study_git]# git merge adai
Already up-to-date.

即,解决冲突的办法是更改目标分支下冲突文件和源分支中内容一致,然后在提交、合并。

原则:只能将新分支合并到旧的分支,即merge后面跟内容最新的分支。

删除分支

[root@localhost study_git]# git branch -d adai
warning: 并未删除分支 'adai', 虽然它已经合并到 HEAD,
         然而却尚未被合并到分支 'refs/remotes/origin/adai' 。
error: 分支 'adai' 没有完全合并。
如果您确认要删除它,执行 'git branch -D adai'。
[root@localhost study_git]# git branch -D adai
已删除分支 adai(曾为 e53b1a0)。
[root@localhost study_git]# git branch 
* master

说明: -d:删除;-D:强制删除

分支使用原则

  • master分支是非常重要的,线上发布代码用这个分支,平时开发代码不要在该分支操作;
  • 创建dev分支,专门用作开发,只有到发布到线上之前再把dev合并到master上
  • 开发人员应该在dev分支的基础上再分支成个人分支,自己的分支(在自己的pc上)里面开发代码,然后合并到dev分支。
  • 在dev分支合并bob分支的命令:
git checkout dev
git merge bob

现场保留

场景: 当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作中。问题是你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决问题的办法就是:git stash(存储)命令。

比如,我们在adai分支,编辑一个新的文件3.txt,此时我们要去其他分支。

[root@localhost study_git]# vim 3.txt
[root@localhost study_git]# git add 3.txt
#暂时还没编辑完,不提交  

开始处理其他工作:  
[root@localhost study_git]# vim study.txt 
#该工作处理完成后提交
[root@localhost study_git]# git add study.txt
[root@localhost study_git]# git commit -m "fix a bug in study.txt"
[master 4626ac4] fix a bug in study.txt
 1 file changed, 1 insertion(+)

查看状态:
[root@localhost study_git]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 4 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	新文件:    3.txt
#
# 未跟踪的文件:
#   (使用 "git add <file>..." 以包含要提交的内容)
#
#	.3.txt.swp

此时查看git状态会一直显示3.txt未提交,为了避免这种状况发生,执行如下命令:

[root@localhost study_git]# git stash
Saved working directory and index state WIP on master: 4626ac4 fix a bug in study.txt
HEAD 现在位于 4626ac4 fix a bug in study.txt
[root@localhost study_git]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 4 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区

[root@localhost study_git]# ls
linux.txt  README.md  study.txt

完成现场保留后,3.txt文件不见了!!! 查看保存过的文件:

[root@localhost study_git]# git stash list
stash@{0}: WIP on master: 4626ac4 fix a bug in study.txt
stash@{1}: WIP on master: 4626ac4 fix a bug in study.txt
stash@{2}: WIP on master: 4626ac4 fix a bug in study.txt
stash@{3}: WIP on master: 4626ac4 fix a bug in study.txt

恢复现场

[root@localhost study_git]# git stash apply stash@{3}
# 位于分支 master
# 您的分支领先 'origin/master' 共 4 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	新文件:    3.txt
#
[root@localhost study_git]# ls
3.txt  linux.txt  README.md  study.txt

[root@localhost study_git]# git commit -m "create new file 3.txt"
[master d3e70c3] create new file 3.txt
 1 file changed, 3 insertions(+)
 create mode 100644 3.txt
 
 
删除保存的内容:
[root@localhost study_git]# git stash drop stash@{0}
#注意:stash@{0}该编号会随着前面的文件被删除而更新

远程分支

在浏览器创建分支

删除分支:

点击此处进入分支管理界面。

在服务器创建分支

[root@localhost study_git]# git branch
* master

创建分支:
[root@localhost study_git]# git branch dev

[root@localhost study_git]# git checkout dev
切换到分支 'dev'

[root@localhost study_git]# git branch
* dev
  master

[root@localhost study_git]# ls
3.txt  linux.txt  README.md  study.txt

删除文件:
[root@localhost study_git]# rm -f 3.txt 
[root@localhost study_git]# git rm 3.txt
rm '3.txt'

[root@localhost study_git]# git commit -m "delete 3.txt"
[dev 5e4665e] delete 3.txt
 1 file changed, 3 deletions(-)
 delete mode 100644 3.txt
[root@localhost study_git]# ls
linux.txt  README.md  study.txt

将本地分支提交到远程

  • 查看远程仓库信息:
[root@localhost study_git]# git remote -v
origin	git@github.com:adaigit/study_git.git (fetch)
origin	git@github.com:adaigit/study_git.git (push)
  • 查看远程分支信息:
[root@localhost study_git]# git ls-remote origin
33f8e56169245e2681b9e5e0c5172efdbcbcdc6e	HEAD
33f8e56169245e2681b9e5e0c5172efdbcbcdc6e	refs/heads/master
  • 推送本地分支到远程:
[root@localhost study_git]# git push origin dev
Counting objects: 19, done.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (15/15), 1.45 KiB | 0 bytes/s, done.
Total 15 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 1 local object.
To git@github.com:adaigit/study_git.git
 * [new branch]      dev -> dev

推送成功!

在本地创建和远程分支对应的分支

  • 先远程创建一个新的分支:
  • 查看本地分支
[root@localhost study_git]# git branch
  dev
* master
  • 同步本地分支
[root@localhost study_git]# git checkout -b dev2 origin/dev2
分支 dev2 设置为跟踪来自 origin 的远程分支 dev2。
切换到一个新分支 'dev2'
[root@localhost study_git]# git branch
  dev
* dev2
  master

从远程抓取分支

[root@localhost study_git]# git pull

标签管理

首先保证自己在master分支下:  
[root@localhost study_git]# git branch
  dev
  dev2
* master

打标签:
[root@localhost study_git]# git tag v1.0

查看所有标签:
[root@localhost study_git]# git tag
v1.0

查看指定标签的信息:
[root@localhost study_git]# git show v1.0
commit d3e70c3e2951547588ef8ecda0ea6a8eb883ede6
Author: adai <adai_mail@163.com>
Date:   Wed Sep 20 17:05:25 2017 +0800

    create new file 3.txt

diff --git a/3.txt b/3.txt
new file mode 100644
index 0000000..2eb0a6b
--- /dev/null
+++ b/3.txt
@@ -0,0 +1,3 @@
+11111111111
+22222
+33

删除标签:
[root@localhost study_git]# git tag
show
v1.0
[root@localhost study_git]# git tag -d show
已删除 tag 'show'(曾为 d3e70c3)

查看操作记录:
[root@localhost study_git]# git log --pretty=oneline --abbrev-commit
d3e70c3 create new file 3.txt
4626ac4 fix a bug in study.txt
e94153c after changed linux.txt
3fb4603 change linux.txt  in master branch
e53b1a0 change linux.txt 1 time
33f8e56 Merge branch 'master' of github.com:adaigit/study_git
297709a create a new file linux
384b0e2 Create README.md
7a09d24 the first time to change study.txt
aab304a add study.txt


针对某条记录加标签:
[root@localhost study_git]# git tag v0.5 7a09d24

[root@localhost study_git]# git show v0.5
commit 7a09d2404121c11fb2b5046d495f3cf1d3b5cfe4
Author: adai <adai_mail@163.com>
Date:   Wed Sep 20 14:51:01 2017 +0800

    the first time to change study.txt

diff --git a/study.txt b/study.txt
index cb3e341..b9662a7 100644
--- a/study.txt
+++ b/study.txt
@@ -3,3 +3,7 @@ study study study study
 study study study
 study study
 study
+study study
+study study study
+study study study study
+study study study study study

为标签增加描述信息:
[root@localhost study_git]# git tag -a v0.6 -m "test tag" 297709a
[root@localhost study_git]# git tag
v0.5
v0.6
v1.0

[root@localhost study_git]# git show v0.6
tag v0.6
Tagger: adai <adai_mail@163.com>
Date:   Wed Sep 20 21:07:44 2017 +0800

test tag
  • 将标签推送到远程
[root@localhost study_git]# git push origin v1.0
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:adaigit/study_git.git
 * [new tag]         v1.0 -> v1.0

添加标签后相当于进行了发布!!!

  • 推送所有标签
[root@localhost study_git]# git push --tag origin
Counting objects: 1, done.
Writing objects: 100% (1/1), 151 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@github.com:adaigit/study_git.git
 * [new tag]         v0.5 -> v0.5
 * [new tag]         v0.6 -> v0.6
  • 在本地删除远程标签
[root@localhost study_git]# git tag -d v0.6
已删除 tag 'v0.6'(曾为 f60aae1)
[root@localhost study_git]# git tag -d v0.5
已删除 tag 'v0.5'(曾为 7a09d24)
[root@localhost study_git]# git tag
v1.0

同步到远程:
[root@localhost study_git]# git push origin :refs/tags/v0.5
To git@github.com:adaigit/study_git.git
 - [deleted]         v0.5
[root@localhost study_git]# git push origin :refs/tags/v0.6
To git@github.com:adaigit/study_git.git
 - [deleted]         v0.6

git 别名

[root@localhost study_git]# git config --global alias.ci commit

说明: 把ci设置为commit命令的别名。

[root@localhost study_git]# git config --global alias.co checkout

[root@localhost study_git]# cat /root/.gitconfig 
[user]
	name = adai
	email = adai_mail@163.com
[push]
	default = simple
[alias]
	co = checkout

设置号的别名会保存在文件/root/.gitconfig中,所以可以通过编辑该文件来设置别名。

* 查看配置文件信息  

[root@localhost study_git]# git config --list |grep alias
alias.co=checkout
  • 查询log的小技巧
[root@localhost study_git]# git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

[root@localhost study_git]# git lg
  • 取消别名
[root@localhost study_git]# git config --global --unset alias.co

[root@localhost study_git]# cat /root/.gitconfig 
[user]
	name = adai
	email = adai_mail@163.com
[push]
	default = simple
[alias]
	lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/09/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Git ——分布式版本管理工具
    • Git 安装
      • 配置个人信息
        • 配置用户名和email
      • 创建仓库推送文件
        • 版本变更
          • 恢复至某版本“git reset”
        • 文件恢复
          • 文件的删除
            • 创建远程仓库
              • 文件推送:
            • 克隆远程仓库
              • 分支管理
                • 查看分支
                • 创建分支
              • 分支的合并和删除
                • 分支合并
                • 删除分支
              • 分支使用原则
                • 现场保留
                  • 恢复现场
                • 远程分支
                  • 在浏览器创建分支
                  • 在服务器创建分支
                  • 将本地分支提交到远程
                  • 在本地创建和远程分支对应的分支
                  • 从远程抓取分支
                • 标签管理
                  • git 别名
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档