undo/delete a commit on git

areful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command.

Assuming you are sitting on that commit, then this command will wack it...

git reset --hard HEAD~1

The HEAD~1 means the commit before head.

Or, you could look at the output of git log, find the commit id of the commit you want to back up to, and then do this:

git reset --hard <sha1-commit-id>


If you already pushed it, you will need to do a force push to get rid of it...

git push origin HEAD --force

However, if others may have pulled it, then you would be better off starting a new branch. Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

If you already pushed, it may be better to use git revert, to create a "mirror image" commit that will undo the changes. However, both commits will be in the log.



FYI -- git reset --hard HEAD is great if you want to get rid of WORK IN PROGRESS. It will reset you back to the most recent commit, and erase all the changes in your working tree and index.



Lastly, if you need to find a commit that you "deleted", it is typically present in git reflog unless you have garbage collected your repository.

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

undo/delete a commit on git的相关文章

delete master error(git push origin :master)

All good so far. We next want to delete the branch on github. However, if we do this the naive way: git push origin :master we just get an error like this: remote: error: refusing to delete the current branch: refs/heads/master To [email protected]:m

[Git] Undo my last commit and split it into two separate ones

When you accidentally committed some changes to your branch you have various possibilities to “undo” that operation and add some more changes. One is to use git amend to change the commit message or add new files. But what we want to take out all of

git分支合并、撤销;git修改已push的commit信息; git 撤销操作;

git分支合并 1.分支代码提交 2.git branch 查询本地分支 3.git checkout 分支名1 切换分支到需要合并的分支上 4.git merge 分支名2  //选择要合并到 分支名1 的分支名2 eg: 如果develop_screen分支合并到develop分支上,这个时候切换到develop分支上, git merge develop_screen 5.合并之后可能会有冲突,可用git status 查看冲突代码 <<<<<<<  ===

Coding 代码push/commit/pull/git clone

小结: 显示隐藏文件,在系统文件中找到 .ssh文件夹,打开终端,输入ssh-keygen -t rsa -C "[email protected]"之后回车,得到 ssh 中的两个文件,将其中.puh 拖入 sublime 得到公钥,在 coding 中新建项目输入公钥,得到 SSH 和 HTTPS. git clone ssh密钥.....在终端运行

【转】git 删除commit

工作中有一次不小心提交了不该提交的代码到GIT上,而且在发现之前又提交了几个commit,现在想在GIT服务器上永久删除这个commit,具体步骤记录如下. 假设当前分支为master,当前的commit情况如下,现在需要删除commit_id_2和commit_id_4: commit_id_1 commit_id_2 commit_id_3 commit_id_4 commit_id_5 .... 一.创建新的分支,用于reset操作 git checkout -b develop git 

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

git 删除错误提交的commit

转自:http://www.douban.com/note/189603387/ 正好遇到进阶问题.试了一下,是我要的,直接把服务器主版本还原了.不过这样也挺危险的,自己用用还可以. 方法: git reset --hard <commit_id> git push origin HEAD --force 其他: 根据–soft –mixed –hard,会对working tree和index和HEAD进行重置:    git reset –mixed:此为默认方式,不带任何参数的git r

第二章-第二题(练习使用git的add/commit/push/pull/fetch/clone等基本命令)--梁绍楠

题目描述: 每人自己建立一个HelloWorld项目,练习使用git的add/commit/push/pull/fetch/clone等基本命令.比较项目的新旧版本的差别. 使用步骤: (1)创建版本库 选择一个合适的地方,创建一个空目录HelloWorld.而后通过git init把这个目录变成Git可以管理的仓库(目录下会多出了一个.git目录,该目录是git跟踪管理版本库的,勿轻易修改): 编辑hello文件,内容如下: (2)将文件hello放到git仓库 首先,需要设置用户名.邮箱信息

[Git] 还原Git上commit,但是没有push代码

直接在Idea上操作2步解决: 1. 找到: 2. 在To Commit里面填写:HEAD^,表示将commit的信息还原为上一次的,需要多次直接reset多次即可: 使用命令行:原理一样 以下内容转载: http://zhyq0826.iteye.com/blog/1671638 如果不小心commit了一个不需要commit的文件,可以对其进行撤销. 先使用git log 查看 commit日志 commit 422bc088a7d6c5429f1d0760d008d86c505f4abe