Checkout

checkout命令用于从历史提交(或者暂存区域)中拷贝文件到工作目录,也可用于切换分支。 ![](./_image/2016-07-14 21-26-37.jpg?r=49) ![](./_image/2016-07-14 21-15-47.jpg?r=49&f=2) 匿名分支:如果既没有指定文件名,也没有指定分支名,而是一个标签、远程分支、SHA-1值或者是像 master~3 类似的东西,就得到一个匿名分支,称作 detached HEAD(被分离的 HEAD 标识)。

![](./_image/2016-07-14 21-44-06.jpg?r=56)

当HEAD处于分离状态(不依附于任一分支)时,提交操作可以正常进行,但是不会更新任何已命名的分支。(你可以认为这是在更新一个匿名分支。)一旦此后你切换到别的分支,比如说 master,那么这个提交节点(可能)再也不会被引用到,然后就会被丢弃掉了。注意这个命令之后就不会有东西引用 2eecb。详细查看:visual-git-guide#detached 但是,如果你想保存这个状态,可以用命令 git checkout -b name 来创建一个新的分支。 ![](./_image/2016-07-14 21-45-50.jpg?r=56)

Log

Description : Shows the commit logs. The command takes options applicable to the git rev-list command to control what is shown and how, and options applicable to the git diff-* commands to control how the changes each commit introduces are shown. git log [options] [revision range] [path]

常用命令整理如下:

  • 查看日志:git log
  • 查看日志,并查看每次的修改内容:git log -p
  • 查看日志,并查看每次文件的简单修改状态:git log --stat
  • 一行显示日志:git log --pretty=oneline / git log --pretty='format:"%h - %an, %ar : %s'
  • 查看日志范围:
    • 查看最近10条日志:git log -10
    • 查看2周前:git log --until=2week 或者指定2周的明确日期,比如:git log --until=2015-08-12
    • 查看最近2周内:git log --since=2week 或者指定2周明确日志,比如:git log --since=2015-08-12
    • 只查看某个用户的提交:git log --committer=user.name / git log --author=user.name
    • 只查看提交msg中包含某个信息的历史,比如包含'测试'两个字的:git log --grep '测试'
    • 试试这个 : git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit 感觉好用就加成 alias ,方便日后用,方法:git config --global alias.aliasname 'alias-content'
    • 更多用法:Viewing the History -- 《Pro Git2》

log 的目的就是为了查看改动点来排查问题,除了 git log 还可以使用 git show 、git blame 来查看文件的改动。

  • Who changed what and when in a file : git blame $file
  • 查看一次 commit 中修改了哪些文件: git show --pretty="" --name-only <sha1-of-commit> 或者 git diff-tree --no-commit-id --name-only -r <sha1-of-commit>

Undo things

  • 上次提交 msg 错误/有未提交的文件应该同上一次一起提交,需要重新提交备注:git commit --amend -m 'new msg'
  • 修改上次提交的 author、email :git commit --amend --author="newName <newEmail>"
  • 修改整个历史记录中的某些错误的 author、email: git rebase 或者 git filter-branch
    # git rebase  模式
    git rebase -i -p 76892625a7b126f4772f8d7e331ada3552c11ce1
    # 弹出编辑器,在需要修改的 commit 处 由 picked 改变为 edit ,然后 wq 退出 vim;
    git commit --amend --author 'newName <newEmail>'
    # 执行后即变更了相应的 author 和 email
    git rebase --continue
    ################################################################
    # git filter-branch 模式 https://help.github.com/articles/changing-author-info/

git filter-branch --env-filter ' OLD_EMAIL="your-old-email@example.com" CORRECT_NAME="Your Correct Name" CORRECT_EMAIL="your-correct-email@example.com" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags ``` 

  • 一次git add -A后,需要将某个文件撤回到工作区,即:某个文件不应该在本次commit中:git reset HEAD filename
  • 撤销某些文件的修改内容:git checkout -- filename 注意:一旦执行,所有的改动都没有了,谨慎!谨慎!谨慎!
  • 将工作区内容回退到远端的某个版本:git reset --hard <sha1-of-commit>

Reset

reset命令把当前分支指向另一个位置,并且有选择的变动工作目录和索引,也用来在从历史仓库中复制文件到索引,而不动工作目录。

![](./_image/2016-07-14 20-31-39.png?r=64&f=1) 将工作区内容回退到远端的某个版本:git reset --hard <sha1-of-commit>

  • git reset --hard HEAD^ reset index and working directory , 以来所有的变更全部丢弃,并将 HEAD 指向
  • git reset --soft HEAD^ nothing changed to index and working directory ,仅仅将 HEAD 指向 ,所有变更显示在 “changed to be committed”中
  • git reset --mixed HEAD^ default,reset index ,nothing to working directory 默认选项,工作区代码不改动,添加变更到index区

Revert

git revert will create a new commit that's the opposite (or inverse) of the given SHA. If the old commit is "matter", the new commit is "anti-matter"—anything removed in the old commit will be added in the new commit and anything added in the old commit will be removed in the new commit. This is Git's safest, most basic "undo" scenario, because it doesn't alter history—so you can now git push the new "inverse" commit to undo your mistaken commit.

git revert [--[no-]edit] [-n] [-m parent-number] [-s] [-S[<keyid>]] <commit>…​
git revert --continue
git revert --quit
git revert --abort

git反悔的更多相关文章

  1. 使用Git,10件你可能需要“反悔”的事

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

  2. git简单使用总结

    一.git配置 git config 1.git config --global user.name "ken" 配置用户名2.git config --global user.e ...

  3. 操作失误不要慌,这个命令给你的Git一次反悔的机会

    今天我们来介绍git当中两个非常非常好用的工具,git show和reflog. 这两个命令虽然不是必知必会,但是如果熟练使用可以极大地帮助我们查看代码仓库的问题,以及在我们操作失误的时候拯救我们.可 ...

  4. Git 撤消

    现在添加一个新的文件 t.c, 写一行 int a; 用 git add . 添加跟踪,当前状态 $ git status On branch master Changes to be committ ...

  5. 详解在Visual Studio中使用git版本系统[转]

    这篇教程的预期,是希望没有任何版本使用基础的新手也可以掌握,所以细节较多,不当之处,欢迎指正. 一 .安装 git 开发工具 如果要使用 git 进行版本管理,其实使用 git 命令行工具就完全足够了 ...

  6. [git]撤销的相关命令:reset、revert、checkout

    基本概念 工作区 暂存区 本地版本仓库 远程版本仓库 如果不清晰上面的四个概念,请查看廖老师的git教程 这里我多说几句:最开始我使用git的时候,我并不明白我为什么写完代码要用git的一些列指令把我 ...

  7. git中的版本回退

    git版本回退有两种情况,一种是从本地版本库中(head区)回退到某个版本,可以用命令 git reset --hard head^ 或git reset --hard head~x ,head指的是 ...

  8. [译]git reflog

    用法 git reflog 显示整个本地仓储的commit, 包括所有branch的commit, 甚至包括已经撤销的commit, 只要HEAD发生了变化, 就会在reflog里面看得到. git ...

  9. 记一次小团队Git实践(中)

    对于初学者,从使用上先入手,往往学的最快,并从中汲取教训,再回头更深入的学习,效果尤佳. 安装git 安装git自不必说,mac已经内置了git,linux下一个命令就能搞定,windows下需要下载 ...

随机推荐

  1. 监听F5刷新,添加路由前缀

    为了解决ninx反向代理,添加路由,但最终react的route还是不认识,研究了半天做个记录: document.addEventListener("keydown",funct ...

  2. struts2的java.lang.NoSuchMethodException错误

    不久前在学习struts时出现这个错误,在网上搜索了半天,发现答案不一.将其总结如下,以方便大家参考. 1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承 ...

  3. d4-01

    一.字典 1.1 var dict = {"name":"zhangsan"}  定义字典 1.2 dict.name     取得name的值 1.3 del ...

  4. day11_单元测试_读取yaml文件中的用例,自动获取多个yaml文件内容执行生成报告

    一.使用.yaml格式的文件直接可以存放字典类型数据,如下图,其中如果有-下一行有缩进代表这是个list,截图中是整体是一个list,其中有两部分,第二部分又包含另外一个list 二.单元测试:开发自 ...

  5. CentOS下的Mysql的安装和使用

    1.使用安装命令 :yum -y install mysql mysql-server mysql-devel 安装完成却发现Myserver安装缺失,在网上找原因,原来是因为CentOS 7上把My ...

  6. (98)Wangdao.com_第三十天_拖拉事件

    拖拉事件 拖拉 drag ,是指用户在某个对象上按下鼠标键不放,拖动它到另一个位置,然后释放鼠标键,将该对象放在那里. 一旦某个元素节点的 draggable 属性设为true,就无法再用鼠标选中该节 ...

  7. AJAX-wamp安装的“橙色”问题

    安装wamp可能会出现的问题 ##1 正常安装wamp后,打开wamp可以在右下角看到一个图片,绿色即正常,红色或者橙色即意味着Apache+Mysql/MariaDB+Perl/PHP/Python ...

  8. Red hat查找命令所属的rpm包

    当安装命令软件包时,很多时候命令名不一定就是软件包的名字 如scp命令,其命令名就和软件包名字不一样,直接安装会失败: #yum install scp .... Trying other mirro ...

  9. js中级6

    1.动画 (1)Css样式提供了运动 过渡属性transition  从一种情况到另一种情况叫过渡 transition:time          linear                 de ...

  10. 壁虎书1 The Machine Learning Landscape

    属性与特征: attribute: e.g., 'Mileage' feature: an attribute plus its value, e.g., 'Mileage = 15000' Note ...