0

I am new in Git. I want to delete previous all commits except the last one from branch TAI-18 and want to push only the latest one for the pull request to merge with master.

For example:

commit_c
commit_b
commit_a

Where commit_a is initial commit and except the commit_c I want to delete all commits like commit_b and commit_a

Can anyone help me?

BDesh
  • 168
  • 1
  • 1
  • 8
  • If you don't want to keep history of the repo, why not just recreating it? Copy your working tree in a new directory and do a `git init`, you're good to go. – Romain Valeri Jul 08 '19 at 12:08
  • 1
    When you say "delete commits", do you mean "remove the changes they introduced" or do you mean "pretend I made 1 commit that contained everything"? – Lasse V. Karlsen Jul 08 '19 at 12:21
  • Yes, everything will have in one commit that is the last commit @LasseVågsætherKarlsen – BDesh Jul 08 '19 at 12:25
  • Your easiest solution would probably be to remove the `.git` directory (***MAKE A BACKUP OF EVERYTHING FIRST***), then create a new repository with `git init .` and then add and commit a new first commit the way you want it to be. – Lasse V. Karlsen Jul 08 '19 at 12:27
  • 1
    Possible duplicate of [Make the current commit the only (initial) commit in a Git repository?](https://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git-repository) – phd Jul 08 '19 at 14:48
  • Please check I've updated my question @Lasse Vågsæther Karlsen – BDesh Jul 09 '19 at 11:16

2 Answers2

1

Having commits:

commit_c
commit_b
commit_a
commit_0

do git rebase -i commit_0, you will see vi interface (or how it's configured):

pick abc1234 commit_c message
pick bcd3455 commit_b message
...

change (press i to INSERT action) pick to one of delete/d/# that you want to remove. It will be removed permanently with all it's changes.

UPDATE as you say you want to keep changes, just use one commit, then you can replace pick with squash/s (except commit_c). It will move anything changed from that commit to previous one.


If your commit_a is initial commit, simply reset all commits and re-do your history

Justinas
  • 41,402
  • 5
  • 66
  • 96
1

For original question: Easiest way to do this would probably be to create a new orphan branch containing the contents of the old branch:

git checkout commit_c    #Checkout latest commit on your branch
git checkout --orphan new_branch_name
git commit

Write some reasonable commit message to describe that this commit is a squash of the old branch. If it looks okay, you can now move your original branch pointer to this commit:

git branch -f original_branch_name

For updated question:

Seems like what you are looking for is a squash merge:

git checkout master
git merge --squash TAI-18

This will take all the changes introduced on TAI-18 branch, and create a commit on master branch which contains all those changes.

If the same files have been updated concurrently on master branch, there may be conflicts, and you will have to resolve those.

Alderath
  • 3,761
  • 1
  • 25
  • 43
  • @BDesh My understanding of the question is quite different now. Previously, it looked like you wanted to remove all previous commits, including initial commit. Now, my interpretation is that you just want to squash merge. Have a look at this [description](https://learn.microsoft.com/en-us/azure/devops/repos/git/merging-with-squash?view=azure-devops) which could be useful here. – Alderath Jul 09 '19 at 11:28
  • Could you clarify if by "initial commit" you mean "the first commit ever made in the repo (ie. the last commit you see when you do `git log`)" (this is normally referred to as the initial commit) or if you mean "the first commit made on your `TAI-18` branch, after it branched out from `master`"? – Alderath Jul 09 '19 at 11:33