# Hidden gem named Restore after commit

Have you ever been in a situation where you had to commit some changes while leaving partial, incomplete, and untested changes for later use? This article highlights how TortoiseGit's **Restore after commit** feature can assist you in partially committing files.

Here, I write about TortoiseGit and Git.

# Introduction

Sometimes, you may need to commit only certain parts of the changes made to a file. This situation usually arises when you're working on a project and need to commit an urgent fix, which happens to be in the same file you are editing.

# Scenario

The original file `mathoperations.py` contained a thoroughly tested `subtract` function, which will serve as the base file for our discussion.

```python
# mathoperations.py

def subtract(a: int, b: int) -> int:
    return a - b

def test_subtract():
    assert subtract(2, 1) == 1

if __name__ == "__main__":
    test_subtract()
```

Recently, you added two functions, `add` and `divide`. While `add` is tested thoroughly, `divide` is yet to be tested.

```python
# change 1 : commit add functionality only and push
def add(a: int, b: int) -> int:
    return a + b
def test_add():
    assert add(1, 2) == 3

# Existing
def subtract(a: int, b: int) -> int:
    return a - b
def test_subtract():
    assert subtract(2, 1) == 1

# change 2 : Ignore the divide function from the commit
def divide(a: int, b: int) -> float:
    return a / b

if __name__ == "__main__":
    test_subtract()
    test_add() # change 1 : commit add functionality only and push
```

Imagine a manager asking you to send out any finished changes. Would you send your file with a fully tested `add` and messy `divide`? Definitely not! You would only want to send fully functional `add` and save untested, work-in-progress `divide` somewhere you can resume after committing.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704474077093/75518933-2449-46ca-8528-fa87241e09fa.png align="center")

We will explore two strategies for committing the urgent `add` change while setting aside the unfinished `divide` work for later use.

# Strategy

## Git stash and manual update

**Git stash** is a feature that allows a user to save all changes made to a repository in a stash list. The stash list serves as a permanent holding area for the repository.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703232221194/8b0069cb-321e-4cc9-95c4-978d7bf8ab79.png align="center")

Afterward, retrieve all the changes from the stash list by using **Stash Apply**.

[![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703232707914/8ed929ac-c194-4983-962b-e7d58fe615bb.png align="center")](https://1drv.ms/i/s!Ao4HZItkCNs6g4VuImhpHcY64fhIoQ?e=cITS8A)

You can now selectively check in interested changes using TortoiseGitMerge. Commit change 1 and push urgent change 1.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704468385073/f63e5dd3-e583-4d54-8ea9-8e540c99908a.png align="center")

We can use the **stash apply** again to retrieve an incomplete change, i.e., `divide` function.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704468582058/ed125602-c941-400c-a9b5-b586087db8eb.png align="center")

This strategy is time-consuming, manual, and cumbersome. Let's add ***Restore after commit*** to our arsenal.

## [Restore after commit.](https://tortoisegit.org/docs/tortoisegit/tgit-dug-commit.html#tgit-dug-commit-restore)

We have all changes in the working directory - complete (change 1) plus incomplete (change 2).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704467904080/66462ef8-93a8-47f8-8506-66232e0606cd.png align="center")

Open the TortoiseGit commit dialog and right-click on `mathoperation.py` to choose **Restore after commit**.

[![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703238397092/7f2132aa-5c3b-4c77-97c6-3738db2d3f99.png align="center")](https://1drv.ms/i/s!Ao4HZItkCNs6g4Vxg40HSEYSTN9EBw?e=D3NGIV)

You might notice a change in the file icon. Now double-click on `mathoperation.py` to select only change 1 in TortoiseGitMerge.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704472070886/e6e7ca28-abde-4d6f-b43d-662df8eb6fc4.png align="center")

Commit change 1, and check file status.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704474410172/4b81d5db-d82c-429e-955c-0002bb756045.png align="center")

You are immediately ready with change 2. No Git Stash and say no manual work.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704472274896/e76dd354-77e0-40fd-9f40-4609c3354068.png align="center")

# Signing off

These days, I am reading TortoiseGit documentation to start contributing to it. If I find something worthwhile that will help me and you in the future, you know the place where to navigate. See you here.
