To do a partial commit in Git, add the –patch option to the git add command.
git add --patch <file-name>
Or for short, you can do:
git add -p <file-name>
Running git add this way iteratively asks you what parts of the changes you want to add. To add the part, type “y”, to not add the part, type “n”.
This is a complete guide to doing partial commits in Git. You will learn how to only stage parts of the changes you’ve made. You’ll also find useful concrete examples that support understanding.
The ‘git add –patch’ Command
To commit specific parts of a file with changes, you need to use the git add command with the –patch option to only stage specific “hunks” of the changes.
git add -p <file-name>
Where the <file-name> is the name of the file with changes. If you leave the file name out, it will consider all the changes.
The ‘git add -p’ Options [y,n,q,a,d,e,?]
When you run the git add -p command, the command line asks what parts of the changes you want to stage. There are many options that Git offers when you’re doing a partial add.
Stage this hunk [y,n,q,a,d,e,?]?
To perform an action, type one of the characters and hit enter. This runs the corresponding action on the change. Usually, you’re only going to use “y” or “n”.
Here’s the full list of options you can choose to do with the change.
y - add this partial change n - do not add this partial change q - (quit) do not ad this partial change or any of the remaining ones a - add this partial change and all later changes in the file d - do not add this partial change or any of the later changes in the file g - select a change to go to / - search for a hunk matching the given regex j - ditch this change, see next undecided change J - ditch this change, see next change k - ditch this change, see previously ditched change K - ditch this change, see previous change s - split the current partial change into smaller partial changes e - manually edit the current partial change ? - display help for the command
How to Review the Staged Parts?
Sometimes the number of partial changes you add can be big and you might want to check what you’ve staged before actually committing the staged changes.
To easily see the partial adds you’ve made, you can run:
git diff --staged
How to Reset a Mistakenly Added Part?
If you accidentally staged a change, you can undo this action by running git reset with the –patch option (-p):
git reset -p
How to view the Commit While Editing the Message?
It can also be helpful to view the partially staged change while writing the commit message. To do this, specify the –view option in git commit:
git commit -v
Let’s see a concrete example of doing partial adds and commits with a real example.
Here’s an example Python file that’s under version control.
Now, let’s make some changes to it.
Now, let’s commit the changes in different patches, such that the code changes highlighted with circles are left out and the ones with arrows are included.
This happens with the git add -p command:
git add -p
Here I only include the 2/2 hunk as it only touches the input parameters. I leave the code fix in the circumference function out of the commit.
Notice that the grouping of the changes isn’t always logical. It’s up to Git what it considers a “hunk”. This can lead to inconsistent or too-long hunks. The next section explains how you can choose what to commit line-by-line.
The ‘git add –edit’ Command
Because the git add -p command might not always group your changes the way you’d like to, doing a partial commit might not work.
To fix this problem, you can use the git add –edit option (git add -e for short).
git add -e <file-name>
Running this command opens up an interactive shell where you can control what’s going to be added line by line. If you don’t want to add the line, just erase it from the view.
The best way to understand how it works is by seeing examples.
Let’s have a look at a concrete example to support understanding.
Here’s a simple text file in my Git project where I’ve added three lines of text:
Instead of committing these changes to the project at once, let’s say we want to commit each line change as a separate commit.
But here’s the issue. Even though you can use git add -p to do a partial add, the reasoning for what’s considered a “part” is somewhat arbitrary.
Let’s try running git add -p on the test.txt file:
$ git add -p test.txt
As you can see, this command doesn’t see the changes as separate parts or “hunk”. Instead, it only offers to commit the changes all at once. This means git add -p doesn’t work in this case.
But you can use the git add -e command to add the changes line by line.
$ git add -e test.txt
This opens up an interactive shell where you can literally choose what to include in the git add command. To exclude lines from staging, simply erase them.
For example, let’s only include the first line in the first commit. To do this, let’s remove other lines from the interactive shell.
Notice that this doesn’t modify the local file but only what’s going to be committed.
After staging the first line change, the git status looks like this:
Next, let’s commit.
$ git commit -m "Add the first line"
Now the first line is committed but the rest of the lines are still not committed. You can see this by running git diff:
To commit the rest of the line changes, we’d need to repeat the actions taken to commit the first line. To keep it short, we’re not going to do it here as it’s merely repeating what you’ve already learned.
Today you learned how to only add parts of the changes in Git. To take home, this happens with the git add -p command (short for git add –patch). When you do this, Git shows you different parts of the changes you’ve made and asks whether you want to add them or not.
Using a partial add is useful if you want to split a larger change into smaller commits as it allows you to choose what parts to include.
If you need a more granular partial add, you can also do the git add -e command. This lets you pick the changes line by line into your commits.
Thanks for reading. Happy coding!
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
- Artificial Intelligence2023.01.10How to Use AI Art Generators—Input Design (Prompt Engineering)
- Favorites2022.11.30ChatGPT—What Is It? How to Use It? (Examples, Tips & More)