Marcin
Marcin

Reputation: 422

Add new changes to any commit

Sometimes when I have done some changes (git diff) I want part of them (only particular files) add to older commit.

What is the easiest way to do it?

Upvotes: 3

Views: 753

Answers (2)

Vangel Tzo
Vangel Tzo

Reputation: 9303

You could add the changes you would like to merge in an older commit like this

git add [path] or git add -p

then create a fixup commit with the hash of the commit you would like to move those changes

an example:

git commit --fixup [commit hash]

To merge these 2 commits into 1 you will finally need to do interactive rebase

git rebase -i origin/[branch name] --autosquash

Be ware that you have to force push (with caution) after an interactive rebase

git push -f

Upvotes: 3

cmbuckley
cmbuckley

Reputation: 42458

When you say 'add to older commit', I'm assuming you want to amend the previous commit:

git add ...          # add as you see fit, maybe with -p
git commit --amend

This will squash the added changes into the previous commit. Remember that this modifies history, so if you've pushed that commit to a remote, it may not be a good idea (see How do I push amended commit to the remote Git repository?).

If you want to amend a different commit than the latest one, you'll either need to use --fixup, or commit it and then use rebase --interactive to squash it into the desired commit. But again, bear in mind that you are rewriting history and may run into not only push problems, but conflicts with subsequent commits.

Upvotes: 1

Related Questions