git filter-branch
https://github.com/git-for-windows/git/issues/2206
https://git-scm.com/docs/git-filter-branch
The --env-filter option can be used to modify committer and/or author identity. For example, if you found out that your commits have the wrong identity due to a misconfigured user.email, you can make a correction, before publishing the project, like this:
git filter-branch --env-filter '
if test "$GIT_AUTHOR_EMAIL" = "root@localhost"
then
GIT_AUTHOR_EMAIL=john@example.com
fi
if test "$GIT_COMMITTER_EMAIL" = "root@localhost"
then
GIT_COMMITTER_EMAIL=john@example.com
fi
' -- --all
To restrict rewriting to only part of the history, specify a revision range in addition to the new branch name. The new branch name will point to the top-most revision that a git rev-list of this range will print.
Consider this history:
D--E--F--G--H
/ /
A--B-----C
To rewrite only commits D,E,F,G,H, but leave A, B and C alone, use:
git filter-branch ... C..H
To rewrite commits E,F,G,H, use one of these:
git filter-branch ... C..H --not D
git filter-branch ... D..H --not C
Running filter-branch over a range of commits
git filter-branch does accept range notation, but the end of the range needs to be a reference, not the ID of a commit.
git checkout -b tofilter commitb
git filter-branch .... commita..tofilter
If given just commits, it would not know what ref to update with the filtered branch.
Changing the Date on a Series of Git Commits
Changing the Dates
Now what you need to do is rewrite the history of your branch.
Note: Do not rewrite the history of your repository if you have already shared it. Only do this on a local copy that you have never pushed.
At this point you should make a copy of your local repository as a backup. This could all end in disaster.
To rewrite the history we will use the filter-branch command in Git.
The general form for filter-branch is:
git filter-branch --env-filter "<shell code>"
The whole shell code part is what makes this difficult on Windows.
If you have Cygwin, you are probably better off using it as I believe it would be more straight forward.
The problem with the Windows command prompt is that the shell code is Bash code, but the escaping and quoting between Windows and Bash gets a little dicey.
The following (don’t run this) would change the date on the all the commits:
git filter-branch --env-filter "GIT_AUTHOR_DATE='Mon May 28 10:49:30 2015 -0400'; GIT_COMMITTER_DATE='Mon May 28 10:49:30 2015 -0400';"
If you ignored my warning and ran it anyway, or made another mistake, you can restore the backup:
git reset --hard refs/original/refs/heads/master
However, what is needed is a way to change specific commits. To do that, you need to wrap the --env-filter in some logic. Specifically an if statement:
git filter-branch --env-filter "if test $GIT_COMMIT = 'f51ced7692f8509b8b5eb2c47c65cb64aec9f415'; then GIT_AUTHOR_DATE='Mon May 28 10:49:30 2015 -0400'; GIT_COMMITTER_DATE='Mon May 28 10:49:30 2015 -0400'; fi"
Note: On Windows this has to be all on one line. If you break it up, it does not work (unlike on Linux).
If you have to run more than one filter-branch, you will need to add the -f switch to force overwritting of the local backup.
When you run this command it will rewrite all of the future commits. That means that they will get new commit ids and the old ones will be invalid, so if you are rewriting multiple commits you need to manually loop. For example (continuing from the previous commands):
git filter-branch的更多相关文章
- Git Your branch is ahead of 'origin/master' by X commits解决方法
(1)方法1:git fetch origin (2)方法2(代码还需要):git push origin (3)方法3 (代码不需要):git reset --hard origin/$branch ...
- git: Your branch and 'origin/master' have diverged
git: Your branch and 'origin/master' have diverged - how to throw away local commits? - Stack Overfl ...
- Git master branch has no upstream branch的解决
Git master branch has no upstream branch的解决 在push代码时,出现“git master branch has no upstream branch”问题的 ...
- git branch & git remote branch
git branch & git remote branch $ git branch -h usage: git branch [<options>] [-r | -a] [-- ...
- [Practical Git] Filter commit history with git log arguments
In the last lesson, we learned how to format the git log output; in this lesson we will learn how to ...
- git branch(git 的branch 使用方法)
查看分支: $ git branch 该命令会类出当先项目中的所有分支信息,其中以*开头的表示当前所在的分支.参数-r列出远程仓库中的分支,而-a则远程与本地仓库的全部分支. 创 ...
- git 创建branch分支
开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这个分支命名为user1/getopt.(1)确保是在开发者user1的工作区中cd /home/j ...
- git 创建branch分支【转】
转自:http://www.cnblogs.com/jackluo/p/3499731.html 开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这 ...
- Git CMD - branch: List, create, or delete branches
命令格式 git branch [--color[=<when>] | --no-color] [-r | -a] [--list] [-v [--abbrev=<length> ...
- [git] git 分支( branch ) 的基本使用
分支( branches ) 是指在开发主线中分离出来,做进一步开发而不影响到原来主线. Git 存储的不是一系列的更改集( changeset ),而是一系列快照.当你执行一次 commit 时, ...
随机推荐
- Bootstrap 学习笔记12 轮播插件
轮播插件: <!-- data-ride="carousel"自动播放 --> <div id="myCarousel" class=&quo ...
- [LeetCode] 477. Total Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- java序列化的相关介绍
1.什么是序列化?为什么要用序列化? 序列化就是将对象状态转换为可保持或传输的格式的过程.与序列化相对的就是反序列化,他将流转换成对象.这两个过程结合起来,可以轻松地存储和传输数据. 注意:对象序列化 ...
- 批量调整word 图片大小
打开文档后,按Alt+F11,在左边Porject下找到ThisDocument,右键插入模块,贴上下面的 Sub Macro()For Each iShape In ActiveDocument.I ...
- 学习servlet时出现的一些问题
此篇用来记录学习servlet时遇到的一些问题,谨防以后再犯. 问题1.导入的web项目,servlet中导入的包名报错. (1)缺少相关包,推荐一个网站下载jar包很方便http://mvnrepo ...
- C#Contains方法的错误理解
一,我们先看看代码: string aa = "1,2,3,44"; "; "); "); Console.WriteLine("a输出&q ...
- 源码分析--HashMap(JDK1.8)
在JDK1.8中对HashMap的底层实现做了修改.本篇对HashMap源码从核心成员变量到常用方法进行分析. HashMap数据结构如下: 先看成员变量: 1.底层存放数据的是Node<K,V ...
- rm - 移除文件或者目录
总览 rm [options] file... POSIX(Portable Operating System Interface 可移植的操作系统接口) 选项: [-fiRr] GNU 选项 (最短 ...
- iconfont图标symbol引用方式,有的图标不能通过设置color样式来修改颜色的解决办法
现象:iconfont安装后的图标,是通过symbol引用方式,有的图标不能通过color修改颜色的解决办法,有的又可以. <svg class="icon" aria-hi ...
- django框架常用的数据库迁移命令
python manage.py makemigrations 默认所有修改过的model层转为迁移文件 python manage.py migrate 默认将所有的迁移文件都执行,更新数据库 ...