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.
# 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.
# 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.
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.
Afterward, retrieve all the changes from the stash list by using Stash Apply.
You can now selectively check in interested changes using TortoiseGitMerge. Commit change 1 and push urgent change 1.
We can use the stash apply again to retrieve an incomplete change, i.e., divide
function.
This strategy is time-consuming, manual, and cumbersome. Let's add Restore after commit to our arsenal.
Restore after commit.
We have all changes in the working directory - complete (change 1) plus incomplete (change 2).
Open the TortoiseGit commit dialog and right-click on mathoperation.py
to choose Restore after commit.
You might notice a change in the file icon. Now double-click on mathoperation.py
to select only change 1 in TortoiseGitMerge.
Commit change 1, and check file status.
You are immediately ready with change 2. No Git Stash and say no manual work.
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.