How to maintain a clean commit history when working with a team on a new feature

Context

You are working as part of a software development team. You picked up a new ticket to implement some feature (let's call it 'create-new-posts' feature) that will take you a week or so to finish. When you start working on it, at the end of every day, it's expected of you to push your changes to your feature branch. How do you keep doing this while also maintaining correct git commit messages?

The first day of work

On the first day you start working on the 'create-new-posts' feature and create a new branch called feat/add-post-endpoint.

# New branch
git checkout -b feat/add-post-endpoint

When you've done your work for the day, you need to push your update to the remote branch. Do this but write 'WIP' as the commit message.

# First WIP update
git add .
git commit -m 'WIP'
git push -u origin/feat/add-post-endpoint

Every day after the first day, before creating the pull-request

When you are working on the new feature, you still need to commit and push your changes every day even if you haven't finished the feature yet.

The way to do this without adding extra commits is to use the git commit --amend flag (git commit -a for short) to amend the previous commit!

If you don't want to change the commit message, which is likely because you haven't finished the feature, you can also add the --no-edit flag. This willl keep the original commit's message.

# Add all changes to your commit
git add . 
# Amend the previous commit with your new changes
git commit -a --no-edit

Note
Because you are amending the commit, you cannot 'just' push your changes anymore! But that is okay. You can use the --force (-f) flag to force push your changes.

# Force push your changes to the remote branch
git push -f

Creating a pull-request

Once you've managed to check all the acceptation and quality criteria for the new feature, it's time to create a pull request.

Before you do that, you first need to clean up your commit.

Run the following command:

git rebase --interactive HEAD~1
  • rebase: command that moves a sequence / or one commit on top of another commit
  • -i / --interactive: Opens a file in your terminal in which you can specify what you want to do with what commit.
  • HEAD~1: Selects the last commit from the HEAD in the file that the interactive flag opens for you. Also see this stackoverflow answer / thread on the syntax

So if you have a history such as this after running git log --oneline:

- 23JH2K2 (HEAD) WIP
- J3HGK89: Add put endpoint

The file will show only the commit that is the HEAD; the commit with the 'WIP' message.

After running git rebase -i HEAD~1:

git interactive rebase overview: pick a348cf7 WIP If image doesnt load: http://ghost.ykhi.xyz/content/images/2025/11/image.png

Change the pick to e (= edit).

git interactive rebase overview: e a348cf7 WIP

After saving and closing the file, run:

git reset HEAD~

What this will do is show you the changes you made under your WIP commit! Now you can commit every feature separately under a descriptive commit(!).

Hope this is useful.

Thanks to Yuvraj Waghray for teaching me this method!
I originally wrote this article on 2024-05-29.

References

External

  1. (dev.to) - How to split a git commit into multiple ones?, by Tim Mouskhelichvili
  2. (stackoverflow) - What's the difference between HEAD^ and HEAD~ in Git?
  3. (youtube) - Git: how cleanup commit history using interactive rebase, by Team Programmer

Related Articles