bmb
bmb

Reputation: 401

Removing old commits but keeping recent commits

Quite awhile ago I created a branch based off of a co-workers branch and started making some changes. Now, after making some changes for about a week it turns out there are some commits from early last week that we want to remove, but we want to keep some of the more recent commits.

For example, if the commit history looked like this:

0123456 commit-information
6543210 commit-information
1029384 commit-information
1234567 commit-information

Where 0123456 is the oldest commit, and 1234567 is the most recent commit. Is there a way to delete the first two commits but keep the last two commits?

Edit: The first two commits and the last two commits do not depend on each other.

Upvotes: 0

Views: 46

Answers (2)

danielorn
danielorn

Reputation: 6167

A third option is to use git rebase to keep remove a number of commits from a branch

The generic format of such a command would be:

git rebase -p --onto <SHA_First_commit_to_remove>^ <SHA_First_commit_to_keep>^ <branch>

Assuming your branch name is "mybranch", and the SHA's in the OP the command becomes:

git rebase -p --onto 0123456^ 1029384^ mybranch

Upvotes: 0

CodeWizard
CodeWizard

Reputation: 142542

Few options:

  1. Leave the old commits in your history and just "remove" the content

    git revert <sha1> <sha2>
    
  2. "grab" the desired 2 commits to your destination branch

    # "Apply" the required changes to your branch
    git cherry-pick <SHA1->
    

enter image description here

Upvotes: 2

Related Questions