Git remove a file in a commit

1 篇文章 0 订阅


http://www.turnkeylinux.org/blog/git-fix-commits

Git - Fixing commit mistakes

I use Git. I use it a lot. I basically use it for everything I do, from code revision control to revisioning my notes, my journal, even my email archive (don't ask, it's a long story).
 
As with anything you do, you are bound to make mistakes. When it comes to coding and revision control, mistakes can range from bad commit messages, to buggy code, to code that shouldn't have been committed together with other code, or even at all.
 
Here are a couple of tips I find myself using quite often.
 

Correcting mistakes in the last commit

This is very easy with git-commit --amend, For example:
 
I decide I don't like the commit description I used.
git-commit --amend # just edit the description
 
I want to edit buggy code I just committed to path/to/file.
editor path/to/file
git-add path/to/file
git-commit -v --amend # note your recent changes are part of the commit
 
I want to remove a file I included accidentally in the last commit (e.g., because I want to commit changes to that file separately).
git-reset HEAD^1 path/to/file # this doesn't delete your changes to path/to/file
git-commit --amend -v
git-commit -v path/to/file # make a new commit with changes to path/to/file
 

Correcting mistakes in previous commits

This also uses git-commit --amend, but involves two extra steps:
1) git-checkout <bad-commit>
   ... make your changes ...
2) git-commit --amend -v
3) git-rebase --onto HEAD <bad-commit> <checked-out-branch>
 
If a conflict occurs while re-applying your commits, you may have to manually resolve the conflict and then git-rebase --continue. See the manual page for further details.
 
For example, I notice I made a mistake in commit 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab several revisions ago and I'm checked out to branch master.
 
To correct the commit history:
git-checkout 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab
editor path/to/file
git-add path/to/file
git-commit --amend -v
git-rebase --onto HEAD 0f0d8a27622e7bf7f008983c4b8ee23bfb9843ab master
 

Implications

Note that by editing your commit history you are effectively re-applying your changes, and thus creating new commits with new commit ids. For example, this means you'll have to recreate existing tags.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值