标签:ict ons insert obj Fix 使用方法 local ges author
此博客用于记录git常用的命令参数使用方法
遗忘或想不起来了可以来看一眼
所以写的并不详细、不适合初学者学习
[root@Check1 ~]# cat /etc/redhat-release CentOS release 6.10 (Final) [root@Check1 ~]# uname -a Linux Check1.net 2.6.32-754.2.1.el6.x86_64 #1 SMP Fri Jul 13 12:50:12 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@Check1 ~]# yum -y install git
[root@Check1 ~]# mkdir -p /usr/local/git/test [root@Check1 ~]# cd /usr/local/git/test/ [root@Check1 test]# git i nit Initialized empty Git repository in /usr/local/git/test/.git/
创建版本库后会在目录内生成一个隐藏目录.git 初始化一个空版本
[root@Check1 test]# ls -al 总用量 12 drwxr-xr-x 3 root root 4096 12月 16 20:51 . drwxr-xr-x 3 root root 4096 12月 16 20:51 .. drwxr-xr-x 7 root root 4096 12月 16 20:51 .git [root@Check1 test]# cd .git/ [root@Check1 . git]# ls branches config description HEAD hooks info objects refs
[root@Check1 test]# pwd /usr/local/git/test [root @Check1 test]# echo "version 1.0" >> code.txt 编辑一个文件或目录, 在里面添加内容 [root@Check1 test]# git add code.txt 将此文件添加到缓存区 [roo t@Check1 test]# git commit -m "版本1.0" 提交此文件创建版本记录 -m "版 本说明信息" [master (root-commit) 0367a6a] 版本1.0 1 files changed, 1 ins ertions(+), 0 deletions(-) create mode 100644 code.txt
再创建几个版本
[root@Check1 test]# echo "version 2.0" >> code.txt [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "版本2.0" [master c94bcab] 版本2.0 1 files changed, 1 insertions(+), 0 deletions(-) [ root@Check1 test]# echo "version 3.0" >> code.txt [root@C heck1 test]# git add code.txt [root@Check1 test]# git commi t -m "版本3.0" [master 9f98991] 版本3.0 1 files changed, 1 inser tions(+), 0 deletions(-)
在git中创建版本记录只是记录一个修改后面的版本是依赖于前面的版本的
[root@Check1 test]# git log commit 9f9899114fd b0f59b8cd0cb3e009f33f2b7702e5 Author: wcy <goo dmoodwjl@163.com> Date: Sun Dec 16 21:02:31 2018 +0800 版本3.0 commit c94bcab611218e72f9d806ee347dcfb1d12e73a3 Auth or: wcy <goodmoodwjl@163.com> Date: Sun Dec 16 21:01:58 2018 +0800 版本2.0 commit 0367a6a3bd901309e282dc630e6562b7f270e39b Author: wc y <goodmoodwjl@163.com> Date: Sun Dec 16 20:55:10 2018 +0800 版本1.0
HEAD指向当前版本
[root@Check1 test]# git reset --hard HEAD^ 回退至上一个版本 HEAD is now at c94bcab 版本2.0
HEAD^指的是上一个版本 HEAD^^指的是上两个版本
上100个版本可以用 HEAD~100
上200个版本可以用 HEAD~200表示
[root@Check1 test]# git reset --hard 9f9899114f HEAD is now at 9f98991 版本3.0
版本序列号在上面git log中查看git commit对应的字符串就是 回退的时候不用填写全部版本序列号,前几位即可
如果Git的版本号通过git log查看不到了可以通过git reflog查看操作记录
比如回退到版本3.0
[root@Check1 test]# git reset --hard HEAD^ HEAD is now at c94bcab 版本2.0 [root@Check1 test]# git log commit c94bcab611218e72f9d80 6ee347dcfb1d12e73a3 Author: wcy <goodmoodwjl@163.com> Date: Sun Dec 1 6 21:01:58 2018 +0800 版本2.0 commit 0367a6a3bd901309e282dc630 e6562b7f270e39b Author: wcy <goodmoodwjl@163.com> Date: Sun Dec 16 2 0:55:10 2018 +0800 版本1.0
[root@Check1 test]# git reflog c94bcab HEAD@{0}: H EAD^: updating HEAD 9f98991 HEAD@{1}: 9f9899114f: upd ating HEAD c94bcab HEAD@{2}: HEAD^: updating HEAD 9f989 91 HEAD@{3}: commit: 版本3.0 c94bcab HEAD@{4}: commit: 版本2. 0 0367a6a HEAD@{5}: commit (initial): 版本1.0 [root@Check1 tes t]# git reset --hard 9f98991 回退到版本3.0 HEAD is now at 9f98991 版本3.0
可以看到上面版本3.0 最前面的就是之前版本序列号的前几位,然后再跟进这个序列号进行其他操作
9f98991 HEAD@{3}: commit: 版本3.0 序列号即:9f98991
git是管理版本的修改
git commit时只是会把暂存区的修改提交到版本记录中(也就是创建一个版本记录)
在工作区修改 code.txt 使用git status查看一下状态
[root@Check1 test]# echo "version 4.0" >> code. txt [root@Check1 test]# git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file> ..." to discard changes in working directory) # # modified: c ode.txt # no changes added to commit (use "git add" and/or "git commit -a")
可以看到已修改文件 code.txt
可以操作的选项是
1、git add 提交至暂存区
2、git checkout -- 删除工作区的修改
[root@Check1 test]# git checkout -- code.txt [root@Check1 test]# cat code.txt version 1.0 version 2.0 version 3.0
删除工作区的修改后 发现刚才添加的version 4.0 已经没有了
[root@Check1 test]# echo "version 4.0" >> code.tx t [root@Check1 test]# git add code.txt [root@Chec k1 test]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: code.txt #
把暂存区的文件拉回工作区
[root@Check1 test]# git reset HEAD code.txt Unstaged changes after reset: M code.txt
再把文件从工作区中checkout出去 丢弃工作区的改动
[root@Check1 test]# git status # On branch master # Changed but not updated: # (use "git add <file>..." to update what wi ll be committed) # (use "git checkout -- <file>..." to discard cha nges in working directory) # # modified: code.txt # no changes adde d to commit (use "git add" and/or "git commit -a") [root@Check1 test]# git checkout -- code.txt [root@Check1 test]# git status # On branch mas ter nothing to commit (working directory clean)
[root@Check1 test]# echo "version 4.0" >> code.txt [root@Check1 test]# cat code. txt version 1.0 version 2.0 version 3.0 version 4.0 [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "版本4.0" [master d61d2f2] 版本4.0 1 files changed, 1 insertions(+), 0 deletions(-)
进行版本回退
[root@Check1 test]# git log --pretty=oneline d61d 2f2cfdf2e2feb0dec63227315fcb7c004ff9 版本4.0 9f98991 14fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e7 2f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e 6562b7f270e39b 版本1.0 [root@Check1 test]# git reset --hard 9 f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 HEAD is now at 9f98991 版本3.0 [root@Check1 test]# cat code.txt version 1.0 version 2.0 version 3.0
[root@Check1 test]# git diff HEAD^ -- code.txt diff - -git a/code.txt b/code.txt index d1f9bad..69afb9a 10064 4 --- a/code.txt +++ b/code.txt @@ -1,2 +1,3 @@ version 1.0 version 2.0 +version 3.0
---为三个版本 HEAD^
+++为工作区的版本
结果为+version 3.0 也就是工作区比上个版本多这一行
[root@Check1 test]# git diff HEAD~1 HEAD~2 -- code.t xt diff --git a/code.txt b/code.txt index d1f9bad..7 c8de03 100644 --- a/code.txt +++ b/code.txt @@ -1,2 +1 @@ version 1.0 -version 2.0
由此可以看出---为前者 +++为后者
可看出 上个版本比上上个版本多一行 version 2.0
新建一个测试文件code2.txt
[root@Check1 test]# echo "version 1.0" >> code2.txt [root@Check1 test]# git status # On branch master # Un tracked files: # (use "git add <file>..." to include in what will be committed) # # code2.txt nothing added to c ommit but untracked files present (use "git add" to track) [r oot@Check1 test]# git add code2.txt [root@Check1 test]# git co mmit -m "new code" [master e39c2ef] new code 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 code2.txt
[root@Check1 test]# ls code2.txt code.txt [root@Check1 test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y [root@Check1 test]# git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: code2.txt # no changes added to commit (use "git add" and/or "git commit -a")
删除后可以通过git checkout -- 撤回删除
[root@Check1 test]# git checkout -- code2.txt [root@Check1 test]# ls code2.txt code.txt
[root@Check1 test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y [root@Check1 test]# git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: code2.txt # no changes added to commit (use "git add" and/or "git commit -a") [root@Check1 test]# git rm code2.txt rm ‘code2.txt‘ [root@Check1 test]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: code2.txt #
删除后可以通过
1、git reset HEAD <file>...
2、git checkout -- <file>...
进行恢复
[root@Check1 test]# git reset HEAD code2.txt Unstaged changes after reset: M code2.txt [root@Check1 test]# git checkout -- code2.txt [root@Check1 test]# ls code2.txt code.txt
[root@Check1 test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y [root@Check1 test]# git rm code2.txt rm ‘code2.txt‘ [root@Check1 test]# git commit -m "DEL code2.txt" [master c88df5f] DEL code2.txt 1 files changed, 0 insertions(+), 1 deletions(-) delete mode 100644 code2.txt
恢复只能版本回退
[root@Check1 test]# git reflog c88df5f HEAD@{0}: commit: DEL code2.txt e39c2ef HEAD@{1}: commit: new code 9f98991 HEAD@{2}: 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5: updating HEAD d61d2f2 HEAD@{3}: commit: 版本4.0 9f98991 HEAD@{4}: 9f98991: updating HEAD c94bcab HEAD@{5}: HEAD^: updating HEAD 9f98991 HEAD@{6}: 9f9899114f: updating HEAD c94bcab HEAD@{7}: HEAD^: updating HEAD 9f98991 HEAD@{8}: commit: 版本3.0 c94bcab HEAD@{9}: commit: 版本2.0 0367a6a HEAD@{10}: commit (initial): 版本1.0 [root@Check1 test]# git reset --hard e39c2ef HEAD is now at e39c2ef new code [root@Check1 test]# ls code2.txt code.txt
查看当前的分支 默认只有一个 master 主分支
[root@Check1 test]# git branch * master
(创建并切换的过程很快 即创建一个指针 HEAD指向新的指针)
git checkout -b <分支名> 是创建并切换分支,是创建分支git branch <分支名>和切换分支 git checkout <分支名>的结合
[root@Check1 test]# git checkout -b dev Switched to a new branch ‘dev‘ [root@Check1 test]# git branch * dev master
新分支会有之前主分支的所有内容
[root@Check1 test]# git log --pretty=oneline e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
在dev分支下编辑文件
[root@Check1 test]# echo "version 4.0" >> code.txt [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "dev分支提交" [dev 80d1fe8] dev分支提交 1 files changed, 1 insertions(+), 0 deletions(-)
dev分支工作完成,切换回master分支
[root@Check1 test]# git checkout master Switched to branch ‘master‘
查看当前所在分支
[root@Check1 test]# git branch dev * master
查看当前master分支下有没有dev下的版本记录 并没有
[root@Check1 test]# git log --pretty=oneline e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
(快速合并,直接把master指针指向dev的当前提交,所以速度也非常快)
[root@Check1 test]# git merge dev Updating e39c2ef..80d1fe8 Fast-forward code.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
再查看一下分支记录
[root@Check1 test]# git log --pretty=oneline 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
(直接删除dev指针)
[root@Check1 test]# git branch -d dev Deleted branch dev (was 80d1fe8). [root@Check1 test]# git branch * master
现象:创建一个分支dev,在分支中修改code.txt文件并提交
[root@Check1 test]# git checkout -b dev Switched to a new branch ‘dev‘ [root@Check1 test]# echo "version 5.0" >> code.txt [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "版本5.0" [dev b001913] 版本5.0 1 files changed, 1 insertions(+), 0 deletions(-) [root@Check1 test]# git log --pretty=oneline b001913d7d2959b1ef47ab9af50c84d105d3a4c0 版本5.0 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
提交后切换到master分支,也修改code.txt文件并提交
[root@Check1 test]# git checkout master Switched to branch ‘master‘ [root@Check1 test]# git log --pretty=oneline 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0 [root@Check1 test]# echo "version 5.0 in master" >> code.txt [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "版本5.0master分支提交" [master 064fe6a] 版本5.0master分支提交 1 files changed, 1 insertions(+), 0 deletions(-) [root@Check1 test]# git log --pretty=oneline 064fe6aa5727578e412f52f693d4fd27e67f4c49 版本5.0master分支提交 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
(自动合并code.txt 合并时起了一个冲突 合并失败)
[root@Check1 test]# git merge dev Auto-merging code.txt CONFLICT (content): Merge conflict in code.txt Automatic merge failed; fix conflicts and then commit the result.
找到冲突的文件,可以通过git status
[root@Check1 test]# git status # On branch master # Unmerged paths: # (use "git add/rm <file>..." as appropriate to mark resolution) # # both modified: code.txt 双方修改 冲突 # no changes added to commit (use "git add" and/or "git commit -a")
查看这个文件,git已给标注是哪里起了冲突
[root@Check1 test]# cat code.txt version 1.0 version 2.0 version 3.0 version 4.0 <<<<<<< HEAD version 5.0 in master ======= version 5.0 >>>>>>> dev
开始解决 (手动修改冲突的部分 并提交)在两个分支上面都有新的提交,并且编辑的还是同一个文件 才会有这样的冲突 冲突解决后需要一个手动的提交
[root@Check1 test]# cat code.txt version 1.0 version 2.0 version 3.0 version 4.0 version 5.0 in master version 5.0 [root@Check1 test]# git add code.txt [root@Check1 test]# git commit -m "解决冲突" [master b893d4c] 解决冲突 [root@Check1 test]# git log --pretty=oneline b893d4cfa12894c24f255ce25590f47f9ca62e15 解决冲突 064fe6aa5727578e412f52f693d4fd27e67f4c49 版本5.0master分支提交 b001913d7d2959b1ef47ab9af50c84d105d3a4c0 版本5.0 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
[root@Check1 test]# git log --graph --pretty=oneline * b893d4cfa12894c24f255ce25590f47f9ca62e15 解决冲突 |\ | * b001913d7d2959b1ef47ab9af50c84d105d3a4c0 版本5.0 * | 064fe6aa5727578e412f52f693d4fd27e67f4c49 版本5.0master分支提交 |/ * 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 * e39c2efc2c787218c481a625893af153aed3e42b new code * 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 * c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 * 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
解决冲突后再删除dev分支
[root@Check1 test]# git branch -d dev Deleted branch dev (was b001913). [root@Check1 test]# git branch * master
You have new mail in /var/spool/mail/root提示的原因与解决办法
标签:ict ons insert obj Fix 使用方法 local ges author
原文地址:https://www.cnblogs.com/debugtest/p/10132843.html