How to remove file and commit from history in git (how to remove the commit history in GIT)

Source: Internet
Author: User
Tags sha1

Assume that our commit tree is as follows:

R-A-B-C-D-E-HEAD

Preparations:

Rm-RF. Git test.txt

Touch test.txt & git init & git add.
Echo "R"> test.txt & git ci-a-m "R"
Echo "A"> test.txt & git ci-a-m ""
Echo "B"> test.txt & git ci-a-m "B"
Echo "C"> test.txt & git ci-a-m "C"
Echo "D"> test.txt & git ci-a-m "D"
Echo "E"> test.txt & git ci-a-m "e"

Next, we will remove the commit tree of B and C and change it

R-A D-E-HEAD

There are two ways to remove B & C:

Method 1:

# Detach head and move to d commit
Git checkkout <SHA1-for-D>

# Move head to a, but leave the index and working tree as for d
Git reset -- soft <SHA1-for-A>

# Redo the D commit re-using the commit message, but now on top of
Git commit-C <SHA1-for-D>

Here, you can add, delete (some temporary files accidentally submitted), and then submit.

# Re-apply everything from the old d onwards onto this new place
Git rebase -- onto head <SHA1-for-D> master

# Push it
Git push -- force

Method 2: Use cherry-pick

Git rereset -- hard <sha1 of a>
Git cherry-pick <sha1 of D>
Git cherry-pick <sha1 of E>

Possible problems

Http://rwmj.wordpress.com/2010/05/14/git-cherry-pick-wins/


If it's unable to apply the patch directly, then you'll see a message

Like this:

Automatic cherry-pick failed. After resolving the conflicts,
Mark the corrected paths with 'git add <paths> 'or 'git Rm <paths>' and commit the result.
When commiting, use the option '-C 94e310d' to retain authorship and message.

This is fairly self-explanatory. Use "Git status" to see which files
Are problematic:

Edit the file to manually resolve the conflict, add the file, and then
Commit with the "-C" option noted in the original message.

The bad thing about cherry-pick is that if D is followed by multiple commits, the command should be executed multiple times, and conflicts may need to be resolved manually.

Both methods directly ignore the history of B and C, but the changes made by B and C still exist.
Commit history is removed.

For more information, see git help cherry-pick or git help rebase.

Refer:
Git: removing selected commits from Repository
Http://stackoverflow.com/questions/495345/git-removing-selected-commits-from-repository
Git: how to remove file and commit from history
Http://bogdan.org.ua/2009/02/13/git-how-to-remove-file-commit-from-history.html
Re :! [Rejected] Master-> master (non-fast forward)
Http://kerneltrap.org/mailarchive/git/2007/11/18/425729

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.