3

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?

Marcin
  • 422
  • 6
  • 17
  • possible duplicate of [How to git-cherry-pick only changes to certain files?](http://stackoverflow.com/questions/5717026/how-to-git-cherry-pick-only-changes-to-certain-files) – isherwood Jul 29 '15 at 12:36
  • Try `git add -i` (git help add) – Kenney Jul 29 '15 at 12:43

2 Answers2

3

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

Vangel Tzo
  • 8,885
  • 3
  • 26
  • 32
  • 1
    My version of git hasn't --fixup for commit command. I have created several commits and have run git rebase -i HEAD~. Next I have changed "pick" to "s" for some commits and I have order of these commit. – Marcin Jul 29 '15 at 14:43
1

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.

Community
  • 1
  • 1
cmbuckley
  • 40,217
  • 9
  • 77
  • 91