美文网首页
2019-03-07 Git高级管理

2019-03-07 Git高级管理

作者: 阿丧小威 | 来源:发表于2019-03-09 11:18 被阅读0次

1. git checkout命令

  • git checkout命令:用于切换分支
  • git checkout -- file.ext撤销对文件的修改
git checkout hotfix after checkout

HEAD由Master转到Hotfix上了

git checkout HEAD~2

后面加了HEAD~2,提交后,HEAD指向了该分支前两次提交的那里

2. checkout

  • Checkout一个文件和带文件路径git reset非常像,除了它更改的是工作目录而不是缓存区。不像提交层面的checkout命令,它不会移动HEAD引用,也就是你不会切换到别的分支上去。
  • 如果你缓存并且提交了checkout的文件,它具备将某个文件回撤到之前版本的效果。注意它撤销了这个文件后面所有的更改,而git revert命令只撤销某个特定提交的更改。
checkout file

示例:

[root@localhost test]# vi index.html 
<h1>hello world</h1>
test checkout file    ---新添加的修改项
:wq
[root@localhost test]# git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   index.html    ---提示已修改

no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost test]# git checkout -- index.html     ---撤销文件
[root@localhost test]# git status
On branch master
nothing to commit, working directory clean    ---变回没修改的状态了

3. git reset

  • --soft:缓存区和工作目录都不会被改变
  • --mixed:默认选项。缓存区和你指定的提交同步,但工作目录不受影响
  • --hard:缓存区和工作目录都同步到你指定的提交
git reset HEAD~2 git reset HEAD~2

HEAD~2往前撤了两次,后面的两个不见了

reset参数影响

--soft只回撤本地版本库,--mixed只回撤暂存区和本地版本库,--hard全部都清了。
示例:

[root@localhost test]# git status
On branch master
nothing to commit, working directory clean
[root@localhost test]# ll
total 20-
-rw-r--r--. 1 root root  7 Mar  2 22:30 about2.html
-rw-r--r--. 1 root root  9 Mar  2 22:30 about.html
-rw-r--r--. 1 root root 21 Mar  2 23:12 index.html
-rw-r--r--. 1 root root  5 Mar  2 20:38 news.html
-rw-r--r--. 1 root root 22 Mar  2 20:43 test.html
[root@localhost test]# vi index.html 
<h1>hello world</h1>
2333333    ---新添加的
:wq
[root@localhost test]# vi news.html 
news
abbbbbb    ---新添加内容
:wq
[root@localhost test]# git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   index.html
    modified:   news.html

no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost test]# git add news.html 
[root@localhost test]# git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)    ---缓存区

    modified:   news.html

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)    ---工作目录

    modified:   index.html
[root@localhost test]# git log
commit 96c9ee3183d8589922448da51a66554037f4c76b
Author: zheng <zheng@qq.com>
Date:   Sat Mar 2 22:16:16 2019 +0800

    about2

commit 06c81edf9d8db4ab95c1558f8a551ac81de5a659
Author: zheng <zheng@qq.com>
Date:   Sat Mar 2 21:40:40 2019 +0800

    about

commit 8880be9fc12c00fe8f5a26a5f87e6b6af193ac7b
Author: zheng <zheng@qq.com>
Date:   Sat Mar 2 20:43:44 2019 +0800

    test

commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date:   Sat Mar 2 20:41:12 2019 +0800

    news

commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date:   Sat Mar 2 20:33:08 2019 +0800

    first commit
[root@localhost test]# git reset --hard 8ce4e28510f0bdf1153c064e507b11e2b2052744    ---缓存区和工作目录都回滚到news那里
HEAD is now at 8ce4e28 news
[root@localhost test]# git status
On branch master
nothing to commit, working directory clean    ---工作目录和暂存区都被清空了
[root@localhost test]# git log    ---只剩下news和news之前的提交了
commit 8ce4e28510f0bdf1153c064e507b11e2b2052744
Author: zheng <zheng@qq.com>
Date:   Sat Mar 2 20:41:12 2019 +0800

    news

commit b613f505626d37c74b262ba5a345ef41740785fb
Author: zheng <zheng@qq.com>
Date:   Sat Mar 2 20:33:08 2019 +0800

    first commit

4. 文件层操作

  • 当检测到文件路径时,git reset将缓存区同步到你指定的那个提交。比如,下面这个命令会将倒数第二个提交中的foo.py加入到缓存区中,供下一个提交使用。
  • git reset HEAD~2 foo.py
  • 运行git reset HEAD foo.py会将当前的foo.py从缓存区中移除出去,而不会影响工作目录中对foo.py的更改。
  • --soft、--mixed和--hard对文件层面的git reset毫无作用,因为缓存区中的文件一定会变化,而工作目录中的文件一定不变。
reset file

reset会把文件拉到暂存区里

使用场景

5. reflog

  • git reflog命令分析你所有分支的头指针的日志来查找你在重写历史上可能丢失的提交。

相关文章

网友评论

      本文标题:2019-03-07 Git高级管理

      本文链接:https://www.haomeiwen.com/subject/wphkpqtx.html