1.1.0git创建密钥
$ ssh-keygen -t rsa //创建密钥 $ cat ~/.ssh/id_rsa.pub //查看密钥 git clone ssh://git@192.168.199.154:52113/yunda/router.git //克隆 git add aa.php //添加 touch ceshi.php 创建php文件 git status //查看状态 git commit -m "php用例"; //提交并添加注释 git push //推 git pull //拉取
1.1.1git提交数据
我们可以简单的把工作目录理解成是一个被git服务程序管理的目录,githi时刻的追踪目录类文件的改动,另外在安装好了git服务程序后,默认就会城建好了一个叫做master的分支,我们直接可以提交数据了。
[root@git-node1~]# mkdir sutongwang #创建本地工作目录 [root@git-node1~]# cd sutongwang/ #进入本地工作目录 [root@git-node1 sutongwang]# git init #初始为git工作目录 Initialized empty Git repository in /root/sutongwang/.git/ [root@git-node1 sutongwang]# touch readme.txt #创建文件 [root@git-node1 sutongwang]# git status #查看git状态 #Untracked files: # (use "git add <file>..." toinclude in what will be committed) # readme.txt #发现新建的readme.txt文件 … [root@git-node1sutongwang]# git add readme.txt #git添加文件至暂存区 [root@git-node1 sutongwang]# git status #再次查看状态 #Changes to be committed: # (use "git rm --cached <file>..." to unstage) # new file: readme.txt#发现新建立的文件readme.txt已经变绿 … [root@git-node1 sutongwang]# git commit -m "the first commit" #git cmmit提交暂存取文件至git版本仓库 [master (root-commit) dde9e40] the first commit1 file changed, 1 insertion(+) create mode 100644 readme.txt
1.1.2git移除数据
有些时候会把已经添加早暂存区的文件移除,但仍希望文件在工作目录中不丢失,换句话说,就是把问价从追踪清单中删除。
[root@git-node1 sutongwang]# touch database #建立文件 [root@git-node1 sutongwang]# git add database #添加文件至暂存区 [root@git-node1 sutongwang]# git status #查看当前git状态 # On branch master # Your branch is ahead of 'origin/master' by 4 commits. # (use "git push" to publish your local commits) # # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: database [root@git-node1 sutongwang]# git rm --cached database #将文件从git暂存区域的追踪列表移除(并不会删除当前工作目录内的数据文件) rm 'database' [root@git-node1 sutongwang]# git status #此时文件已经是未追踪状态了 # On branch master #Untracked files: # (use "git add <file>..." to include in what will be committed) # # database no changes added to commit (use "git add" and/or "git commit -a") #如果想将文件数据从git暂存区和工作目录一起删除,可以做如下操作。 [root@git-node1 sutongwang]# git add database #再将database文件提交到git暂存区 [root@git-node1 sutongwang]# git rm -f database #但如果在删除之前数据文件已经被放入到暂存区域的话,git会担心你误删未提交的文件而报错信息,此时可追加强制删除-f参数。 rm 'database' [root@git-node1 sutongwang]# ls #查看工作区也没database文件 LICENSE deployhelp.md readme.txt [root@git-node1 sutongwang]# git status #查看当前状态 # Onbranch master no changes added to commit (use "git add" and/or "git commit -a")
1.1.3git移动数据
[root@git-node1 sutongwang]# git mv readme.txt test.txt #git如果要修改文件名称,则使用git mv命令 [root@git-node1 sutongwang]# git status #查看状态发现下次提交会有一个改名操作 # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: readme.txt -> test.txt [root@git-node1 sutongwang]# git commit -m "changed name" #提交到git版本仓库 [master 88f3791] changed name 2 files changed, 31 deletions(-) delete mode 100644 1 rename readme.txt => test.txt (100%) #其实可以如下方法改名 [root@git-node1 sutongwang]# mv test.txt readme.txt #先修改名称 [root@git-node1 sutongwang]# git rm test.txt #然后删除git版本仓库内的文件快照 rm 'test.txt' [root@git-node1 sutongwang]# git add readme.txt #最后再将新的文件添加进入 [root@git-node1 sutongwang]# git commit -m "changed the file name" #提交至git版本仓库 [master 2caa209] changed the file name 1 file changed, 0 insertions(+), 0 deletions(-) rename test.txt => readme.txt (100%)
1.1.4git的历史记录:
[root@git-node1 sutongwang]# git log #查看提交历史记录 commit 2caa2090efa1aaf5c32524a13c500c1524e0a5ee Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:32:53 2016 +0800 changed the file name commit 88f379175b5ead7e0d84e47bd6db6f5a3b072921 Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:31:03 2016 +0800 changed name commit 76c486fcf5d70b6a443ba9e7dad209c6722c8bee Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:22:39 2016 +0800 tet [root@git-node1 sutongwang]# git log -2 #查看最近几条记录 commit 2caa2090efa1aaf5c32524a13c500c1524e0a5ee Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:32:53 2016 +0800 changed the file name commit 88f379175b5ead7e0d84e47bd6db6f5a3b072921 Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:31:03 2016 +0800 changed name [root@git-node1 sutongwang]# [root@git-node1 sutongwang]# git log -p -1 #-p显示每次提交的内容差异,例如仅查看最近一次差异 commit 2caa2090efa1aaf5c32524a13c500c1524e0a5ee Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:32:53 2016 +0800 changed the file name diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..a9b574e --- /dev/null +++ b/readme.txt @@ -0,0 +1,3 @@ +1 hehe +Create new mode two +Create new branch is linux diff --git a/test.txt b/test.txt deleted file mode 100644 index a9b574e..0000000 --- a/test.txt +++ /dev/null @@ -1,3 +0,0 @@ -1 hehe -Create new mode two -Create new branch is linux [root@git-node1 sutongwang]# git log --stat -2 #--stat简要显示数据增改行数,这样能够看到提交中修改过的内容,对文件添加或移动的行数,并在最后列出所有增减行的概要信息 commit 2caa2090efa1aaf5c32524a13c500c1524e0a5ee Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:32:53 2016 +0800 changed the file name readme.txt | 3 +++ test.txt| 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) commit 88f379175b5ead7e0d84e47bd6db6f5a3b072921 Author: sutongwang <sutongwang@mail.com> Date: Sat Jan 16 18:31:03 2016 +0800 changed name 1| 31 ------------------------------- readme.txt |3 --- test.txt| 3 +++ 3 files changed, 3 insertions(+), 34 deletions(-) [root@git-node1 sutongwang]# git log --pretty=oneline #--pretty根据不同的格式展示提交的历史信息 2caa2090efa1aaf5c32524a13c500c1524e0a5ee changed the file name 88f379175b5ead7e0d84e47bd6db6f5a3b072921 changed name 76c486fcf5d70b6a443ba9e7dad209c6722c8bee tet [root@git-node1 sutongwang]# git log --pretty=fuller -2 #以更详细的模式输出提交的历史记录 commit 2caa2090efa1aaf5c32524a13c500c1524e0a5ee Author: sutongwang <sutongwang@mail.com> AuthorDate: Sat Jan 16 18:32:53 2016 +0800 Commit: sutongwang <sutongwang@mail.com> CommitDate: Sat Jan 16 18:32:53 2016 +0800 changed the file name commit 88f379175b5ead7e0d84e47bd6db6f5a3b072921 Author: sutongwang <sutongwang@mail.com> AuthorDate: Sat Jan 16 18:31:03 2016 +0800 Commit: sutongwang <sutongwang@mail.com> CommitDate: Sat Jan 16 18:31:03 2016 +0800 changed name 还可以使用format参数来指定具体的输出格式,这样非常便于后期编程的提取分析哦,常用的格式有: %s 提交说明。 %cd 提交日期。 %an 作者的名字。 %cn 提交者的姓名。 %ce 提交者的电子邮件。 %H 提交对象的完整SHA-1哈希字串。 %h 提交对象的简短SHA-1哈希字串。 %T 树对象的完整SHA-1哈希字串。 %t 树对象的简短SHA-1哈希字串。 %P 父对象的完整SHA-1哈希字串。 %p 父对象的简短SHA-1哈希字串。 %ad 作者的修订时间。 [root@git-node1 sutongwang]# git log --pretty=fomat:"%h %cn" #查看当前所有提交记录的简短SHA-1哈希字串与提交着的姓名 fomat:2caa209 sutongwang fomat:88f3791 sutongwang fomat:76c486f sutongwang
1.1.5git还原数据
[root@git-node1 sutongwang]# echo "Git is a version control system" >> readme.txt #追加一段话 [root@git-node1 sutongwang]# git add readme.txt #添加至暂存区 [root@git-node1 sutongwang]# git commit -m "introduction software" #提交至git版本仓库 [master 4bf5b29] introduction software 1 file changed, 1 insertion(+) 此时觉得写得不妥,想还原某一次提交的文件快照 [root@git-node1 sutongwang]# git log --pretty=oneline #提交的历史信息 4bf5b296db9678f1851b896ed040fe37e6d7efb5 introduction software 2caa2090efa1aaf5c32524a13c500c1524e0a5ee changed the file name 88f379175b5ead7e0d84e47bd6db6f5a3b072921 changed name Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录,而上一个提交版本会叫HEAD^,上上一个版本则会叫做HEAD^^,当然一般会用HEAD~5来表示往上数第五个提交版本。 [root@git-node1 sutongwang]# git reset --hard HEAD^ #还原历史提交版本上一次 HEAD is now at 2caa209 changed the file name [root@git-node1 sutongwang]# cat readme.txt #查看文件内容(已经还原) Create new branch is linux 刚刚的操作实际上就是改变了一下HEAD版本指针的位置,就是你将HEAD指针放在那里,那么你的当前工作版本就会定位在那里,要想把内容再还原到最新提交的版本,先看查看下提交版本号 [root@git-node1 sutongwang]# git log --pretty=oneline 2caa2090efa1aaf5c32524a13c500c1524e0a5ee changed the file name 88f379175b5ead7e0d84e47bd6db6f5a3b072921 changed name 怎么搞得?竟然没有了Introduction software这个提交版本记录? 原因很简单,因为我们当前的工作版本是历史的一个提交点,这个历史提交点还没有发生过Introduction software更新记录,所以当然就看不到了,要是想“还原到未来”的历史更新点,可以用git reflog命令来查看所有的历史记录: [root@git-node1 sutongwang]# git reflog #查看未来历史更新点 2caa209 HEAD@{0}: reset: moving to HEAD^ 4bf5b29 HEAD@{1}: commit: introduction software 2caa209 HEAD@{2}: commit: changed the file name [root@git-node1 sutongwang]# git reset --hard 4bf5b29 #找到历史还原点的SHA-1值后,就可以还原(值不写全,系统会自动匹配) HEAD is now at 4bf5b29 introduction software [root@git-node1 sutongwang]# cat readme.txt #查看文件内容(已经还原) Create new branch is linux Git is a version control system 如是只是想把某个文件内容还原,就不必这么麻烦,直接用git checkout命令就可以的,先写一段话 [root@git-node1 sutongwang]# echo "Some mistkes words" >> readme.txt [root@git-node1 sutongwang]# cat readme.txt #查看内容 Git is a version control system Some mistkes words 我们突然发现不应该写一句话的,可以手工删除(当内容比较多的时候会很麻烦),还可以将文件内容从暂存区中恢复 [root@git-node1 sutongwang]# git checkout -- readme.txt [root@git-node1 sutongwang]# cat readme.txt Git is a version control system 这其中是有一套规则,如果暂存区中有该文件,则直接从暂存区恢复,如果暂存区没有该文件,则将还原成最近一次文件提交时的快照。
1.1.6创建git分支
[root@git-node1 sutongwang]# git branch linux #创建分支 [root@git-node1 sutongwang]# git checkout linux #切换分支 Switched to branch 'linux' [root@git-node1 sutongwang]# git branch #查看当前分支情况,当前分支前有*号 * linux master [root@git-node1 sutongwang]# echo "Create new branch is linux" >> readme.txt #我们对文件追加一行字符串 [root@git-node1 sutongwang]# git add readme.txt #提交到暂存区 [root@git-node1 sutongwang]# git commit -m "new branch" #提交到git版本仓库 [linux 8bf5757] new branch 1 file changed, 1 insertion(+) [root@git-node1 sutongwang]# git checkout master #我们在提交文件后再切回master分支 Switched to branch 'master' [root@git-node1 sutongwang]# cat readme.txt #查看文件内容,发现并没有新追加的字符串1 hehe
1.1.7git合并分支
现在,我们想把linux的工作成果合并到master分支上,则可以使用“git merge“ 命令来讲指定的分支与当前的分支合并
[root@git-node1 sutongwang]# git branch #查看是否在master分支 linux * master [root@git-node1 sutongwang]# git merge linux #合并Linux分支至master Updating 185d668..8bf5757 Fast-forward readme.txt | 1 + 1 file changed, 1 insertion(+) [root@git-node1 sutongwang]# cat readme.txt #查看合并后的readme文件1 hehe Create new branch is linux [root@git-node1 sutongwang]# git branch -d linux #确认合并完成后,可以放心地删除Linux分支 Deleted branch linux (was 8bf5757). [root@git-node1 sutongwang]# git branch #删除后,查看branch,只剩下master分支了 *master
1.1.8git分支冲突
[root@git-node1 sutongwang]# git checkout -b linux #创建分支并切换到该分支 [root@git-node1 sutongwang]# git branch #查看分支 * linux master [root@git-node1 sutongwang]# vim readme.txt #编辑readme文件 [root@git-node1 sutongwang]# git add readme.txt #在Linux分支添加readme至暂存区 [root@git-node1 sutongwang]# git commit -m "create two" #在Linux分支提交readme [linux 13a42ad] create two 1 file changed, 1 insertion(+), 2 deletions(-) [root@git-node1 sutongwang]# git checkout master #切换到master分支 Switched to branch 'master' [root@git-node1 sutongwang]# git branch #查看是否切换至master分支 linux * master [root@git-node1 sutongwang]# vim readme.txt #编在master分支上修改readme文件同一行的内容 [root@git-node1 sutongwang]# git add readme.txt #添加至暂存区 [root@git-node1 sutongwang]# git commit -m 'create to master' #提交至Git版本仓库 [master 75bd55c] create to master 1 file changed, 1 insertion(+) [root@git-node1 sutongwang]# git merge linux #合并Linux分支(冲突) Auto-merging readme.txt CONFLICT (content): Merge conflict in readme.txt Automatic merge failed; fix conflicts and then commit the result. #那么此时,我们在master与linux分支上都分别对中readme文件进行了修改并提交了,那这种情况下Git就没法再为我们自动的快速合并了,它只能告诉我们readme文件的内容有冲突,需要手工处理冲突的内容后才能继续合并 [root@git-node1 sutongwang]# cat readme.txt #冲突内容如下 #Git用< <<<<<<,=======,>>>>>>>分割开了各个分支冲突的内容,我们需要手工的删除这些符号,并将内容修改 1 hehe <<<<<<< HEAD ======= >>>>>>> linux Create new branch is linux [root@git-node1 sutongwang]# git add readme.txt #再次添加至暂存区 [root@git-node1 sutongwang]# git commit -m 'config' #提交至git版本仓库 [master eb9bb83] config [root@git-node1 sutongwang]# git branch -d linux #最后删除Linux分支结束 Deleted branch linux (was 13a42ad). [root@git-node1 sutongwang]# git branch #检查是否删除完毕 * master
转载请注明转自:运达's blog 原文地址:http://www.yunda51.com/1762.html