温馨提示
此博客用于记录git常用的命令参数使用方法 遗忘或想不起来了可以来看一眼 所以写的并不详细、不适合初学者学习
环境说明
[[email protected] ~]# cat /etc/redhat-release CentOS release 6.10 (Final) [[email protected] ~]# 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
git的安装
[[email protected] ~]# yum -y install git
创建版本仓库
[[email protected] ~]# mkdir -p /usr/local/git/test [[email protected] ~]# cd /usr/local/git/test/ [[email protected] test]# git i nit Initialized empty Git repository in /usr/local/git/test/.git/
创建版本库后会在目录内生成一个隐藏目录.git 初始化一个空版本
[[email protected] 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 [[email protected] test]# cd .git/ [[email protected] . git]# ls branches config description HEAD hooks info objects refs
版本创建
[[email protected] test]# pwd /usr/local/git/test [root @Check1 test]# echo "version 1.0" >> code.txt 编辑一个文件或目录, 在里面添加内容 [[email protected] test]# git add code.txt 将此文件添加到缓存区 [roo [email protected] 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
再创建几个版本
[[email protected] test]# echo "version 2.0" >> code.txt [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "版本2.0" [master c94bcab] 版本2.0 1 files changed, 1 insertions(+), 0 deletions(-) [ [email protected] test]# echo "version 3.0" >> code.txt [[email protected] heck1 test]# git add code.txt [[email protected] test]# git commi t -m "版本3.0" [master 9f98991] 版本3.0 1 files changed, 1 inser tions(+), 0 deletions(-)
在git中创建版本记录只是记录一个修改后面的版本是依赖于前面的版本的
查看版本记录
[[email protected] test]# git log commit 9f9899114fd b0f59b8cd0cb3e009f33f2b7702e5 Author: wcy <goo [email protected]163.com> Date: Sun Dec 16 21:02:31 2018 +0800 版本3.0 commit c94bcab611218e72f9d806ee347dcfb1d12e73a3 Auth or: wcy <[email protected]163.com> Date: Sun Dec 16 21:01:58 2018 +0800 版本2.0 commit 0367a6a3bd901309e282dc630e6562b7f270e39b Author: wc y <[email protected]163.com> Date: Sun Dec 16 20:55:10 2018 +0800 版本1.0
版本回退
根据HEAD指针回退
HEAD指向当前版本
[[email protected] test]# git reset --hard HEAD^ 回退至上一个版本 HEAD is now at c94bcab 版本2.0
HEAD^指的是上一个版本 HEAD^^指的是上两个版本
上100个版本可以用 HEAD~100
上200个版本可以用 HEAD~200表示
根据版本序列号回退
[[email protected] test]# git reset --hard 9f9899114f HEAD is now at 9f98991 版本3.0
版本序列号在上面git log中查看git commit对应的字符串就是 回退的时候不用填写全部版本序列号,前几位即可
如果Git的版本号通过git log查看不到了可以通过git reflog查看操作记录
比如回退到版本3.0
[[email protected] test]# git reset --hard HEAD^ HEAD is now at c94bcab 版本2.0 [[email protected] test]# git log commit c94bcab611218e72f9d80 6ee347dcfb1d12e73a3 Author: wcy <[email protected]163.com> Date: Sun Dec 1 6 21:01:58 2018 +0800 版本2.0 commit 0367a6a3bd901309e282dc630 e6562b7f270e39b Author: wcy <[email protected]163.com> Date: Sun Dec 16 2 0:55:10 2018 +0800 版本1.0
查看操作记录
[[email protected] test]# git reflog c94bcab [email protected]{0}: H EAD^: updating HEAD 9f98991 [email protected]{1}: 9f9899114f: upd ating HEAD c94bcab [email protected]{2}: HEAD^: updating HEAD 9f989 91 [email protected]{3}: commit: 版本3.0 c94bcab [email protected]{4}: commit: 版本2. 0 0367a6a [email protected]{5}: commit (initial): 版本1.0 [[email protected] tes t]# git reset --hard 9f98991 回退到版本3.0 HEAD is now at 9f98991 版本3.0
可以看到上面版本3.0 最前面的就是之前版本序列号的前几位,然后再跟进这个序列号进行其他操作
9f98991 [email protected]{3}: commit: 版本3.0 序列号即:9f98991
工作区、暂存区和版本库
git是管理版本的修改
git commit时只是会把暂存区的修改提交到版本记录中(也就是创建一个版本记录)
撤销修改
丢弃工作区的改动
在工作区修改 code.txt 使用git status查看一下状态
[[email protected] test]# echo "version 4.0" >> code. txt [[email protected] 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 -- 删除工作区的修改
[[email protected] test]# git checkout -- code.txt [[email protected] test]# cat code.txt version 1.0 version 2.0 version 3.0
删除工作区的修改后 发现刚才添加的version 4.0 已经没有了
已经添加至暂存区未commit
[[email protected] test]# echo "version 4.0" >> code.tx t [[email protected] test]# git add code.txt [[email protected] k1 test]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: code.txt #
把暂存区的文件拉回工作区
[[email protected] test]# git reset HEAD code.txt Unstaged changes after reset: M code.txt
再把文件从工作区中checkout出去 丢弃工作区的改动
[[email protected] 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") [[email protected] test]# git checkout -- code.txt [[email protected] test]# git status # On branch mas ter nothing to commit (working directory clean)
已经commit
[[email protected] test]# echo "version 4.0" >> code.txt [[email protected] test]# cat code. txt version 1.0 version 2.0 version 3.0 version 4.0 [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "版本4.0" [master d61d2f2] 版本4.0 1 files changed, 1 insertions(+), 0 deletions(-)
进行版本回退
[[email protected] test]# git log --pretty=oneline d61d 2f2cfdf2e2feb0dec63227315fcb7c004ff9 版本4.0 9f98991 14fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e7 2f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e 6562b7f270e39b 版本1.0 [[email protected] test]# git reset --hard 9 f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 HEAD is now at 9f98991 版本3.0 [[email protected] test]# cat code.txt version 1.0 version 2.0 version 3.0
对比文件的不同
对比工作区和版本库的某个文件
[[email protected] 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 也就是工作区比上个版本多这一行
对比两个版本库中文件
[[email protected] 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
[[email protected] test]# echo "version 1.0" >> code2.txt [[email protected] 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 [email protected] test]# git add code2.txt [[email protected] test]# git co mmit -m "new code" [master e39c2ef] new code 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 code2.txt
rm直接删除工作区文件
[[email protected] test]# ls code2.txt code.txt [[email protected] test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y [[email protected] 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 -- 撤回删除
[[email protected] test]# git checkout -- code2.txt [[email protected] test]# ls code2.txt code.txt
git rm删除暂存区文件
[[email protected] test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y [[email protected] 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") [[email protected] test]# git rm code2.txt rm ‘code2.txt‘ [[email protected] 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>...
进行恢复
[[email protected] test]# git reset HEAD code2.txt Unstaged changes after reset: M code2.txt [[email protected] test]# git checkout -- code2.txt [[email protected] test]# ls code2.txt code.txt
git commit提交删除
[[email protected] test]# rm code2.txt rm:是否删除普通文件 "code2.txt"?y [[email protected] test]# git rm code2.txt rm ‘code2.txt‘ [[email protected] test]# git commit -m "DEL code2.txt" [master c88df5f] DEL code2.txt 1 files changed, 0 insertions(+), 1 deletions(-) delete mode 100644 code2.txt
恢复只能版本回退
[[email protected] test]# git reflog c88df5f [email protected]{0}: commit: DEL code2.txt e39c2ef [email protected]{1}: commit: new code 9f98991 [email protected]{2}: 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5: updating HEAD d61d2f2 [email protected]{3}: commit: 版本4.0 9f98991 [email protected]{4}: 9f98991: updating HEAD c94bcab [email protected]{5}: HEAD^: updating HEAD 9f98991 [email protected]{6}: 9f9899114f: updating HEAD c94bcab [email protected]{7}: HEAD^: updating HEAD 9f98991 [email protected]{8}: commit: 版本3.0 c94bcab [email protected]{9}: commit: 版本2.0 0367a6a [email protected]{10}: commit (initial): 版本1.0 [[email protected] test]# git reset --hard e39c2ef HEAD is now at e39c2ef new code [[email protected] test]# ls code2.txt code.txt
分支
查看当前的分支 默认只有一个 master 主分支
[[email protected] test]# git branch * master
创建一个分支
(创建并切换的过程很快 即创建一个指针 HEAD指向新的指针)
git checkout -b <分支名> 是创建并切换分支,是创建分支git branch <分支名>和切换分支 git checkout <分支名>的结合
[[email protected] test]# git checkout -b dev Switched to a new branch ‘dev‘ [[email protected] test]# git branch * dev master
新分支会有之前主分支的所有内容
[[email protected] test]# git log --pretty=oneline e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
在dev分支下编辑文件
[[email protected] test]# echo "version 4.0" >> code.txt [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "dev分支提交" [dev 80d1fe8] dev分支提交 1 files changed, 1 insertions(+), 0 deletions(-)
切换分支
dev分支工作完成,切换回master分支
[[email protected] test]# git checkout master Switched to branch ‘master‘
查看当前所在分支
[[email protected] test]# git branch dev * master
查看当前master分支下有没有dev下的版本记录 并没有
[[email protected] test]# git log --pretty=oneline e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
合并分支
(快速合并,直接把master指针指向dev的当前提交,所以速度也非常快)
[[email protected] test]# git merge dev Updating e39c2ef..80d1fe8 Fast-forward code.txt | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
再查看一下分支记录
[[email protected] test]# git log --pretty=oneline 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
合并后删除分支
(直接删除dev指针)
[[email protected] test]# git branch -d dev Deleted branch dev (was 80d1fe8). [[email protected] test]# git branch * master
解决冲突
现象:创建一个分支dev,在分支中修改code.txt文件并提交
[[email protected] test]# git checkout -b dev Switched to a new branch ‘dev‘ [[email protected] test]# echo "version 5.0" >> code.txt [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "版本5.0" [dev b001913] 版本5.0 1 files changed, 1 insertions(+), 0 deletions(-) [[email protected] 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文件并提交
[[email protected] test]# git checkout master Switched to branch ‘master‘ [[email protected] test]# git log --pretty=oneline 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0 [[email protected] test]# echo "version 5.0 in master" >> code.txt [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "版本5.0master分支提交" [master 064fe6a] 版本5.0master分支提交 1 files changed, 1 insertions(+), 0 deletions(-) [[email protected] test]# git log --pretty=oneline 064fe6aa5727578e412f52f693d4fd27e67f4c49 版本5.0master分支提交 80d1fe86dc1c5e1070018783b733729428dc99aa dev分支提交 e39c2efc2c787218c481a625893af153aed3e42b new code 9f9899114fdb0f59b8cd0cb3e009f33f2b7702e5 版本3.0 c94bcab611218e72f9d806ee347dcfb1d12e73a3 版本2.0 0367a6a3bd901309e282dc630e6562b7f270e39b 版本1.0
合并分支失败
(自动合并code.txt 合并时起了一个冲突 合并失败)
[[email protected] 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
[[email protected] 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已给标注是哪里起了冲突
[[email protected] 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
开始解决 (手动修改冲突的部分 并提交)在两个分支上面都有新的提交,并且编辑的还是同一个文件 才会有这样的冲突 冲突解决后需要一个手动的提交
[[email protected] test]# cat code.txt version 1.0 version 2.0 version 3.0 version 4.0 version 5.0 in master version 5.0 [[email protected] test]# git add code.txt [[email protected] test]# git commit -m "解决冲突" [master b893d4c] 解决冲突 [[email protected] 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
查看分支冲突的情况
[[email protected] 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分支
[[email protected] test]# git branch -d dev Deleted branch dev (was b001913). [[email protected] test]# git branch * master
稍候继续更新。。。感谢关注
原文地址:https://www.cnblogs.com/debugtest/p/10132843.html