一、为什么要使用版本管理工具?
老板要求小明写一个可行性报告,当他写完给老板看后,结果……
二、git简介
早先linux内核代码托管在BitKeeper,这个东西是商业的,但是免费给linux社区使用
linux社区有个牛人试图破解BitKeeper,被BitKeeper公司人发现后,不再免费提供使用权,于是linus花两周时间开发了一个分布式的版本控制系统,就是git
其实git功能和svn是类似的,但svn是集中管理,git是分布式管理
集中管理:必须要有一台服务端,代码仓库是在服务端上,客户端(个人电脑)需要从服务端上同步代码到自己电脑上,开发完要推送到服务端。这种架构依赖网络,传输也比较慢。
分布式:可以没有服务端,所有个人电脑都可以有完整的代码仓库,代码的更新、推送、分支、合并都可以在自己电脑上完成。它也支持多人协作,不过需要有一个公共的git服务器作为支撑。
三、Git使用
1. 安装git
centos上
yum install -y epel-release
yum install -y git
ubuntu上
sudo apt-get install git
windows上安装msysgit
下载地址 https://git-for-windows.github.io
或者 http://pan.baidu.com/s/1skFLrMt#path=%252Fpub%252Fgit
安装完成后,还需要最后一步设置
[root@132 ~]# git config --global user.name "daixuan" //写你的姓名
[root@132 ~]# git config --global user.email "[email protected]" //写你的邮箱
[root@132 ~]# ls -la .gitconfig
-rw-r--r-- 1 root root 50 May 3 19:42 .gitconfig
[root@132 ~]# cat .gitconfig
[user]
name = daixuan
email = [email protected]
2. 创建版本库
mkdir /home/gitroot
cd /home/gitroot
git init //用这个命令初始化,让这个目录变成git可以管理的仓库
[root@132 gitroot]# ls -la
total 12
drwxr-xr-x 3 root root 4096 May 3 19:48 .
drwxr-xr-x. 5 root root 4096 May 3 19:48 ..
drwxr-xr-x 7 root root 4096 May 3 19:48 .git
[root@132 gitroot]# ls .git/
branches config description HEAD hooks info objects refs
3. 提交文件到仓库
创建一个文件 1.txt
echo -e "123\naaa\n456\nbbb" > 1.txt
[root@132 gitroot]# touch 1.txt [root@132 gitroot]# echo -e "123\nfshfhd\njdshjfhj" > 1.txt [root@132 gitroot]# cat 1.txt 123 fshfhd jdshjfhj
echo -e "111111\n2222222222" >> 1.txt //先更改一下1.txt
git status //查看当前仓库中的状态,比如是否有改动的文件
git diff 1.txt //可以对比1.txt本次修改了什么内容,相比较仓库里面的版本
# On branch master # Changed but not updated: # (use "git add..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: 1.txt # no changes added to commit (use "git add" and/or "git commit -a")
git add 1.txt //把1.txt添加到仓库中
git commit -m "add new file 1.txt" //add完了必须要commit才算真正把文件提交到git仓库里
如果之前没有设置user和email,初始使用该命令会提示:
*** Please tell me who you are.
Run
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
这是要让我们设置用户邮箱和用户名。根据它提示设置一下即可:
git config --global user.email "[email protected]"
git config --global user.name "aming"
4. 撤销修改
修改1.txt,发现改的不对,想恢复到上一次提交时的状态。或者1.txt不小心给删除掉了。可以使用
git checkout -- 1.txt //恢复到上一次提交时的状态
如果1.txt修改完了,保存后,git add 1.txt,但没有commit,再想回退到上一次提交时的状态。可以使用
git reset HEAD 1.txt
然后再
git checkout -- 1.txt
那如果不仅add也commit了,那就用第5步,版本回退的方法。
实例:
更改了1.txt,没有add,也没有commit,如何恢复?
git checkout -- 1.txt
[root@132 gitroot]# echo -e "123\nfshfhd\njdshjfhj\n12345\7898" > 1.txt [root@132 gitroot]# git status # On branch master # Changed but not updated: # (use "git add..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: 1.txt # no changes added to commit (use "git add" and/or "git commit -a") [root@132 gitroot]# git diff 1.txt diff --git a/1.txt b/1.txt index 95b3d50..6d731a7 100644 --- a/1.txt +++ b/1.txt @@ -1,3 +1,4 @@ 123 fshfhd jdshjfhj +12345\7898 与线上的git仓库对比,添加了行12345\7898 [root@132 gitroot]# git checkout -- 1.txt 取消修改1.txt [root@132 gitroot]# git diff 1.txt [root@132 gitroot]# git status 没有任何需要修改 # On branch master nothing to commit (working directory clean)
git add 1.txt 但是没有commit,如何恢复??
git reset HEAD 1.txt
git checkout -- 1.txt
[root@132 gitroot]# echo -e "123\nfshfhd\njdshjfhj\n12345\n7898\nhdjhs\n" > 1.txt [root@132 gitroot]# git diff diff --git a/1.txt b/1.txt index f8a2f82..ab6f7f8 100644 --- a/1.txt +++ b/1.txt @@ -3,3 +3,5 @@ fshfhd jdshjfhj 12345 7898 +hdjhs + [root@132 gitroot]# git add 1.txt [root@132 gitroot]# git status # On branch master # Changes to be committed: # (use "git reset HEAD..." to unstage) # #modified: 1.txt # [root@132 gitroot]# git reset HEAD 1.txt Unstaged changes after reset: M 1.txt [root@132 gitroot]# git checkout -- 1.txt
5. 版本回退
多更改几次1.txt,并进行 git add, git commit操作,如何恢复?
git log //可以查看所有提交git仓库的记录操作
git log --pretty=oneline //一行显示,更清楚
通过git log我们可以查看到过去提交的所有版本,所以根据这个log,我们可以指定回退某个版本
比如,结果如下
git log --pretty=oneline
[root@132 gitroot]# git log --pretty=oneline 271c6acb7cc97e995faf939479c00f8f5cedbc5b change 1.txt again again 57483599130d4e396f3e6c09872b1cac1c961633 add 3 row 9d03c74abd369642015dbcfa9a53b31ef9cd005d add 2 row 5c1809bd1698d8d5b2012596fafec9a4a53f3ac5 add 12345 7898 7736621c72aef31bcc1e564865a934ff467c8d90 add new file 1.txt
git reset --hard 5c18 //可以回退到第二行的这个版本,这里后面跟的字符串可以简写。
当回退到该版本后,再 git log 则最上面的版本不再显示了,只显示回退时间之前的版本,那如果想再倒回退到第一行那个版本怎么办?
git reflog //可以显示所有的版本,然后使用:
git reset --hard 271c 需要回退行版本号
[root@132 gitroot]# vim 1.txt [root@132 gitroot]# git add 1.txt [root@132 gitroot]# git commit -m "add 3 row" [master 5748359] add 3 row 1 files changed, 4 insertions(+), 2 deletions(-) [root@132 gitroot]# echo "1234567890" >> 1.txt [root@132 gitroot]# git add 1.txt [root@132 gitroot]# git commit -m "change 1.txt again again" [master 271c6ac] change 1.txt again again 1 files changed, 1 insertions(+), 0 deletions(-) [root@132 gitroot]# git log --pretty=oneline 271c6acb7cc97e995faf939479c00f8f5cedbc5b change 1.txt again again 57483599130d4e396f3e6c09872b1cac1c961633 add 3 row 9d03c74abd369642015dbcfa9a53b31ef9cd005d add 2 row 5c1809bd1698d8d5b2012596fafec9a4a53f3ac5 add 12345 7898 7736621c72aef31bcc1e564865a934ff467c8d90 add new file 1.txt [root@132 gitroot]# git reset --hard 5c18 HEAD is now at 5c1809b add 12345 7898 [root@132 gitroot]# cat 1.txt 123 fshfhd jdshjfhj 12345 7898 [root@132 gitroot]# git log --pretty=oneline 5c1809bd1698d8d5b2012596fafec9a4a53f3ac5 add 12345 7898 7736621c72aef31bcc1e564865a934ff467c8d90 add new file 1.txt [root@132 gitroot]# git reflog 5c1809b HEAD@{0}: 5c18: updating HEAD 271c6ac HEAD@{1}: commit: change 1.txt again again 5748359 HEAD@{2}: commit: add 3 row 9d03c74 HEAD@{3}: commit: add 2 row 5c1809b HEAD@{4}: commit: add 12345 7898 7736621 HEAD@{5}: commit (initial): add new file 1.txt [root@132 gitroot]# git reset --hard 5748359 HEAD is now at 5748359 add 3 row [root@132 gitroot]# cat 1.txt 123 fshfhd jdshjfhj 12345 7898 hdjhs jisjfs fjjkhsdjh jsdkjh 2456789 2345678 [root@132 gitroot]# git reflog 5748359 HEAD@{0}: 5748359: updating HEAD 5c1809b HEAD@{1}: 5c18: updating HEAD 271c6ac HEAD@{2}: commit: change 1.txt again again 5748359 HEAD@{3}: commit: add 3 row 9d03c74 HEAD@{4}: commit: add 2 row 5c1809b HEAD@{5}: commit: add 12345 7898 7736621 HEAD@{6}: commit (initial): add new file 1.txt [root@132 gitroot]# git reset --hard 271c6a HEAD is now at 271c6ac change 1.txt again again [root@132 gitroot]# cat 1.txt 123 fshfhd jdshjfhj 12345 7898 hdjhs jisjfs fjjkhsdjh jsdkjh 2456789 2345678
6. 删除与add方法相同
touch 2.txt
echo "22222222222222" > 2.txt
git add 2.txt
git commit -m "add new file 2.txt"
上面的操作,我们在git仓库里提交了一个新的文件2.txt,下面删除它
rm -f 2.txt
git status //可以看到,提示2.txt文件被删除了(如果要想恢复,直接用 git checkout -- 2.txt)
现在是,我们想在git仓库里删除
git rm 2.txt (这个时候如果想要恢复,git reset HEAD 2.txt git checkout -- 2.txt)
git commit -m "delete 2.txt"
这样就彻底删除了2.txt(这个时候如果想恢复2.txt,就得参考第5步,使用log了)
7. 做一个远程仓库(github)
首先到 https://github.com 注册一个账号,创建自己的git,点repositories --> new
名字自定义,比如叫studygit 选择public 点 create repository
添加key:
右上角点自己头像,选择settings,左侧选择SSH and GPG keys
右上角点New SSH key,把linux机器上的 /root/.ssh/id_rsa.pub内容粘贴到这里
本地先建立好本地的git仓库和文件
[root@132 home]# mkdir /home/studygit [root@132 home]# cd /home/studygit/ [root@132 studygit]# git init [root@132 studygit]# echo -e "david test github" > david.txt [root@132 studygit]# git add david.txt [root@132 studygit]# git commit -m "add david.txt" [master (root-commit) 6a7ff00] add david.txt 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 david.txt
8.把本地参考推送到远程仓库
[root@132 studygit]# git remote add origin [email protected]:1350368559/studygit.git
下一次再推送,就可以直接 git push ,第一次git push 需要输入yes且使用origin master
git push -u origin master
[root@132 studygit]# git push -u origin master The authenticity of host 'github.com (192.30.253.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.253.113' (RSA) to the list of known hosts. Counting objects: 3, done. Writing objects: 100% (3/3), 227 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:1350368559/studygit.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
刷新浏览器,已经把david.txt推送到了远程github
第二次修改david.txt,一条git push命令即可,内容是我们本地修改的内容
[root@132 studygit]# echo -e "david test github\n djshahdjha" > david.txt [root@132 studygit]# git add david.txt [root@132 studygit]# git commit -m "change david.txt" [master 66c9786] change david.txt 1 files changed, 1 insertions(+), 0 deletions(-) [root@132 studygit]# git push Warning: Permanently added the RSA host key for IP address '192.30.253.112' to the list of known hosts. Counting objects: 5, done. Writing objects: 100% (3/3), 270 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:1350368559/studygit.git 6a7ff00..66c9786 master -> master
9.下面我们克隆一个远程仓库过来
cd /home 不用初始化新建目录
git clone [email protected]:aminglinux/lanmp.git
它提示,会在当前目录下初始化一个仓库,并创建一个.git的目录
Initialized empty Git repository in /home/lanmp/.git/
完成后,ls可以看到一个lanmp的目录
cd lanmp
vi lanmp.sh 编辑一下文件,然后提交
git add lanmp.sh
git commit -m "sdlfasdf"
然后再推送到远程服务端
git push
实例:
[root@132 david]# cd /home/david/ [root@132 david]# ll total 0 [root@132 david]# git clone [email protected]:1350368559/studygit.git Initialized empty Git repository in /home/david/studygit/.git/ remote: Counting objects: 6, done. remote: Compressing objects: 100% (2/2), done. remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0 Receiving objects: 100% (6/6), done. [root@132 david]# ll total 4 drwxr-xr-x 3 root root 4096 May 4 21:04 studygit [root@132 studygit]# vim david.txt david test github you can use this to test git clone [root@132 studygit]# git add david.txt [root@132 studygit]# git commit -m "you can use this to study git clone" [master 9cfd640] you can use this to study git clone 1 files changed, 2 insertions(+), 1 deletions(-) [root@132 studygit]# git push Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 303 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:1350368559/studygit.git 66c9786..9cfd640 master -> master
远程的git服务器已经更新了最新:
10. 分支管理
查看分支
git branch
创建分支
git branch daixuan
切换到分支daixuan
git checkout daixuan
[root@132 studygit]# git branch daixuan [root@132 studygit]# git branch daixuan * master [root@132 studygit]# git checkout daixuan Switched to branch 'daixuan' [root@132 studygit]# git branch * daixuan master
在daixuan分支下 ,编辑linux.txt,并提交到新分支
echo -e "add branch daixuan" > linux.txt
git add linux.txt
git commit -m "add a new linux.txt"
切换回master分支
git checkout master
[root@132 studygit]# git add linux.txt [root@132 studygit]# git commit -m "add a new linux.txt" [daixuan 5aff4d7] add a new linux.txt 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 linux.txt [root@132 studygit]# git checkout master Switched to branch 'master' [root@132 studygit]# ls david.txt [root@132 studygit]# git checkout daixuan Switched to branch 'daixuan' [root@132 studygit]# ls david.txt linux.txt
合并分支,在master分支下,合并daixuan分支到master
git merge daixuan //把daixuan分支合并到了master
[root@132 studygit]# git checkout master Switched to branch 'master' [root@132 studygit]# git merge daixuan Updating 9cfd640..5aff4d7 Fast-forward linux.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 linux.txt [root@132 studygit]# ls david.txt linux.txt
如果master分支和daixuan.txt分支都对linux.txt进行了编辑,当合并时会提示冲突,需要先解决冲突才可以继续合并。
解决冲突的方法是在master分支下,编辑linux.txt,改为daixuan分支里面linux.txt的内容。 然后提交linux.txt,再合并daixuan分支。(这里其实不用merge,add+commit后的两个分支内容都是一样的)
但是这样有一个问题,万一master分支更改的内容是我们想要的呢? 我们可以编辑linxu.txt内容,改为我们想要的,然后提交。切换到daixuan分支,然后合并master分支到daixuan分支即可。(倒着合并)(这种情况下,说实话,直接把daixuan分支删除,然后重新新建分支daixuan效果一样)
[root@132 studygit]# git branch daixuan * master [root@132 studygit]# ls david.txt linux.txt [root@132 studygit]# echo -e "add a line to linux.txt" >> linux.txt [root@132 studygit]# cat linux.txt add branch daixuan add a line to linux.txt [root@132 studygit]# git checkout daixuan Mlinux.txt Switched to branch 'daixuan' [root@132 studygit]# git branch * daixuan master [root@132 studygit]# git merge master Already up-to-date. [root@132 studygit]# ls david.txt linux.txt [root@132 studygit]# cat linux.txt add branch daixuan add a line to linux.txt
合并分支有一个原则:那就是要把最新的分支合并到旧的分支。也就是说merge后面跟的分支名字一定是最新的分支。
这个新指的是内容新
删除分支
git branch -d aming
[root@132 studygit]# git branch -d daixuan Deleted branch daixuan (was 5aff4d7). [root@132 studygit]# git branch * master
如果分支已经合并到master,会直接删除,如果分支没有合并,删除之前会提示,那我们就不想合并,强制删除,使用-D
git branch -D aming
对于分支的应用,建议大家以这样的原则来:
master分支是非常重要的,线上发布代码用这个分支,平时我们开发代码不要在这个分支上。
创建一个dev分支,专门用作开发,只有当发布到线上之前,才会把dev分支合并到master
开发人员应该在dev的基础上再分支成个人分支,个人分支(在自己pc上)里面开发代码,然后合并到dev分支
在dev分支上合并bob分支代码的命令是:
git checkout dev //先切换到dev分支,然后
git merge bob
11. git stash 保留现场
当你正在进行项目中某一部分的工作,里面的东西处于一个比较杂乱的状态,而你想转到其他分支上进行一些工作。问题是,你不想提交进行了一半的工作,否则以后你无法回到这个工作点。解决这个问题的办法就是git stash命令。
比如我们在master分支,编辑了一个新的文件3.txt
这时候我们需要到其他分支去修复一个bug,所以需要先git add 3.txt
然后 git stash 保存一下现场
再切换到另外分支去修复bug,修复完bug后,再回到master分支
git stash list 可以看到我们保存过的现场
用 git stash apply 恢复现场
也可以指定stash:
git stash apply stash@{1}
[root@132 studygit]# vim 3.txt [root@132 studygit]# ls 3.txt david.txt linux.txt [root@132 studygit]# git branch * master 3.txt只编辑到一半,现在测试发现david.txt存在bug,先去修改david.txt [root@132 studygit]# vim david.txt [root@132 studygit]# git add david.txt [root@132 studygit]# git commit -m "fix a bug daixuan.txt" [master fb5651a] fix a bug daixuan.txt 1 files changed, 1 insertions(+), 1 deletions(-) [root@132 studygit]# git stash Saved working directory and index state WIP on master: fb5651a fix a bug daixuan.txt HEAD is now at fb5651a fix a bug daixuan.txt [root@132 studygit]# git status 注意:这里暂时没有commit的3.txt会被所有人看到,很low # On branch master # Your branch is ahead of 'origin/master' by 2 commits. # # Untracked files: # (use "git add..." to include in what will be committed) # # 3.txt nothing added to commit but untracked files present (use "git add" to track) 解决这个问题的办法是,git add 3.txt ,git stash保留现场 [root@132 studygit]# git add 3.txt [root@132 studygit]# git stash Saved working directory and index state WIP on master: fb5651a fix a bug daixuan.txt HEAD is now at fb5651a fix a bug daixuan.txt [root@132 studygit]# git status 这时候就看不到在在编辑的3.txt了 # On branch master # Your branch is ahead of 'origin/master' by 2 commits. # nothing to commit (working directory clean) 保留现场之后3.txt就消失了 [root@132 studygit]# ls david.txt linux.txt 恢复现场 [root@132 studygit]# git add linux.txt [root@132 studygit]# git commit -m "modify linux.txt" [master 566ec47] modify linux.txt 1 files changed, 1 insertions(+), 0 deletions(-) [root@132 studygit]# git status # On branch master # Your branch is ahead of 'origin/master' by 3 commits. # nothing to commit (working directory clean) [root@132 studygit]# git stash list stash@{0}: WIP on master: fb5651a fix a bug daixuan.txt stash@{1}: WIP on master: fb5651a fix a bug daixuan.txt [root@132 studygit]# git stash apply stash@{0} # On branch master # Your branch is ahead of 'origin/master' by 3 commits. # # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # new file: 3.txt # [root@132 studygit]# ls 这时候3.txt又出现了,因为恢复了现场 3.txt david.txt linux.txt
12. 远程分支
查看远程库信息,使用git remote -v 本地新建的分支如果不推送到远程,对其他人就是不可见的
查看远程分支 git ls-remote origin
[root@132 studygit]# git remote -v [email protected]:1350368559/studygit.git (fetch) [email protected]:1350368559/studygit.git (push) [root@132 studygit]# git ls-remote origin 9cfd640181585d4987b19c117077055ae9195de7HEAD 9cfd640181585d4987b19c117077055ae9195de7refs/heads/master 通过web页面新建dev2分支后,查看多了一个dev分支 [root@132 studygit]# git remote -v [email protected]:1350368559/studygit.git (fetch) [email protected]:1350368559/studygit.git (push) [root@132 studygit]# git ls-remote origin 9cfd640181585d4987b19c117077055ae9195de7HEAD 9cfd640181585d4987b19c117077055ae9195de7refs/heads/dev2 9cfd640181585d4987b19c117077055ae9195de7refs/heads/master
从本地推送分支到远程,使用git push origin dev(branch-name),如果推送失败,先用git pull抓取远程的新提交
[root@132 studygit]# git ls-remote origin 9cfd640181585d4987b19c117077055ae9195de7 HEAD 9cfd640181585d4987b19c117077055ae9195de7 refs/heads/master [root@132 studygit]# git branch dev [root@132 studygit]# git push origin dev Counting objects: 11, done. Compressing objects: 100% (7/7), done. Writing objects: 100% (9/9), 865 bytes, done. Total 9 (delta 0), reused 0 (delta 0) To [email protected]:1350368559/studygit.git * [new branch] dev -> dev [root@132 studygit]# git ls-remote origin 9cfd640181585d4987b19c117077055ae9195de7 HEAD 566ec47437479f722f25c1751f4ab89c8163963c refs/heads/dev 9cfd640181585d4987b19c117077055ae9195de7 refs/heads/master
在本地创建和远程分支对应的分支,把远程的分支checkout到本地来
git checkout -b dev origin/dev
git checkout -b branch-name origin/branch-name ,本地和远程分支的名称最好一致 ,从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。
由于dev2对应线上的老的master分支,而本地master分支已经更新并且push,所以dev2仍然是老的,此时推送本地dev到远程会报错,解决办法:
1、删除远程的dev2,重新建立
2、删除本地的git,重新clone线上的git
方法二的实际操作如下:
[root@132 studygit]# cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = [email protected]:1350368559/studygit.git [branch "master"] remote = origin merge = refs/heads/master [root@132 studygit]# cd .. [root@132 david]# rm -rf studygit/ 删除本地git仓库 [root@132 david]# git clone [email protected]:1350368559/studygit.git 重新克隆git仓库 Initialized empty Git repository in /home/david/studygit/.git/ remote: Counting objects: 18, done. remote: Compressing objects: 100% (11/11), done. remote: Total 18 (delta 1), reused 17 (delta 0), pack-reused 0 Receiving objects: 100% (18/18), done. Resolving deltas: 100% (1/1), done. [root@132 david]# cd studygit/ [root@132 studygit]# ls david.txt linux.txt [root@132 studygit]# git branch 此时获取了线上的最新的master分支 * master [root@132 studygit]# git checkout -b dev origin/dev Branch dev set up to track remote branch dev from origin. Switched to a new branch 'dev' [root@132 studygit]# git branch * dev master [root@132 studygit]# git checkout -b dev2 origin/dev2 Branch dev2 set up to track remote branch dev2 from origin. Switched to a new branch 'dev2' [root@132 studygit]# git branch dev * dev2 master
注:在web页面上创建分支和删除分支的方法
删除分支,首先点击branches
然后点击回收链接,
13. 标签管理
标签类似于快照功能,我们可以给版本库打一个标签,记录某个时刻库的状态。我们可以随时恢复到该状态。
git checkout master 先切到master分支上
git tag v1.0 给master打一个标签v1.0
查看所有的标签
git tag
tag是针对commit来打标签的,所以可以针对历史的commit来打tag
git log --pretty=oneline --abbrev-commit
git tag v0.9 46d3c1a
可以对标签进行描述
git tag -a v0.8 -m "tag just v1.1 and so on" 5aacaf4
删除标签:
git tag -d v0.8
推送指定标签到远程:
git push origin v1.0
推送所有标签:
git push --tag origin
如果本地删除了一个标签,远程也想要删除需要这样操作:
git tag v1.0 -d
git push origin :refs/tags/v1.0
[root@132 studygit]# git checkout master 切换到主分支 Switched to branch 'master' [root@132 studygit]# git branch 查看分支 dev dev2 * master [root@132 studygit]# git tag v1.0 把主分支目前代码打标签为发布版本V1.0 [root@132 studygit]# git tag 查看标签tag v1.0 [root@132 studygit]# git log --pretty=oneline --abbrev-commit 显示提交仓库的历史操作 566ec47 modify linux.txt fb5651a fix a bug daixuan.txt 5aff4d7 add a new linux.txt 9cfd640 you can use this to study git clone 66c9786 change david.txt 6a7ff00 add david.txt [root@132 studygit]# git tag v0.9 fb5651a 给指定提交代码的版本打tag [root@132 studygit]# git tag 查看标签tag v0.9 v1.0 [root@132 studygit]# git tag -a v0.8 -m 'tag just v1.1 and so on' 9cfd640 给标签tag添加说明 [root@132 studygit]# git tag v0.8 v0.9 v1.0 [root@132 studygit]# git tag -d v0.8 删除指定的tag Deleted tag 'v0.8' (was 558b563) [root@132 studygit]# git tag v0.9 v1.0 [root@132 studygit]# git push origin v1.0 推送指定标签到远程 Total 0 (delta 0), reused 0 (delta 0) To [email protected]:1350368559/studygit.git * [new tag] v1.0 -> v1.0 [root@132 studygit]# git push --tag origin 推送所有标签到远程 Total 0 (delta 0), reused 0 (delta 0) To [email protected]:1350368559/studygit.git * [new tag] v0.9 -> v0.9 [root@132 studygit]# git tag -d v1.0 删除指定的标签tag Deleted tag 'v1.0' (was 566ec47) [root@132 studygit]# git tag v0.9 [root@132 studygit]# git push origin:refs/tags/v1.0 删除远程的标签tag ssh: Could not resolve hostname origin: Name or service not known fatal: The remote end hung up unexpectedly [root@132 studygit]# git push origin :refs/tags/v1.0 To [email protected]:1350368559/studygit.git - [deleted] v1.0
14. 巧用别名
git commit 这个命令是不是有点长? 用别名可以提高我们的工作xiaol
git config --global alias.ci commit git ci = git commit
git config --global alias.co checkout git co =git checkout
git config --global alias.br branch git br=git branch
可以把配置文件拷贝到新的服务器上,方便使用:
[root@132 studygit]# git config --global alias.ci commit [root@132 studygit]# git config --global alias.co checkout [root@132 studygit]# cat /root/.gitconfig [user] name = daixuan email = [email protected] [alias] ci = commit co = checkout [root@132 studygit]# git config --global alias.lg "log --pretty=oneline --abbrev-commit" [root@132 studygit]# git lg 566ec47 modify linux.txt fb5651a fix a bug daixuan.txt 5aff4d7 add a new linux.txt 9cfd640 you can use this to study git clone 66c9786 change david.txt 6a7ff00 add david.txt [root@132 studygit]# 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@132 studygit]# git lg * 566ec47 - (HEAD, origin/master, origin/dev, origin/HEAD, master, dev) modify linux.txt (3 hours ago)* fb5651a - (v0.9) fix a bug daixuan.txt (4 hours ago) * 5aff4d7 - add a new linux.txt (6 hours ago) * 9cfd640 - (origin/dev2, dev2) you can use this to study git clone (7 hours ago) * 66c9786 - change david.txt (24 hours ago) * 6a7ff00 - add david.txt (24 hours ago) [root@132 studygit]# cat /root/.gitconfig [user] name = daixuan email = [email protected] [alias] ci = commit co = checkout lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
查看git别名使用命令
git config --list |grep alias
[root@132 studygit]# git config --list user.name=daixuan [email protected] alias.ci=commit alias.co=checkout alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* [email protected]:1350368559/studygit.git branch.master.remote=origin branch.master.merge=refs/heads/master branch.dev.remote=origin branch.dev.merge=refs/heads/dev branch.dev2.remote=origin branch.dev2.merge=refs/heads/dev2 [root@132 studygit]# git config --list | grep alisa [root@132 studygit]# git config --list | grep alias alias.ci=commit alias.co=checkout 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@132 studygit]#
查询log小技巧:
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"
取消别名
git config --global --unset alias.br
15. 搭建git服务器
github毕竟是公开的,而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的,只自己公司使用的。
yum install git //安装git
useradd -s /usr/bin/git-shell git //添加git用户,并且设置shell为/usr/bin/git-shell,目的是为了不让git用户远程登陆
cd /home/git
mkdir .ssh
touch .ssh/authorized_keys
chown -R git.git .ssh
chmod 600 .ssh/authorized_keys 其他用户不能读和写这个文件,只要authorized_keys能被属主用户读就行了
或者chmod 700 .ssh 其他用户包括root不能看和进这个.ssh这个目录,只允许属主用户进入该目录
定好存储git仓库的目录,比如 /data/gitroot
mkdir /data/gitroot
cd /data/gitroot
git init --bare sample.git // 会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾
chown -R git.git sample.git
[root@133 ~]# useradd -s /usr/bin/git-shell git [root@133 ~]# cd /home/git/ [root@133 git]# mkdir .ssh [root@133 git]# touch .ssh/authorized_keys [root@133 git]# chown -R git:git .ssh/ [root@133 git]# chmod 600 .ssh/authorized_keys [root@133 git]# mkdir /data/gitroot [root@133 git]# vim .ssh/authorized_keys [root@133 git]# ls -ld .ssh/ drwxr-xr-x 2 git git 4096 8月 31 02:51 .ssh/ [root@133 git]# ls -ld .ssh/authorized_keys -rw------- 1 git git 408 8月 31 02:51 .ssh/authorized_keys [root@133 git]# chmod 700 .ssh/ [root@133 git]# chmod 600 .ssh/authorized_keys [root@133 git]# usermod -s /bin/bash git 允许其他server通过git用户登录 [root@133 git]# usermod -s /usr/bin/git-shell git 拒绝其他server通过git用户登录 [root@133 git]# cd /data/gitroot/ [root@133 gitroot]# git init --bare sample.git 创建一个裸仓库 Initialized empty Git repository in /data/gitroot/sample.git/ [root@133 gitroot]# ls -lai 总用量 12 23593535 drwxr-xr-x 3 root root 4096 8月 31 03:01 . 23330817 drwxr-xr-x 5 root root 4096 8月 31 02:49 .. 23593536 drwxr-xr-x 7 root root 4096 8月 31 03:01 sample.git [root@133 gitroot]# cd sample.git/ [root@133 sample.git]# ll 总用量 32 drwxr-xr-x 2 root root 4096 8月 31 03:01 branches -rw-r--r-- 1 root root 66 8月 31 03:01 config -rw-r--r-- 1 root root 73 8月 31 03:01 description -rw-r--r-- 1 root root 23 8月 31 03:01 HEAD drwxr-xr-x 2 root root 4096 8月 31 03:01 hooks drwxr-xr-x 2 root root 4096 8月 31 03:01 info drwxr-xr-x 4 root root 4096 8月 31 03:01 objects drwxr-xr-x 4 root root 4096 8月 31 03:01 refs
在客户端上(自己pc)克隆远程仓库
以上操作是在git服务器上做的,平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时操作都是在我们自己的pc上做的。
首先要把客户端上的公钥放到git服务器上/home/git/.ssh/authorized_keys文件里
git clone git@ip:/data/gitroot/sample.git
[root@132 studygit]# ssh [email protected] Last login: Wed Aug 31 02:59:25 2016 from 112.65.140.132 fatal: What do you think I am? A shell? Connection to 112.65.140.133 closed. [root@132 studygit]# cd .. [root@132 david]# ls studygit [root@132 david]# git clone [email protected]:/data/gitroot/sample.git Initialized empty Git repository in /home/david/sample/.git/ warning: You appear to have cloned an empty repository. [root@132 david]# ls sample studygit [root@132 sample]# echo -e "234523456\nwetryu\n" > 1.txt [root@132 sample]# git add 1.txt [root@132 sample]# git commit -m "add new file 1.txt" [root@132 sample]# git push -u origin master Counting objects: 3, done. Writing objects: 100% (3/3), 227 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:/data/gitroot/sample.git * [new branch] master -> master Branch master set up to track remote branch master from origin.
此时就可以在当前目录下生成一个sample的目录,这个就是我们克隆的远程仓库了。进入到这里面,可以开发一些代码,然后push到远程。