Logo of Git Versioning Software

Amending A File in Last Commit

Example: bring a shared commit from a feature branch back to its parent

You’re working on target-branch. You create a new branch from it:

1
git checkout -b source-branch

You make a first commit specific to source-branch, then a second commit with changes that should also live on target-branch.

You need to bring only that last commit back to target-branch. How do you perform this task?

The Solution

1
2
git checkout target-branch
git cherry-pick source-branch

When you pass a branch name without a range, git cherry-pick picks only the latest commit of that branch.

Cherry-Picking Multiple Commits

If you need the last two commits instead:

1
git cherry-pick source-branch~2..source-branch

The range source-branch~2..source-branch means “all commits after the second-to-last up to and including the tip.”

Handling Empty Cherry-Picks

You may encounter this message:

1
The previous cherry-pick is now empty, possibly due to conflict resolution.

It means the changes from that commit already exist on target-branch. The commit produces no diff.

You have two options:

1
2
git cherry-pick --skip      # skip it and move on
git cherry-pick --abort     # cancel the whole operation

This typically happens when a previous merge or cherry-pick already brought the changes in, or when conflict resolution accepted the target branch’s version entirely.

Follow me

Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.

Credit: Git Logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.

License GPLv3 | Terms
Built with Hugo
Theme Stack designed by Jimmy