ChampYin's Blog

自律是真正的自由


  • Home

  • Tags

  • Archives

  • About

  • Categories

  • Search

git reflog

Posted on 2018-09-25 | In 工具 , Git | Visitors:
Words : 1.2k | Reading ≈ 5 min

我们对 git log 应该很熟悉了,它是我们常用的用来查看提交记录的命令,而对另一个查看日志的命令 git reflog,可能就不那么熟悉了。我最近因为排查一个线上bug,定位到一段代码被注释掉了,从 log 日志上看,是在一次 merge 的过程中发生的,但因为 rebase 和 amend 等命令的存在,光看 log 有的时候是不可靠的,为了进一步确认该操作是如何发生的,我用到了 git reflog。顺便把 reflog 的用法整理了一份。

git log VS git reflog

git log shows the current HEAD and its ancestry. That is, it prints the commit HEAD points to, then its parent, its parent, and so on. It traverses back through the repo’s ancestry, by recursively looking up each commit’s parent.

(In practice, some commits have more than one parent. To see a more representative log, use a command like git log –oneline –graph –decorate.)

git reflog doesn’t traverse HEAD’s ancestry at all. The reflog is an ordered list of the commits that HEAD has pointed to: it’s undo history for your repo. The reflog isn’t part of the repo itself (it’s stored separately to the commits themselves) and isn’t included in pushes, fetches or clones; it’s purely local.

Aside: understanding the reflog means you can’t really lose data from your repo once it’s been committed. If you accidentally reset to an older commit, or rebase wrongly, or any other operation that visually “removes” commits, you can use the reflog to see where you were before and git reset –hard back to that ref to restore your previous state. Remember, refs imply not just the commit but the entire history behind it.

git log 是显示当前的HEAD和他的祖先的,递归是沿着当前指针的父亲,父亲的父亲……这样的原则。

git reflog 根本不遍历HEAD的祖先,他是HEAD所指向的一个顺序的提交列表。reflog并不是repo的一部分,它单独存储,而且不包含在pushes、fetches、或者clones里,它纯属是本地的。

reflog查看的是所有的 HEAD 改变的记录,约等于记录用户操作行为。reflog 可以很好地帮助你恢复你误操作的数据,例如你错误地 reset 了一个旧的提交,或者 rebase 等等,这个时候想要查看在错误操作之前的信息,log就做不到了,而reflog可以。然后使用 git reset –hard 去恢复之前的状态。

git log shows the commit log accessible from the refs (heads, tags, remotes)
git reflog is a record of all commits that are or were referenced in your repo at any time.
That is why git reflog (a local recording which is pruned after 90 days by default) is used when you do a “destructive” operation (like deleting a branch), in order to get back the SHA1 that was referenced by that branch.
See git config

参考:https://stackoverflow.com/questions/17857723/whats-the-difference-between-git-reflog-and-log

git reflog 命令

git 的版本表示法

HEAD@{2} means “where HEAD used to be two moves ago”, master@{one.week.ago}means “where master used to point to one week ago in this local repository”

HEAD@{2}表示HEAD指针在两次移动之前的情况;而 master@{one.week.ago}表示master在本地仓库一周之前的情况。

命令语法

1
git reflog <subcommand> <options>
1
2
3
4
git reflog [show] [log-options] [<ref>]
git reflog expire [--expire=<time>] [--expire-unreachable=<time>] [--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] [--verbose] [--all | <refs>…​]
git reflog delete [--rewrite] [--updateref] [--dry-run | -n] [--verbose] ref@{specifier}…​
git reflog exists <ref>

“expire”子命令会删除掉更老的reflog条目。

“delete”子命令从reflog中删除一个条目。

“exists”子命令检查一个ref是否有一个reflog。

reflog 高级

reflog 跟 log 一样也可以自定义输出格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
%H: commit hash
%h: 缩短的commit hash
%T: tree hash
%t: 缩短的 tree hash
%P: parent hashes
%p: 缩短的 parent hashes
%an: 作者名字
%aN: mailmap的作者名字 (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
%ae: 作者邮箱
%aE: 作者邮箱 (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
%ad: 日期 (--date= 制定的格式)
%aD: 日期, RFC2822格式
%ar: 日期, 相对格式(1 day ago)
%at: 日期, UNIX timestamp
%ai: 日期, ISO 8601 格式
%cn: 提交者名字
%cN: 提交者名字 (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
%ce: 提交者 email
%cE: 提交者 email (.mailmap对应,详情参照git-shortlog(1)或者git-blame(1))
%cd: 提交日期 (--date= 制定的格式)
%cD: 提交日期, RFC2822格式
%cr: 提交日期, 相对格式(1 day ago)
%ct: 提交日期, UNIX timestamp
%ci: 提交日期, ISO 8601 格式
%d: ref名称
%e: encoding
%s: commit信息标题
%f: sanitized subject line, suitable for a filename
%b: commit信息内容
%N: commit notes
%gD: reflog selector, e.g., refs/stash@{1}
%gd: shortened reflog selector, e.g., stash@{1}
%gs: reflog subject
%Cred: 切换到红色
%Cgreen: 切换到绿色
%Cblue: 切换到蓝色
%Creset: 重设颜色
%C(...): 制定颜色, as described in color.branch.* config option
%m: left, right or boundary mark
%n: 换行
%%: a raw %
%x00: print a byte from a hex code
%w([[,[,]]]): switch line wrapping, like the -w option of git-shortlog(1)

如何让本地分支与远程分支建立映射关系

Posted on 2018-09-25 | In 工具 , Git | Visitors:
Words : 425 | Reading ≈ 1 min

git 建分支很 cheap,本地和远程都常常各自拥有大量分支,有时本地分支需要跟某个新的远程分支建立追踪映射,以便于在 pull、 push 等操作时,简化命令,也在一定程度上防止误传到其他分支。今天建立了新的远程分支,本地不想弄一个新的跟它对应,想用当前分支换个关联,用到了该技能。

Read more »

如何让远程仓库回退到某个之前的版本

Posted on 2018-09-23 | In 工具 , Git | Visitors:
Words : 359 | Reading ≈ 1 min

pull 代码再次遇到冲突,这次突发奇想,想试试用 git stash 来处理,结果 push 完,队友反映她 pull 后好多代码被重置,并且遇到严重冲突,受牵连70几个文件。我查看了下 commit 记录,惊讶地发现,我处理完冲突传上去的代码确实都变成了我本地的老代码,pull 下来的修改都被我重置了。都是乱用 git stash 的错。。还好发现的及时,我立即决定撤回远程仓库中我的那次 push,让代码回滚到我 push 前的状态。那么如何让远程仓库回退到某个之前的版本?步骤如下。

Read more »

如何撤销 git add 和 git commit

Posted on 2018-09-19 | In 工具 , Git | Visitors:
Words : 514 | Reading ≈ 1 min

由于心急,提交代码的时候,commit 后,发现多提交了一个文件,然后第一想法是使用 rebase 来修改提交,然后我把那个多提交的文件,恢复成修改前的样子,然后打算在 git add . 之后进行 rebase ,结果查看状态发现,它把我之前在编辑器里面忽略的一个文件也给加进来了…所以这个时候,我既多 commit 了, 又多 add 了…蜜汁尴尬…

Read more »

进阶(二):hexo博客配置

Posted on 2018-09-19 | In 博客搭建 | Visitors:
Words : 986 | Reading ≈ 4 min

进阶(二):hexo博客配置

进阶配置内容:

  1. 添加评论系统
  2. 添加 tags 页面
  3. 添加 categories 页面
  4. 添加 about 页面
  5. 配置404页
  6. 设置 ‘阅读全文’
  7. 配置博客文档模版
Read more »

如何修改git中已经提交的内容

Posted on 2018-09-11 | In 工具 , Git | Visitors:
Words : 1.6k | Reading ≈ 7 min

今天在git上提交代码的时候,不小心在 commit message 中打了几个错别字,merge、push 完了才发现。。 由于我的完美主义加强迫症比较严重,那几个错别字越看越不顺眼,寻思着把它们给改过来。一番资料搜寻和操作,成功搞定!

Read more »

使用express/koa快速起一个node服务

Posted on 2018-09-11 | In 前端技术 | Visitors:
Words : 189 | Reading ≈ 1 min

express

express 导出的是一个函数。

1. 最简单的服务

1
npm i express
1
2
3
4
5
6
7
8
9
// www.js
const express = require('express');
const app = express();

app.get('/', (req, res) => {
res.end('server by express');
})

app.listen(3000);

2. 使用 express-generator

1
2
3
4
5
npm i express
npx express-generator //需要nodejs8.2及以上
//nodejs8.2以下:
//npm i -g express-generator
//express --view=ejs myproject

会在当前目录下生成一个项目,7个文件夹,9个文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|--app.js
|--bin/
| |-- www.js
|--package.json
|--public/
| |-- images/
| |-- javascript/
| |-- stylesheets/
| |-- style.css
|--routes/
| |-- index.js
| |-- users.js
|--views/
|-- error.jade
|-- index.jade
|-- layout.jade

然后

1
2
3
4
npm i
DEBUGE=projectname:* npm start
//在windows下这样:
//set DEBUG=projectname:* npm start

koa

koa导出的是一个对象。

最简单的服务

1
npm i koa
1
2
3
4
5
6
7
8
9
// www.js
const Koa = require('koa');
cnost app = new Koa();

app.use((ctx) => {
ctx.body = 'server by koa';
})

app.listen(3000);

git错误:HTTP Basic: Access denied

Posted on 2018-09-10 | In 工具 , Git | Visitors:
Words : 465 | Reading ≈ 1 min

上周五修改了gitlab的用户密码,今天发现操作git远程仓库都报错拒绝,错误信息如下:

1
2
remote: HTTP Basic: Access denied
fatal: Athentication failed for 'https://************'

直觉告诉我,是改密码引起。网上查了资料,确实 git 会把第一次输入过的用户名密码存储起来,再次使用 git 命令的时候,会使用存储的用户名密码,然而当 git 的密码修改后,原来存储的密码肯定匹配不了,于是直接报没有权限终止操作。网上类似的帖子很多,但是不是都有效,在多次尝试后,终于解决,解决办法如下:

首先我因为有两台电脑,一台 win7,一台 win10,不同操作系统解决方式还不一样,也是坑了我很多时间。。。

win 10 下的解决办法

Read more »

代码片段

Posted on 2018-09-06 | In 前端技术 | Visitors:
Words : 300 | Reading ≈ 1 min

1. 文字截断

1
2
3
4
5
.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

2. 清除浮动

1
2
3
4
5
6
7
8
.clearfix {
zoom: 1;
}
.clearfix:after {
clear:both;
content: "";
display: block;
}

3. javascript 生成 img 标签的3种方式

方式1: 使用 createElement 方法

Read more »

web端页面如何在移动端也获得较好体验

Posted on 2018-09-05 | In 前端技术 | Visitors:
Words : 205 | Reading ≈ 1 min

在网页的 head 标签里,加上对 viewport 的设置,就可以让页面在移动设备上可以以比较好的缩放和比例来呈现:

1
<meta name="viewport" content="width=device-width, initial-scale=1.0" >

还可以加入更多设置,如缩放之类:

Read more »
1…891011
ChampYin

ChampYin

Life doesn't get easier, you just get stronger.

110 posts
22 categories
115 tags
© 2025 ChampYin | 109.1k
Powered by Hexo
| UV | PV
浙ICP备2020044347号-1