Deleting a git commit

When working with Git you will find that sometimes commits need to be removed as they have introduced a bug or need to be reworked.

If it is the last commit this is very straight forward. Simply run:

git reset HEAD

This will pop off the latest commit but leave all of your changes to the files intact.

If you need to delete more than just the last commit there are two methods you can use. The first is using rebase this will allow you to remove one or more consecutive commits the other ischerry-pick which allows you to remove non consecutive commits.

Example git log

Number Hash Commit Message Author
1 2c6a45b (HEAD) Adding public method to access protected method Tom
2 ae45fab Updates to database interface Contractor 1
3 77b9b82 Improving database interface Contractor 2
4 3c9093c Merged develop branch into master Tom
5 b3d92c5 Adding new Event CMS Module Paul
6 7feddbb Adding CMS class and files Tom
7 a809379 Adding project to Git Tom

Using Rebase

Using the git log above we want to remove the following commits; 2 & 3 (ae45fab & 77b9b82). As they are consecutive commits we can use rebase.

git rebase --onto <branch name>~<first commit number to remove> <branch name>~<first commit to be kept> <branch name>

e.g to remove commits 2 & 3 above

git rebase --onto repair~3 repair~1 repair

Using Cherry Pick

Step 1: Find the commit before the commit you want to remove git log

Step 2: Checkout that commit git checkout <commit hash>

Step 3: Make a new branch using your current checkout commit git checkout -b <new branch>

Step 4: Now you need to add the commit after the removed commit git cherry-pick <commit hash>

Step 5: Now repeat Step 4 for all other commits you want to keep.

Step 6: Once all commits have been added to your new branch and have been commited. Check that everything is in the correct state and working as intended. Double check everything has been commited: git status

Step 7: Switch to your broken branch git checkout <broken branch>

Step 8: Now perform a hard reset on the broken branch to the commit prior to the one your want to remove git reset --hard <commit hash>

Step 9: Merge your fixed branch into this branch git merge <branch name>

Step 10: Push the merged changes back to origin. WARNING: This will overwrite the remote repo! git push --force origin <branch name>

You can do the process without creating a new branch by replacing Step 2 & 3 with Step 8 then not carry out Step 7 & 9.

Example

Say we want to remove commits 2 & 4 from the repo.

  1. git checkout b3d92c5 Checkout the last usable commit.
  2. git checkout -b repair Create a new branch to work on.
  3. git cherry-pick 77b9b82 Run through commit 3.
  4. git cherry-pick 2c6a45b Run through commit 1.
  5. git checkout master Checkout master.
  6. git reset --hard b3d92c5 Reset master to last usable commit.
  7. git merge repair Merge our new branch onto master.
  8. git push --hard origin master Push master to the remote repo.

Final note

Git rebase & cherrypick are dangerous but powerful solutions that should only be used as a last option and only be undertaken by someone who knows what they are doing.  Beware that both solutions could have adverse effects on other users who are working on the same repository / branch.

Finally remember to be careful and good luck!

时间: 2024-08-01 03:25:10

Deleting a git commit的相关文章

Git中将git add 与 git commit合并

修改hello.php文件 vim hello.php <?php         echo "hello world!"; ?> 查看hello.php文件 cat hello.php 查看项目文件状态 git status -s add与commit合并操作 git commit -am "合并提交" 命令行输出

如何把apk编译时间和最后次git commit的sha值,写入到app中

需求背景:我们修复Bug的时候,频繁提交APK包,导致测试同学搞不清哪个包才是最新的 比如一个版本3.0.1,我们可能后续基于这个版本陆续提交了好几个修复包 同时,如果服务端ip地址能在界面上配置的话,更好了 以上都是基于debug模式下的,线上版本不会出现这些选项 思路:git每次提交commit的时候,都会创建一个唯一的sha串,我们拿这个作为内部版本号. 先给上最终效果图 那么如何在gradle编译的时候,就把这些信息写入到app中呢 关键字 BuildConfig类. 在build.gr

Git学习01 --git add, git commit , git log ,git status, git reset --hard, head

特点:Git极其强大的分支管理:分布式版本 集中式版本控制系统,版本库是集中存放在中央服务器的,而干活的时候,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器.中央服务器就好比是一个图书馆,你要改一本书,必须先从图书馆借出来,然后回到家自己改,改完了,再放回图书馆.集中式版本控制系统最大的毛病就是必须联网才能工作. 使用Git 1.创建版本库 首先,选择一个合适的地方,创建一个空目录,通过git init命令把这个目录变成Git可以管理

git commit

git commit git commit命令提交stage区的快照到项目历史中去(HEAD). 被提交的快照被认为是一个项目的安全版本. Git不会修改他们, 除非你显示的要求了. 和git add一样git commit是Git最重要的命令之一. 尽管名字相同git commit和svn commit完全不一样. 快照被提交到本地仓储,  不会和其他git仓储有任何的交互影响. 用法 git commit 提交stage区的快照. 上面的命令运行后会自动打开一个文本编辑器让你写一些关于这次c

02_创建Git仓库,克隆仓库,git add,git commit,git push,git pull,同行冲突,不同行冲突的结局方案,git mergetool的使用

1 创建Git资源库,残酷目录信息 创建git资源库的命令: git init –bare 仓库名称 (其中-bare表示的意思是空的库的意思) 进入E:\software\repository\git\itheima28,截图如下: hooks:提交一些脚本文件 info:存放一些个人信息,配置信息 objects:所有数据存放位置 refs:git指针信息,记录了修改了什么等的信息 config:核心的配置信息 description:描述信息 HEAD:存放的分支信息. 2 使用上面创建的

三十五、git commit简介

翻译整理自: http://web.mit.edu/~mkgray/project/silk/root/afs/sipb/project/git/git-doc/git-commit.html 在用git来进行版本控制时,我需要执行git commit命令,将索引内容添加到仓库中. 示例1: git commit  -m "提交的描述信息" 如果我们这里不用-m参数的话,git将调到一个文本编译器(通常是vim)来让你输入提交的描述信息 可能一天下来,你对工作树中的许多文档都进行了更新

如何修改Git commit的信息

原文地址: http://xiguada.org/change-git-commit-message Git cimmit信息push后,如何修改,amend可以修改最后一次commit信息,但对于历史提交信息,需要使用rebase命令. 1 比如要修改的commit是倒数第三条,使用下述命令: git rebase -i HEAD~3 2 把pick改为edit 3 然后 :wq 出现如下信息: 4 执行 git commit --amend 修改commit信息 5 退出保存 :wq 6 执

修改git commit 最后一次提交的注释信息 以及如何退出git bash vim编辑器

今天用git commit -m “注释”提交的时候,注释写错了,于是各种查资料开始了和git bash vim的纠缠...(网上的资料我真是没操作成功,不过最后还是摸索出来了) 首先 使用 git commit --amend 命令,(修改最近一次提交的注释信息),会进入到vim 编辑器 然后 你会发现编辑器里你怎么输入都没反应,这是因为vim处在不可编辑状态,按下字母键 c,此时进入编辑状态,可以开始修改注释信息了 在然后 你会发现你怎么都退出不了,回到shell了,然后操作如下: ESC

git commit --amend用法

提交信息很长时间内会一直保留在你的代码库(code base)中,所以你肯定希望通过这个信息正确地了解代码修改情况. 下面这个命令可以让你编辑最近一次的提交信息,但是你必须确保没有对当前的代码库(working copy)做修改,否则这些修改也会随之一起提交. $ git commit --amend -m ”YOUR-NEW-COMMIT-MESSAGE” 假如你已经将代码提交(git commit)推送(git push)到了远程分支,那么你需要通过下面的命令强制推送这次的代码提交. $ g