-2

I need a help in git/gitlab. I got gitlab with test repo e.g. 6 commits: A->B->C->D->E->F Commit A is first and F the last one. How Can I do this: remove commits B,C,D,E? I want to have only the first one and last one" A->F I do it because my repo weights a lot. What commands shoul I enter?

ruohola
  • 21,987
  • 6
  • 62
  • 97
user1654358
  • 57
  • 2
  • 8
  • 2
    Does this answer your question? [Delete and completely remove the commit from git history](https://stackoverflow.com/questions/34746245/delete-and-completely-remove-the-commit-from-git-history) – phd Feb 18 '20 at 08:59
  • https://stackoverflow.com/search?q=%5Bgit%5D+remove+old+commits – phd Feb 18 '20 at 08:59

3 Answers3

1

Assuming that you're on branch master at the moment, and that no one else is working on that repo:

git checkout <hash-A>
git cherry-pick <hash-F>
git branch -f master
git checkout master

Then if you are happy with the results you can:

git push -f origin master

Remember that you will permanently lose all changes made in commits B to E.

ruohola
  • 21,987
  • 6
  • 62
  • 97
1
  1. Begin an interactive rebase, choosing a commit early enough that it is before the commits you want to remove, such as A.

    git rebase -i <hash-A>
    
  2. A file opens with a list of commits up to A, with the instruction to pick all the commits.

    In that file that opens, delete the lines of the commits you want to remove.

  3. Save and close the file.

    The rebase will begin automatically and apply your desired changes.

  4. Push your changes, which will require force push due to rewriting history.

    git push -f
    
grg
  • 5,023
  • 3
  • 34
  • 50
1

Generally you would like to do is to use interactive rebase as suggested by another answer.

If you are familiar with how rebase works, you can also do something like:

git rebase rev-of-E rev-of-F --onto rev-of-A

e.g. if your HEAD is at F, you can do

git rebase HEAD~ HEAD --onto HEAD~5
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131