BDesh
BDesh

Reputation: 168

Except only the last commit delete previous all commits

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?

Upvotes: 0

Views: 559

Answers (2)

Alderath
Alderath

Reputation: 3874

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.

Upvotes: 1

Justinas
Justinas

Reputation: 43549

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

Upvotes: 1

Related Questions