1

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?

6
  • 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. Commented Jul 8, 2019 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"? Commented Jul 8, 2019 at 12:21
  • Yes, everything will have in one commit that is the last commit @LasseVågsætherKarlsen Commented Jul 8, 2019 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. Commented Jul 8, 2019 at 12:27
  • 1
    Possible duplicate of Make the current commit the only (initial) commit in a Git repository? Commented Jul 8, 2019 at 14:48

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Please revise this answer given the comments on the question.
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.

2 Comments

@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 which could be useful here.
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"?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.