Is it possible to revert only block of code changes of a commit?

Viewed 363

So I am working with Visual Studio Code, and Sourcetree.

Let say I do have a file named Occupation.ts

Occupation.ts (Initial Commit)

private getOccupationCategoryTable(index: number): string {
    switch (index) {
        case 0:
        case 1: return 'occupationCategoryLookUpCommon';
        case 2: return 'occupationCategoryLookUpCreditCard';
        case 3: return 'occupationCategoryLookUpASB';
    }
}

private saveOccupationTable(index: number): string {
    switch (index) {
        case 0:
        case 1: return 'saveOccupationCategoryLookupCommon';
        case 2: return 'saveOccupationCategoryLookupCreditCard';
        case 3: return 'saveOccupationCategoryLookupASB';
    }
}

private updateOccupationTable(index: number): string {
    switch (index) {
        case 0:
        case 1: return 'updateOccupationLookupDetailCommon';
        case 2: return 'updateOccupationLookupDetailCreditCard';
        case 3: return 'updateOccupationLookupDetailASB';
    }
}

Now I made some changes with this file.

Occupation.ts (Latest Changes)

private getOccupationCategoryTable(index: number): string {
    switch (index) {
        case 0:
        case 1: return 'occupationLookUpCommon';
        case 2: return 'occupationLookUpCreditCard';
        case 3: return 'occupationLookUpASB';
    }
}

private saveOccupationTable(index: number): string {
    switch (index) {
        case 0:
        case 1: return 'saveOccupationLookupCommon';
        case 2: return 'saveOccupationLookupCreditCard';
        case 3: return 'saveOccupationLookupASB';
    }
}

private updateOccupationTable(index: number): string {
    switch (index) {
        case 0:
        case 1: return 'updateOccupationLookupDetailCommon';
        case 2: return 'updateOccupationLookupDetailCreditCard';
        case 3: return 'updateOccupationLookupDetailASB';
    }
}

So I made changes on getOccupationCategoryTable, and saveOccupationTable functions. Now I wanted to revert my changes only in getOccupationCategoryTable. Is there any git way to achieve this?

1 Answers

Revert the bad commit and pass the -n flag. This should create a new commit, but it will stage those "undo" changes. Next, a simple git reset will unstage those changes, then use SourceTree to selectively undo chunks within that file:

  1. git revert -n <Commit Id>
  2. git reset
  3. Open SoureTree and select the modified file
  4. Stage chunks that "undo" the bad code.
  5. Commit.
Related